I have been trying to do the following: When a toggle switch is flipped down, the pic I'm using (16F1503) sends a pulse and turns on a led, when it is flipped up sends the same pulse and turns off the led. I managed to cobble some code together and works great. The problem is that I need to manage another toggle switch which it does exactly the same thing, but it sends a pulse on a different port and turns on/off a different led. Any suggestion? I'm having trouble mostly on debouncing multiple switches.
Here the code I'm using:
void main()
{
uint8_t db_cnt;
PORTA=0;
PORTC=0;
ADCON0=0;
TRISA=0b001000;
TRISC=0b010000;
for(;;)
{
//Wait for button press, debounce by counting:
for (db_cnt=0; db_cnt <= 10; db_cnt++)
{
__delay_ms(1); //Sample every 1ms
if (TOGGLELEFT==0) //If toggle off (RA3 low)
db_cnt=0; //Restart count
} //Until button on for 10 successive reads
PORTA=0b100000; //Turn OFF toggle LED, turn ON transistor
__delay_ms(10);
PORTA=0b000000; //Turn OFF transistor
//Wait for button release, debounce by counting:
for (db_cnt=0; db_cnt <=10; db_cnt++)
{
__delay_ms(1); //Sample every 1ms
if (TOGGLELEFT==1) //If toggle on (RA3 high)
db_cnt=0; //Restart count
} //Until button off for 10 successive reads
PORTA=0b110000; //Turn ON toggle LED and transistor
__delay_ms(10);
PORTA=0b010000; //Turn OFF transistor
}
}