Sunteți pe pagina 1din 19

Microchip PIC 16F84 MicrocontrollerPROGRAMMING

16F84 Pins

27 March 2012

CISL,UNOM

The Registers
Bank 0 and Bank 1. Bank 1 is used to control the actual operation of the PIC. For example to tell the PIC which bits of Port A are input and which are output. First we need to go to Bank 1 to set the particular bit, or pin, on Port A as an output. TRISA allows us to select which pins on Port A are output and which are input, TRISB allows us to select which pins on Port B are output and which are input.

27 March 2012

CISL,UNOM

STATUS: To change from Bank 0 to Bank 1


By setting bit 5 of the STATUS register to 1 To switch Bank 1 to Bank 0, we set bit 5 of the STATUS register to 0. The STATUS register is located at address 03h

TRISA and TRISB,PORTA and PORTB


These are located at addresses 85h and 86h respectively. Port A Pin RA4 RA3 RA2 RA1 RA0 Bit Number 4 3 2 1 0 Binary 0 0 1 1 0

The same goes for TRISB.

27 March 2012

CISL,UNOM

Example 1: how to set up the IO port pins on the PIC to be either input or output

BSF 03h,5 ; now in Bank 1 MOVLW b'00110` MOVLW 06h ; directly into the W register MOVWF 85h ;address points to TRISA.
Our TRISA register now has the value 00110 Port A Pin RA4 RA3 RA2 RA1 RA0 Binary 0 0 1 1 0 Input/output 0 0 1 1 0

Now we have set up our Port A pins, we need to come back to Bank 0 to manipulate any data.
BCF 03h,5
27 March 2012

; now is set bit 5 on our STAUS register to 0 We are now back in Bank 0.
CISL,UNOM 5

Example
BSF MOVLW MOVWF BCF 03h,5 06h 85h 03h,5 ;Go to Bank 1 ;Put 00110 into W ;Move 00110 onto TRISA ;Come back to Bank 0

27 March 2012

CISL,UNOM

Example 2: how to send data to the ports LED on and off


First, let us set up Port A bit 2 as an output: bsf 03h,5 ;Go to Bank 1 movlw 00h ;Put 00000 into W movwf 85h ;Move 00000 onto TRISA all pins set to o/p bcf 03h,5 ;Come back to Bank 0

Turn an LED ON:


movlw 02h ;Write 02h to the W register. 00010 ;puts a 1 on bit 2 (pin 18) while keeping the other pins to 0 ;Now move the contents of W (02h) onto the Port A, whose ;address is 05h

movwf

05h

27 March 2012

CISL,UNOM

So, now LED is ON, we now need to turn it off


movlw 00h ;Write 00h to the W register. This puts a 0 on all pins. movwf 05h ;Now move the contents of W (0h) onto the Port A, whose address is 05h

27 March 2012

CISL,UNOM

LED to turn on then off continuously


Start movlw movwf movlw movwf goto 02h 05h 00h 05h Start ;Write 02h to the W ; move the contents of W to PortA ; Write 00h to the W register. ; move the contents of W (0h) onto A ;Goto where we say Start

27 March 2012

CISL,UNOM

program Recap

Start

bsf movlw movwf bcf movlw movwf movlw movwf goto

03h,5 00h 85h 03h,5 02h 05h 00h 05h Start

27 March 2012

CISL,UNOM

10

*****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 00h ;Set the Port A pins movwf TRISA ;to output. bcf STATUS,5 ;Switch back to Bank 0 ;****Turn the LED on**** Start movlw 02h ;Turn the LED on by first putting movwf PORTA ;****Start of the delay loop 1**** Loop1 decfsz COUNT1,1 ;Subtract 1 from 255 goto Loop1 ;If COUNT is zero, carry on. decfsz COUNT2,1 ;Subtract 1 from 255 goto Loop1 ;Go back to the start of our loop. ;This delay counts down from ;255 to zero, 255 times ;****Delay finished, now turn the LED off**** movlw 00h ;Turn the LED off by first putting movwf PORTA ;it into the w register and then on ;the port ;****Add another delay**** Loop2 decfsz COUNT1,1 ;This second loop keeps the goto Loop2 ;LED turned off long enough for decfsz COUNT2,1 ;us to see it turned off goto Loop2 ; ;****Now go back to the start of the program goto Start ;go back to Start and turn LED ;on again March ;**** End of the program**** 27 2012 CISL,UNOM end

11

Subroutines
A subroutine is a section of code, or program, than can be called as and when you need it. Subroutines are used if you are performing the same function more than once, for example creating a delay. Advantages of using a subroutine are that it will be
Easier to alter the value once inside a subroutine rather than, say, ten times throughout your program Helps to reduce the amount of memory your program occupies inside the PIC.

27 March 2012

CISL,UNOM

12

ROUTI LABEL

COUNT decfsz Goto RETURN

equ 255 COUNT,1 LABEL

27 March 2012

CISL,UNOM

13

;*****Set up the Constants**** STATUS equ 03h TRISA equ 85h PORTA equ 05h COUNT1 equ 08h COUNT2 equ 09h ;****Set up the port****

;Address of the STATUS register ;Address of the tristate register for port A ;Address of Port A ;First counter for our delay loops ;Second counter for our delay loops

bsf STATUS,5 ;Switch to Bank 1 movlw 00h ;Set the Port A pins movwf TRISA ;to output. 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 ;****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 ;****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 goto decfsz goto COUNT1,1 Loop1 COUNT2,1 Loop1 ;This second loop keeps the LED ;turned off long enough for us to ;see it turned off ; ;Needed by some compilers, and also ;just in case we miss the goto instruction.
14

return ;****End of the program**** end

XORWF

So, to turn our LED on and off, we just need two lines: MOVLW 02h XORWF PORTA,1

xorwf xorwf xorwf xorwf


27 March 2012

PORTA 00010 00000 00010 00000 00010


CISL,UNOM 15

;*****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 00h ;Set the Port A pins movwf TRISA ;to output. bcf STATUS,5 ;Switch back to Bank 0 movlw 02h ;Set up our w register with 02h ;****Turn the LED on and off**** Start xorwf PORTA,1 ;Toggle the LED ;****Add a 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

27 March 2012

CISL,UNOM

16

Logical And Arithmetic Operators


Arithmetic Operators
ADD:
ADDLW <number> ADDWF <register>,d where <register, d=0 W register.

Increment INCF <register>,d where <register>


movlw addwf Loop 01 0c,1 incfsz 0C Goto Loop : : Rest of program.
CISL,UNOM 17

27 March 2012

Compliment COMF <register>,d where <register 0C = 11001100 COMF 0C,1 0C = 00110011

27 March 2012

CISL,UNOM

18

THANKYOU

27 March 2012

CISL,UNOM

19

S-ar putea să vă placă și