Please check my schematic simulation using Proteus.
Switch is toggle ON 3 times so that the LED will ON. After that, the PIC will start again to count 3 times of the switch.
The code is as below, any comment? The simulation is not working
- CODE: SELECT_ALL_CODE
#include <htc.h>
#pragma config FOSC = HS // HS oscillator
#pragma config FCMEM = OFF // Fail-Safe Clock Monitor disabled
#pragma config IESO = OFF // Oscillator Switchover mode disabled
#pragma config PWRT = OFF // PWRT disabled
#pragma config BOREN = OFF // Brown-out Reset disabled in hardware and software
#pragma config WDT = OFF // WDT disabled (control is placed on the SWDTEN bit)
#pragma config MCLRE = ON // MCLR pin enabled; RE3 input pin disabled
#pragma config PBADEN = OFF // PORTB<4:0> pins are configured as digital I/O on Reset
#pragma config CCP2MX = ON // CCP2 input/output is multiplexed with RC1
#pragma config LVP = OFF // Single-Supply ICSP disabled
#pragma config XINST = OFF // Extended
delay_ms(unsigned int x); // function prototype for delay
void main()
{
char TEST = 3; // Constant TEST = 3
enum outputs {LED = 3}; // Constant RELAY = 3
ADCON1= 0b00001111; // Make all AN0 - AN12 digital inputs
PORTA = 0; // Reset port A
TRISA = 0xFF; // All portA pins are configured as inputs
PORTD = 0; // Reset port D
TRISD = 0b11110111; // Pin RD3 is configured as an output, while the rest are
// configured as inputs
T0CON=0b11111000; // ON Timer 0,
TMR0 = 0; // Reset timer/counter TMR0
do {
if (TMR0 == TEST) // Does the number in timer match constant TEST?
{PORTD.LED = 1; // Numbers match. Set the RD3 bit (output LED
delay_ms(1500);
}
}
while (1); // Remain in endless loop
}
delay_ms(unsigned int x) // delay 1 mS
{
for(;x>0;x--);
{
T0CON=0b10010000; //Fosc 4MHz, Timer 0,16-bit mode, prescaler 1:2, use internal clock, increment on positive-edge
TMR0H=0xF6;
TMR0L=0X3C;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}
}