The program will only move onto ‘Carry on here’ only if bit 0 on PortA is set to a 1.
Let us now write a program which will flash an LED at one speed, but if a switch is closed it will flash the LED twice as slow. You can probably work this program out for yourself, but We have included the listing anyway. You could try and write the whole program, just to see if you have grasped the concepts. We are using the same circuit as before, with the addition of a switch connected RA0 of the PIC and the positive rail of our supply.
;*****Set up the Constants****
STATUS equ 03h ;Address of the STATUS register
TRISA equ 85h ;Address of the tristate register for port A
PORTA equ 05h ;Address of Port A
COUNT1 equ 08h ;First counter for our delay loops
COUNT2 equ 09h ;Second counter for our delay loops
;****Set up the port****
bsf STATUS,5 ;Switch to Bank 1
movlw 01h ;Set the Port A pins:
movwf TRISA ;bit 1to output, bit 0 to input.
bcf STATUS,5 ;Switch back to Bank 0
;****Turn the LED on****
Start movlw 02h ;Turn the LED on by first putting it
movwf PORTA ;into the w register and then on the port
;****Check if the switch is closed
BTFSC PORTA,0 ;Get the value from PORT A
;BIT 0. If it is a zero
call Delay ;a zero, carry on as normal.
;If is is a 1, then add an
;extra delay routine
;****Add a delay
call Delay
;****Delay finished, now turn the LED off****
movlw 00h ;Turn the LED off by first putting it
movwf PORTA ;into the w register and then on the port
;****Check if the switch is still closed
BTFSC PORTA,0 ;Get the value from port A
;BIT 0. If it is a zero,call
Delay ;carry on as normal.
;If is a 1, then add an
;extra delay routine
;****Add another delay****
call Delay
;****Now go back to the start of the program
goto Start ;go back to Start and turn LED on again
;****Here is our Subroutine
Delay
Loop1 decfsz COUNT1,1 ;This second loop keeps the LED
goto Loop1 ;turned off long enough for us to
decfsz COUNT2,1 ;see it turned off
goto Loop1 ;return
;****End of the program****
end ;Needed by some compilers, and also
;just in case we miss the goto instruction.
What we have done here is to turn the LED on. We then check to see if the switch is closed. If it is closed, then we make a call to our delay subroutine. This gives us the same delay as before, but we are now calling it twice. The same goes for when the LED is off. If the switch is not closed, then we have our old on and off times.
You can compile and run this program. However a word of warning. The final circuit and code will look un-impressive to someone who is not interested in programming microcontrollers. So, don’t be upset if, when you show your family and friends how you can change the speed of a flashing LED with a switch, they show very little interest – We are talking from personal experience, here!
If you have been following these tutorials from the start, then you may be interested to know that you have now learnt 10 of the 35 instructions for the PIC 16F84! And all of these have been learnt just by simply turning an LED on and off.