Microprocessors and Microcontrollers: Unit V: (b) Introduction to RISC Based Architecture

PIC Programming Techniques

Introduction to RISC Based Architecture

PIC Programming Techniques

PIC Programming Techniques

Program Using W Register and Literal Values

MOVLW 15H ; Load 15H into W register

ADDLW 20H ; Add 20H to W register (W = W + 20H = 35H)

ADDLW 36H ; Add 35H to W register (W = W + 35H = 70H)

ADDLW 05H ; Add 05H to W register (W = W + 05H = 75H = 15H + 20H + 35H + 05H)

Accessing locations in the file register (General Purpose Register or SFR)

MOVLW 95H ; Load 95H into W register

MOVWF 10H ; Copy W register contents to location 10H

MOVWF PORTA ; Copy W register contents to Port A

MOVF PORTB, W; Copy data from Port C to W register

MOVF 20H, W ; Copy data from location 20H to W register

Copy data from one location in file register to another location in file register

MOVFF 20H, PORTB ; Copy data from location 20H to Port B

Arithmetic Operation on data from file register location and Saving Result in W register

MOVLW 15H ; Load 15H into W register

ADDWF 5H ; Add contents of location 5H and W register, store result in W register.

; If contents of location 5H = 34H, After execution: W = 15H + 34H = 49H

Arithmetic Operation on data from file register location and Saving Result in W register

MOVLW 15H ; Load 15H into W register

ADDWF 5H, 0 ; Add contents of location 5H and W register, store result in W register.

; If contents of location 5H = 34H, After execution: W = 15H + 34H 49H

Arithmetic Operation on data from file register location and Saving Result in file register location

MOVLW 15H ; Load 15H into W register

ADDWF 5H, 1 ; Add contents of location 5H and W register, store result in location 5H.

; If contents of location 5H = 34H,

; After execution : Location 05 = 15H + 34H = 49H

Sample PIC Assembly Language Program


Arithmetic Operation with Conditional Branch Instruction

Let us add two 8-bit numbers and save 16-bit result in two file register locations

L_Byte EQU 10H ; Assign RAM location 10H to store L_Byte of Sum

H_Byte EQU 11H ; Assign RAM location 11H to store H_Byte of Sum

ORG 0 ; Start at address 0

MOVLW 0 ; W register = 0

MOVWF H_Byte ; H_Byte = 0

MOVLW 85H ; Load first number in W register

ADDLW A5F ; Add second number, W = 85H + A5H 2AH, C = 1

BNC SKIP ; If C = 0, skip the next instruction

INCF H_Byte, F ; If C = 1, increment H_Byte, i.e., H_Byte

SKIP MOVWF L_Byte ; L_Byte = 2AH

END

Looping in PIC

Let us see PIC18 program to add value 5 to W register ten times.

COUNT   EQU 20H ; Reserve location 20H for counter

SUM        EQU 10H ;Reserve location 10H for sum

MOVLW D ‘10' ; Load value decimal 10 in W register for counter

MOVWF COUNT ; Initialize the Counter with value 10

MOVLW 0 ; Sum = 0

BACK   ADDLW 5 ; W = W +5

DECF COUNT, F; Decrement counter and skip next instruction if count = 0

GOTO BACK ; if count ≠ 0, repeat previous two instructions

MOVWF SUM ; Save the sum at location 10H

Using conditional branch instruction

Let us see the same program using conditional branch instruction

COUNT EQU 20H ; Reserve location 20H for counter

SUM   EQU 10H ; Reserve location 10H for sum

MOVLW D '10' ; Load value decimal 10 in W register for counter

MOVWF COUNT ; Initialize the Counter with value 10

MOVLW 0 ; Sum = 0

BACK   ADDLW 5 ; W = W + 5

DECF COUNT, F; Decrement counter

BNZ BACK ; if count ≠ 0, repeat previous two instructions

MOVWF SUM ; Save the sum at location 10H

Using Look-up Tables

The following program will send the ASCII value based on the value in W register to Port C. For example, if W = 0000 0010, program will send ASCII for 2, i.e., '2' to Port C. It is assumed that W register contents value between 0 - 9 (decimal).

ORG   0  ; Start at address 0

MOVLW D `5' ; W register = decimal 5

CALL ASCII_TABLE ; Get ASCII value from Look-up Table

MOVWF PORTC ; Send ASCII value to Port C

ASCII_TABLE

MULLW 0x2 ; Multiply contents of W by 2 to align offset for even address

MOVF PRODL, W ; Put the offset in W register for indexing

ADDWF PCL ; PCL = PCL + W (offset) so that PC will access

; corresponding RETLW instruction

RETLW `0' ; Return ASCII for 0

RETLW 1' ; Return ASCII for 1

RETLW 2' ; Return ASCII for 2

RETLW 3' ; Return ASCII for 3

 RETLW '4'  ; Return ASCII for 4

RETLW '5'  ; Return ASCII for 5

RETLW `6'  ; Return ASCII for 6

RETLW '7'  ; Return ASCII for 7

RETLW `8'  ; Return ASCII for 8

RETLW '9' END  ; Return ASCII for 9

END

Note: In the program memory, Look-up Table contains the opcode for RETLW instruction, followed by ASCII code consuming 2 bytes for each RETLW instruction. Thus to obtain the right offset, we must multiply the contents of the W register by 2. ADDWF PCL instruction allows PC to access the correct RETLW instruction and once executed it returns the corresponding ASCII code.

Microprocessors and Microcontrollers: Unit V: (b) Introduction to RISC Based Architecture : Tag: : Introduction to RISC Based Architecture - PIC Programming Techniques