Friday, January 2, 2009

Explaining PIC16F84A Flasher Program in Detail

You may have felt that the Flasher program is difficult. But this is not true.

Now, we 'll explain it step-by-step. Just remember, when I started learning Microcontroller programming, I started by understanding this program in detail and put on it my own comments to make me remember what each piece of code did.

list p=16f84
include "p16f84.inc"

; This part is necessary for the compiler to
; know the type of the PIC you are using.

org 0x00
goto start

; The word ORG tells the compiler to put the following code ( goto
; start label ) in the address 0x00 which is the reset vector of the PIC


org 0x20

start

; Again. Puts the label start at the address 0x20

bcf INTCON,7

; Clear the R7 bit in the register INTCON which disables the interrupts

movlw 0x00

; Put 0x00 in the W register

bsf STATUS,5


; Set the Bit 5 in the status register which selects Bank 1 in the RAM

movwf TRISB
; Copy the W register content into TRISB register
; [ makes PORTB output]

bcf STATUS,5
; Clear the Bit 5 in the status register which selects Bank 0 in the RAM

again


; This is a label for the repeating part of the program

movlw 0x80
movwf PORTB

; Copies 0x80 [binary 1000 0000 ] in the W register
; Copy the W register content into PORTB register

call delay

; This command calls the Delay routine.

movlw 0x00
movwf PORTB

; Copies 0x00 [binary 0000 0000 ] in the W register
; Copy the W register content into PORTB register


goto again

; This label is important to keep the program running forever

delay


; This is the label for the delay routine
; The delay consists of 3 nested loops

movlw 0x01
movwf 0x0e

; Put 0x01 in W register
; And copy it to the memory address 0x0e in RAM

loop3


; Label

movlw 0xfa

movwf 0x0d

; Put 0xfa in W register
; And copy it to the memory address 0x0d in RAM

loop2

; Label

movlw 0xfa
movwf 0x0c

; Put 0xfa in W register
; And copy it to the memory address 0x0c in RAM

loop1


; Label

decfsz 0x0c,1
; Decrease contents of memory address 0x0c by 1 ,
; then skip the next command if the result is zero


goto loop1


; goto the outer loop

decfsz 0x0d,1


; Decrease contents of memory address 0x0d by 1 ,
; then skip the next command if the result is zero

goto loop2

; goto the outer loop

decfsz 0x0e,1

; Decrease contents of memory address 0x0e by 1 ,
; then skip the next command if the result is zero

goto loop3

; goto the outer loop

return
; Retrun from the Delay routine

end

; must be put at the end of the program

;*********************************************************************

This is the end of the program. Now that you understand this basic program of LED flasher, you can understand more complex programs and tricks.






If you like this blog you can support us by many ways:
   
   1. Leave comments stating your point of view about this article.

   2. Buy our book on Amazon Learn By Making.

   3. Click on links of our sponsors without adding any extra cost on you if you make purchase from them. Actually, many of these offers are totally free.
  4. Visit our new website outatime.tech.

Thank you for visiting our blog.

1 comment:

Oussema Harbi said...

assalam alaykom wara7matollah

First i wanna say Good job..i was really glad to find some "young" arabian-muslim interrested in PIC programming..
I read some of your posts and they re good (to some extent)..
for this one ,i wanted to suggest that you replace comments on moving 0x00 to PORTB by some more "expressive" comments like "we switch off the port pins/led..."

i hope you ill continue in this effort and may allah bless you :)
fe amen elleh