Activity 1 - Blinking (PICmicro Assembly Programming)
Activity 1 - Blinking (PICmicro Assembly Programming)
; turns on RB0
; turns off RB0
; do it again
To appreciate the blinking, we need to slow down the blinking so it can be seen. This is done by implementing a
delay.
The simple principle of a delay is to make a variable counter that counts down and when zero is reached, the
counting stops. The first thing that we need to know is how define a constant to use as the counter. This can be
done inside the working register that has a size of 8-bits; the largest possible number is 255 or FFh. By using the
EQU we could assign a value to a variable inside the situated inside the working registers. However, we cannot
assign a value of FFh to a register because it is a reserve value for unused locations (unimplemented locations).
FFh is also the value of an undefined register content. Lower than FFh is possible. To write a value to a the
working register we can do this:
MOVLW F1h
MOVWF 0Ch
The INCFSZ and DECFSZ instructions add or subtract one from the contents of the file register and skips the
next instruction when the result is zero. One use is demonstrated below.
Loop
DECFSZ Delay,f
GOTO Loop
The GOTO Loop simply does is over again until the Delay content becomes zero. It takes 3 instructions for a
single loop. One for decrement and two for the GOTO and the counter will force the loop to go for 256 times to
a total of 768 instructions cycles or 768s. This is still too fast for an eye to see. By adding another loop around
the loop above, the delay could be increased further.
Loop
DECFSZ Delay1,f
GOTO Loop
DECFSZ Delay2,f
GOTO Loop
The inner loop will take 768us plus 3 for the outer loop. The outer loop will force an execution for 256 times
more resulting to (768+3)*256 = 197676s 197ms. Below is the complete sample code for implementing a
delay.
1
#include <p16F84a.inc>
__config (_PWRTE_OFF & _WDT_OFF & _CP_OFF & _HS_OSC)
cblock 0X0C
DELAY1
DELAY2
endc
org 0
;reset vector
start
bsf
bcf
bcf
bcf
main_loop
bsf
STATUS,RP0
TRISB,0
PORTB,0
STATUS,RP0
PORTB,0
;set RB0
loop1
decfsz
goto
decfsz
goto
DELAY1,f
loop1
DELAY2,f
loop1
bcf
PORTB,0
decfsz
goto
decfsz
goto
DELAY1,f
loop2
DELAY2,f
loop2
;clear RB0
loop2
goto main_loop
end
Problems:
1. Generate an approximately 5Hz square wave.
2. Generate an approximately 5Hz PWM with 30% duty cycle.