How to program a pic16f84a

How to program a pic16f84a:
To explain that we’ll use an example of flashing a led and we’ll explain this code.
The code is the following:
start
; assigning names to addresses
STATUS equ    0x03
TRISA  equ   0x85
TRISB  equ   0x86
COUNT1 equ    0x0c
; Port configuration
bsf STATUS,5    ;go to bank1
movlw 0x00      ;put the value 00h " 'h' means in hexadecimal
movwf TRISA     ;put the value in TRISA
bcf STATUS,5     ;go to bank0
; initialize the COUNT1 register
movlw 0xff         ;put the value ffh in the w register
movwf COUNT1
main
movlw 0x01         ;put the value 01h in the w register
movwf PORTA      ;put the value in PORTA
call delay              ;make a delay
call delay              ;make a delay
clrf PORTA            ;make all PORTA pins '0' logic
call delay              ;make a delay
call delay              ;make a delay
goto main
delay
decfsz COUNT1,1  ; decrement COUNT1 by '1' when reaching '0' value then jump
goto delay
movlw 0xff              ;put the value ffh in the w register
movwf COUNT1     ;put the value in COUNT1 register
return
END                       ; directive 'end of program'
NOTE:
To make any bit of the pic16f84a’s port (PORTA or PORTB) as output, we send the logic”0” to the specified bit in the TRIS register (TRISA or TRISB).
Code explanation:
bsf STATUS,5
STATUS equ   0x03
This means that we give the name STATUS to the address o3h, “h” means in hexadecimal, so instead of using addresses it will be easier to use names that we can remember in the program. So each time we write “STATUS” in the program the assembler will use the address 03h instead.
Set the bit 5 in the STATUS register  to change from bank0 to bank1 that contains registers to configure ports (pins as outputs or inputs) and other registers (see PIC16F84A REGISTER FILE MAP).
movlw 0x00  
Put the value 00h “h” means the value is in hexadecimal and in our code we used the “0x” which means also that the value is in hexadecimal.
movwf TRISA    
put the value of the “w” register in TRISA register, the value is 00h and that means that we make all the bits of PORTA as output.
bcf STATUS,5     
clear the bit 5 in the STATUS register  to change from bank1 to bank0 .
movlw 0xff        ;put the value ffh in the w register
movwf COUNT1
put the value ffh in the COUNT register, this value that we are going to decrement till we reach the value 00h and that will make  a delay.
movlw 0x01         ;put the value 01h in the w register
movwf PORTA      ;put the value in PORTA
This will make the bit0 of PORTA high (logic 1)
call delay              ;make a delay
Jump to the delay subroutine to make a delay and then come back to the next instruction.
clrf PORTA            ;make all PORTA pins '0' logic
Make all pins of PORTA low (logic 0).
goto main
Go back to main label, and that will make the program be repeated.
decfsz COUNT1,1  ; decrement COUNT1 by '1' when reaching '0' value then jump
Decrement COUNT1 by '1' when reaching '0' value then jump the next instruction, and in this example it will jump to “movlw 0xff “instruction.
return
End the subroutine and go back to the main program( to the instruction that follows the instruction that called this subroutine).

Facebook

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Bluehost