Вы находитесь на странице: 1из 2

Activity 1 - Blinking

PIC Microcontroller Programming


Introduction
Most PIC microcontroller instruction takes one clock cycle to complete. If we are using a 4MHz crystal oscillator
then each instruction cycle takes 1/4MHz or 1s. In the code below, by adding the BCF instruction and making it
loop will make it blink. It shall blink but is too fast that human eyes cant see. The loop requires 4 instruction
cycles to execute. The first one turns on RB0. The second turns it off while the GOTO takes 2 instructions of the
time. This means that the LED that is connected to RB0 will be on for 25% of the time or 1us.
loop
BSF PORTB,0
BCF PORTB,0
GOTO loop

; 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

;write the value 0xF1 to working register


;move to the register 0x0C

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

;go to the bank1


;make RB0 an output
;clear RB0
;return to the bank0

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

;return to the main loop

end
Problems:
1. Generate an approximately 5Hz square wave.
2. Generate an approximately 5Hz PWM with 30% duty cycle.

Вам также может понравиться