330 03
330 03
330_02
330_02
330_03
330_03
ALU
Instruction Decoder
16-bit Instructions
8-bit Data 5 individual bits called flags
Table Latch
8-bit Accumulator
16-bit Product of 8-bit by 8-bit Multiply
330_03 6
Product
N (Negative Flag)
OV (Overflow Flag)
Z (Zero Flag)
C (Carry Flag)
Registers
Program Counter (PC) 21-bit register functions as a pointer to Program Memory during program execution Table Pointer 21-bit register used as a pointer to copy bytes between Program Memory and data registers Stack Pointer (SP) 5-bit register used to point to the stack Stack 31 registers used for temporary storage of memory addresses during execution of a subroutines
330_03 8
Registers
BSR: Bank Select Register (0H to FH)
4-bit Register
330_03
Data registers associated with I/O ports, support devices, and processes of data transfer
I/O Ports (A to E) Interrupts EEPROM Serial I/O Timers Capture/Compare/PWM (CCP) Analog-to-Digital (A/D) Converter
330_03 10
Move (Data Copy) and Load Arithmetic Logic Program Redirection (Branch/Jump) Bit Manipulation Table Read/Write Machine Control
330_03
11
Addressing Modes
Method of specifying of an operand
Direct addressing
Indirect addressing
330_05
12
Points to Remember
Each instruction has two parts
When instructions copy data from one register to another, the source is not modified In general, these instructions do not affect flags
330_03
15
Arithmetic Instructions
ADDLW ADDLW ADDWF 8-bit 0x32 F,d,a ;Add 8-bit number to W & set flags ;Add 32H to W ;Add W to F & set flags ;Save result in W if d = 0 (or W) ;Save result in F if d = 1 (or F) ;Add W to REG20H and ;save result in W ;Alternate format ;Add W to REG20H and ;save result in REG20H ;Alternate format
330_03 16
Arithmetic Instructions
ADDWFC SUBLW SUBWF SUBWFB INCF DECF NEGF F,d,a 8-bit F,d,a F,d,a F,d,a F,d,a F,a ;Add W to F with carry ;and save result in W or F ;Subtract W from literal ;Subtract W from F ;Subtract W from F with borrow ;Increment F ;Decrement F ;Take 2s Complement of F
330_03
17
Arithmetic Instructions
MULLW MULWF DAW 8-bit F,a ;Multiply 8-bit Literal and W ;Save result in PRODH:PRODL ;Multiply W and F ;Save result in PRODH:PRODL ;Decimal adjust W for BCD ;Addition
330_03
18
Points to Remember
Arithmetic instructions
330_03
19
Logic Instructions
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 ;OR W with REG12H and ;save result in REG12H ;Exclusive OR Literal with W ;Exclusive OR W w/ F ;and save result in W or F
330_03 20
IORLW IORWF
IORWF XORLW XORWF
8-bit F,d,a
0x12,F 8-bit F,d,a
Points to Remember
Logic instructions
330_03
21
Branch Instructions
BC BC BC BNC BZ BNZ BN BNN BOV BNOV BRA ;Branch if C flag = 1, + or 64 Words ;to PC+2+2n 5 ;Branch on Carry to PC+2+10 Label ;Alternate: Branch to Label n ;Branch if C flag = 0 n ;Branch if Z flag = 1 n ;Branch if Z flag = 0 n ;Branch if N flag = 1 n ;Branch if N flag = 0 n ;Branch if OV flag = 1 n ;Branch if OV flag = 0 nn ;Branch always, + or 512 Words n
330_03 22
Branch Example
Address Label 000020 000022 000024 000026 000028 00002A 00002C Opcode MOVWF MOVLW MOVWF ADDWF BNC MOVLW Operand BYTE1 REG0 BYTE2 REG1 REG0,W SAVE 0x00 Comment ;Load BYTE1 into W ;Save into REG0 ;Load BYTE2 into W ;Save into REG1 ;Add REG0 to REG1 ;Branch if no carry ;Clear W START: MOVLW
00002E
000030
SAVE:
MOVWF
SLEEP
REG2
;Save Result
330_03
23
RETURN s RETFIE s
330_03
24
Points to Remember
Eight conditional relative branch instructions
If the operand is positive, the jump is forward If negative, the jump is backward
330_03
25
RRCF RRNCF
Points to Remember
Any bit in a File (data) register
Saved in W or F
330_03
27
330_03
28
330_03
29
Points to Remember
Any File (data) register or single bit in a File (data) register can be tested for 0 A File (data) register can be compared with W for equality, greater than, and less than A File (data) register can be incremented or decremented and tested for 0 If a condition is met, the next instruction is skipped (no flags are affected)
330_03 30
330_03
31
330_03
32
330_03
33
Instruction Format
The PIC18F instruction format divided into four groups
330_03
34
Instruction Format
Byte-oriented instruction ADDWF F, d, a
ADDWF 0x1,F ;Add W to REG1, save in REG1
330_03
35
Instruction Format
Literal instruction MOVLW k
MOVLW 0x7F ;Load 7FH into W
Branch instruction BC n
BC 0x15 ;Branch if carry +15H words
330_03
36
Illustration: Addition
Problem Statement
Load two bytes (37H and 92H) in registers REG0 and REG1 Add the bytes and store the sum in REG2
Opcode MOVLW MOVWF Operand 0x37 REG0 Comments ;Load first byte in W ;Save first byte in REG0
0024
0026 0028 002A 002C
0E92
6E01 2400 6E02 0003
MOVLW
MOVWF ADDWF MOVWF SLEEP
0x92
REG1 REG0,W REG2
330_03
37
Bus Contents
Execution of the instruction: MOVLW 0x37
330_03
38
330_03
39