Your request is slightly more complex than you first imagine, because if the PIC is busy timing one LED, it can't really be attending to the other. So we have to do a bit of magic...
Here I've changed the Labels to make association between Switches, LED's, Constants and Variables more readable.
Like Clint Eastwood said in the Dirty Harry films... "Every man has to know his limitations". Now this applies equally to PICs and programs as well. Here, in our example program, the limitation I've set, is that everything happens in multiples of 10mS... but the flip side is that with this method you can add as many LED's and Switches as you have pins on your PIC and each can have different timing rates...
Code:
SwitchA var PORTA.0 ' switch on porta.0
SwitchB var PORTA.3 ' switch on portb.0
LedA VAR PORTB.0 ' LED on portb.0
LedB VAR PORTB.5 ' LED on PortB.5
TRISA.0 = 1 ' porta.0 is now an input
TRISA.3 = 1 ' Porta.3 is now an input
TRISB.0 = 0 ' Portb.0 is left
TRISB.5 = 0 ' Portb.5 is STB
CMCON=%00000111 ' ensures PortA is in Digital Mode
LedABlinkRate con 10 ' Blink Rate in Steps of 10mS (10=100mS)
LedBBlinkRate con 33 ' Blink Rate in Steps of 10mS (33=330mS)
LedACounter var BYTE ' Counter for LedA
LedBCounter var BYTE ' Counter for LedB
Low LedA ' Startup with all LEDs OFF
Low LedB
LedACounter=0 ' Startup with all Counters OFF
LedBCounter=0
Main:
If SwitchA=0 THEN ' Closing SwitchA blinks LedA
If LedACounter=0 then
Toggle LedA
LedACounter=LedABlinkRate
else
If LedACounter>0 then LedACounter=LedACounter-1
endif
else
LedACounter=0
low LedA
endif
If SwitchB=0 THEN ' Closing SwitchB blinks LedB
If LedBCounter=0 then
Toggle LedB
LedBCounter=LedBBlinkRate
else
If LedBCounter>0 then LedBCounter=LedBCounter-1
endif
else
LedBCounter=0
low LedB
endif
Pause 10
Goto Main
end
Just added a minor change (zeroing LedACounter and LedBCounter in the main loop), so if you missed it first time, copy the code again.
Bookmarks