Professional Documents
Culture Documents
330_05
330_05
Addressing Modes
Method of specifying of an operand
Direct addressing
Indirect addressing
330_05
F,a
F,d,a
Fs,Fd
330_05
SET/CLR Instructions
Instructions CLRF SETF SWAPF
F,a
F,a F,d,a
Examples CLRF REG1 Clear REG1 SETF REG1 Set all bits in REG1 SWAPF REG1,F Exchange low and high nibbles in REG1
330_05
Points to Remember
When instructions copy data from one register to another, the source is not modified In general, these instructions do not affect flags
Except CLRF and MOVF F If d = 0 or W, result saved in WREG If d = 1 or F, result saved in File (data) register
330_03
LFSR
F,12-bit
330_05
Pointer Example
Opcode LFSR Operands FSR1,0x0120 Comments ;Load 120H into FSR1
LFSR
MOVFF
FSR2,0x0150
POSTINC1,POSTINC2
330_05
Indirect Addressing
330_05
10
TBLRD*
Copy from Program Memory into Table Latch Using Table Pointer
Copy from Program Memory into Table Latch and Increment Table Pointer
Copy from Program Memory into Table Latch and Decrement Table Pointer
Increment Table Pointer first and then copy from Program Memory into Table Latch
TBLRD*+
TBLRD+*
330_05
11
Copying Data
330_05
12
Example 5.3
The program memory location BUFFER (at address 000040H) holds the byte F6H. Write the assembly language instructions to copy the byte from BUFFER to the data register REG10 (at address 010H)
330_05
13
Operand 0x10 UPPER BUFFER TBLPTRU HIGH BUFFER TBLPTRH LOW BUFFER TBLPTRL
Comment ;Data Register ;Load upper bits of BUFFER ;Load high byte of BUFFER ;Load low byte of BUFFER ;Copy data to Table Latch
TABLAT,W REG10
;Data Byte
14
Arithmetic Operations
PIC18F MPU
330_05
15
Points to Remember
Arithmetic instructions
330_03
16
330_05
17
Branch Instructions
330_05
18
Points to Remember
Branch instructions use relative addressing
If the operand is positive, the jump is forward If negative, the jump is backward
Flags set by previous instructions used to make decisions
330_05
19
Increment File (Data) Register: INCFSZ F,d,a ;Skip next instruction if F = 0 INFSNZ F,d,a ;Skip next instruction if F 0 Decrement File (Data) Register: DECFSZ F,d,a ;Skip next instruction if F = 0 DCFSNZ F,d,a ;Skip next instruction if F 0
330_05 20
330_05
21
Loop Example
Label COUNT1 START: LOOP1: Opcode EQU ORG MOVLW MOVWF DECF BNZ SLEEP END Operand 0x01 0x20 0x05 COUNT1 COUNT1,F LOOP1 ;Decrement Counter ;Count1 = 0? ;Done ;Initialize Counter to 5 Comments ;Counter is REG01
330_05
22
Loop Example2
Label COUNT1 START: LOOP1: Opcode EQU ORG MOVLW MOVWF DECFSZ BRA SLEEP END Operand 0x01 0x20 0x05 COUNT1 COUNT1,F LOOP1 ;Done ;Decrement Counter, Skip if 0 ;Initialize Counter to 5 Comments ;Counter is REG01
330_05
23
Illustrative Program
Problem Statement
Write a program to copy five data bytes from program memory (with the starting location 000050H called SOURCE) to data registers (with beginning address 010H called BUFFER). When the copying process is complete, turn on all LEDs at PORTC. Data Bytes: F6H, 67H, 7FH, A9H, 72H
330_05
24
330_05
25
BUFFER
EQU
0x10
0x01 0x00 START
330_05
26
Comments ;Init PORTC as Output ;Init COUNTER = 5 ;Init FSR0 Pointer ;Init Table Pointer
MOVF
MOVWF DECF BNZ MOVLW MOVWF SLEEP
TABLAT,W
POSTINC0 COUNTER,F NEXT 0xFF PORTC
;Copy data to W
;Copy to Register ;Inc FSR0 Pointer ;Dec COUNTER ;COUNTER = 0? ;Set W ;Turn ON LEDs
330_05
28
ORG
DB END
0x50
0xF6,0x67,0x7F,0xA9,0x72
330_05
29
330_05
30
Logic Operations
COMF ANDLW ANDWF F,d,a 8-bit F,d,a ;Complement (NOT) F ;and save result in W or F ;AND Literal with W ;AND W with F and ;save result in W or F ;Inclusive OR Literal with W ;Inclusive OR W with F ;and save result in W or F ;Exclusive OR Literal with W ;Exclusive OR W w/ F ;and save result in W or F
IORLW IORWF
XORLW XORWF
8-bit F,d,a
8-bit F,d,a
330_05
31
Application Example
The W register holds a packed BCD number 68H. Write the instructions to mask the high-order four bits (7-4), preserve the low-order bits (3-0), and save the result in REG1.
Label Opcode ANDLW MOVWF Operand B00001111 REG1 Comments ;And with Mask ;Save Result
330_05
32
Points to Remember
Each bit (7-0) of W is logically operated with the corresponding bit of the operand When the operand is a File (data) register, the result can be saved in either W or F using the d parameter These instructions affect only the N and Z flags
330_05
33
BSF F,b,a
BTG F,b,a These instructions can set, reset, or toggle any (single) bit in a data register.
330_05
34
BTFSS F,b,a
330_05
35
Application Example
The MPU checks RC1 (Bit1) at PORTC.
If the switch is open (RC1=1), it stays in the loop and continues to check the switch. When the switch is closed (RC1=0), the MPU skips the branch instruction, and turns on the LED.
Opcode BTFSC BRA BSF Operand PORTC,1 CHECK PORTC,0
Label CHECK:
330_05
36
Bit Rotation
The instruction set includes four instructions that can shift a bit to the adjacent position
Left or right
The instructions are further classified as 8-bit rotation and 9-bit rotation
330_05
37
Rotate Instructions
330_05
38
330_05
39
Unpacking Example
Write a program to unpack a packed BCD byte into two buffers, so it can be displayed on two seven-segment displays.
330_05
40
Unpacking Example
Label BCD0 BCD1 Opcode EQU EQU Operand 0x10 0x11 Comments ;Define Data Registers
REG1
EQU
ORG GOTO
0x01
0x00 START ;Reset Vector
330_05
41
Unpacking Example
Label START: Opcode ORG MOVLW Operand 0x20 0x37 ;Load packed byte ;Mask high order digit ;Save low order digit ;Reload byte ;Mask low order digit ;Rotate 4 times Comments
MOVWF
ANDLW MOVWF MOVF ANDLW RRNCF RRNCF RRNCF RRNCF
REG1
0x0F BCD0 REG1,W 0xF0 WREG,W WREG,W WREG,W WREG,W
MOVWF
SLEEP
BCD1
330_05
Illustrative Program
Find the Highest Temperature in a Data String
Data string includes positive and negative 8-bit readings. Terminated in null character 00. To find the highest temperature. Get a reading and check whether it is zero. Check whether the byte is negative. Is the byte larger than the previously saved data?
If yes, replace the existing byte.
330_05
43
Flow Chart
330_05
44
Highest Temperature
Label Opcode Operand Comments
REG0
BUFFER
EQU
EQU ORG GOTO
0x00
0x10 0x00 START
;Reset Vector
330_05
45
Highest Temperature
Label START: NEXT: Opcode ORG CLRF LFSR MOVF BZ BTFSC BRA CPFSLT BRA MOVWF BRA FINISH SLEEP
330_05 46
Operand 0x20 REG0 FSR0,BUFFER POSTINC0,W FINISH WREG,7 NEXT REG0 NEXT REG0 NEXT
Comments ;Init REG0 ;Init Pointer ;Get Data & Inc Pointer ;Data = 0? ;Data > 0? ;REG0 < Data? ;Save Larger Data
Program Execution
330_05
47