## Overview of PIC18 Microcontroller Instruction Set

### Instruction Set of PIC microcontroller

Has 77 instructions

- Earlier PIC family of microcontrollers have either 33 or 35 instructions
- In PIC18F instruction set, all instructions are 16-bit word length except four instructions that are 32-bit length

##### PIC18 Instruction Set

- Includes 77 instructions
- 73 one word (16-bit) long
- long 4 two words (32-bit)

##### Divided into seven groups

- Move (Data Copy) and Load
- Arithmetic
- Logic
- Program Redirection (Branch/Jump)
- Bit Manipulation
- Table Read/Write
- Machine Control

#### Arithmetic Instructions

- ADDWFC F,d,a ;Add W to F with carry and save result in W or F
- SUBLW 8-bit ;Subtract W from literal
- SUBWF F,d,a ;Subtract W from F
- SUBWFB F,d,a ;Subtract W from F with borrow
- INCF F,d,a ;Increment F
- DECF F,d,a ;Decrement F
- NEGF F,a ;Take 2’s Complement of F
- MULLW 8-bit ;Multiply 8-bit Literal and W Save result in PRODH:PRODL
- MULWF F,a ;Multiply W and F ;Save result in PRODH:PRODL
- DAW ;Decimal adjust W for BCD ;Addition

##### Points to Remember

- Arithmetic instructions can perform operations on W and 8-bit literals and save the result in W
- Arithmetic instructions can perform operations an W and F and save the result in W or F
- In general, arithmetic instructions affect all flags

#### Logic Instructions

- COMF F,d,a ;Complement (NOT) F ;and save result in W or F
- ANDLW 8-bit ;AND Literal with W
- ANDWF F,d,a ;AND W with F and ;save result in W or F
- IORLW 8-bit ;Inclusive OR Literal with W
- IORWF F,d,a ;Inclusive OR W with F ;and save result in W or F IORWF 0x12,F;OR W with REG12H and ;save result in REG12H
- XORLW 8-bit ;Exclusive OR Literal with W
- XORWF F,d,a ;Exclusive OR W w/ F ;and save result in W or F

##### Points to Remember

- Logic instructions can perform operations on W and 8-bit literals and save the result in W
- Logic instructions can perform operations an W and F and save the result in W or F
- In general, logic instructions affect only two flags: N and Z.

#### Branch Instructions

- BC n ;Branch if C flag = 1, + or – 64 Words ;to PC+2+2n BC 5 ;Branch on Carry to PC+2+10 BC Label ;Alternate: Branch to Label
- BNC n ;Branch if C flag = 0
- BZ n ;Branch if Z flag = 1
- BNZ n ;Branch if Z flag = 0
- BN n ;Branch if N flag = 1
- BNN n ;Branch if N flag = 0
- BOV n ;Branch if OV flag = 1
- BNOV n ;Branch if OV flag = 0
- BRA nn ;Branch always, + or – 512 Words

#### Call and Return Instructions

- RCALL nn ;Relative Call subroutine ;within + or – 512 words
- CALL 20-bit,s ;Call subroutine ;If s = 1, save W, STATUS, BSR
- RETURN s ;Return subroutine ;If s = 1, retrieve W, STATUS, BSR
- RETFIE s ;Return from interrupt ;If s = 1, retrieve W, STATUS, BSR

##### Points to Remember

- The instruction set includes eight conditional relative branch instructions based on four flags. The range is limited to 64 words.
- The range of the unconditional relative branch is 512 words
- If the operand is positive, the jump is forward and if negative, the jump is backward

#### Bit Manipulation Instructions

- BCF F,b,a ;Clear bit b of F, b = 0 to 7 BCF 0x2,7 ;Clear bit 7 of Reg2
- BSF F,b,a ;Set bit b of F, b = 0 to 7
- BTG F,b,a ;Toggle bit b of F, b = 0 to 7
- RLCF F,d,a ;Rotate bits left in F through ;carry and save in W or F
- RLNCF F,d,a ;Rotate bits left in F ;and save in W or F
- RRCF F,d,a ;Rotate bits right in F through carry and save in W or F
- RRNCF F,d,a ;Rotate bits right in F ;and save in W or F

##### Points to Remember

- Any bit in a File (data) register can be set, reset, or complemented
- There are two types of rotate instructions
- 8-bit and 9-bit (include C)
- Bits of any File (data) register can be rotated to the right or left and saved in W or F

#### Test and Skip Instructions

- BTFSC F,b,a ;Test bit b in F and skip the ;next instruction if bit is cleared (bit=0) BTFSC 0x2,7 ;Test bit B7 in REG2 ;if B7=0 then skip next instruction
- BTFSS F,b,a ;Test bit b in F and skip the ;next instruction if bit is set (bit=1)
- CPFSEQ F,a ;Compare F with W, skip if F = W
- CPFSGT F,a ;Compare F with W, skip if F > W
- CPFSLT F,a ;Compare F with W, skip if F < W
- TSTFSZ F,a ;Test F, skip if F = 0

#### Increment/Decrement and Skip Next Instruction

- DECFSZ F,d,a ;Decrement F and skip the ;next instruction if F = 0
- DECFSNZ F,d,a ;Decrement F and skip the ;next instruction if F ≠ 0
- INCFSZ F,d,a ;Increment F and skip the ;next instruction if F = 0
- INCFSNZ F,d,a ;Increment F and skip the ;next instruction if F ≠ 0

#### References

- WikiNote Foundation

Last modified: Thursday, 5 September 2019, 5:05 PM