chp 5 pic micro controller instruction set
TRANSCRIPT
PIC PIC MicrocontrollerMicrocontroller
PIC stands for Peripheral or programmable Interface Controller given by Microchip Technology to identify its single-chip microcontrollers.
These devices have been very successful in 8-bit microcontrollers. The main reason is that Microchip Technology has continuously upgraded the device architecture and added needed peripherals to the microcontroller to suit customers' requirements.
PIC Microcontroller
The architectures of various PIC microcontrollers can be divided
Low - end PIC Architectures
Mid range PIC Architectures
ARCHITECTURES
Microchip PIC microcontrollers are available in various types. When PIC microcontroller MCU was first available from General Instruments in early 1980's, the microcontroller consisted of a simple processor executing 12-bit wide instructions with basic I/O functions. These devices are known as low-end architectures. They have limited program memory and are meant for applications requiring simple interface functions and small program & data memories. Some of the low-end device numbers are
12C5XX 16C5X 16C505
Low - end PIC Architectures
Mid range PIC architectures are built by upgrading low-end architectures with more number of peripherals, more number of registers and more data/program memory. Some of the mid-range devices are
16C6X 16C7X 16F87X
Program memory type is indicated by an alphabet. C = EPROM F = Flash RC = Mask ROM
Mid range PIC Architectures
Speed: Harvard Architecture, RISC architecture, 1 instruction cycle = 4 clock cycles.
Instruction set simplicity: The instruction set consists of just 35 instructions (as opposed to 111 instructions for 8051).
Power-on-reset and brown-out reset. Brown-out-reset means when the power supply goes below a specified voltage (say 4V), it causes PIC to reset; hence malfunction is avoided. A watch dog timer (user programmable) resets the processor if the software/program ever malfunctions and deviates from its normal operation.
PIC microcontroller has four optional clock sources. ◦ Low power crystal ◦ Mid range crystal◦ High range crystal◦ RC oscillator (low cost).
PIC microcontrollers -Features
Program Memory map
Data Memory is also known as Register File. Register File consists of two components. General purpose register file (same as RAM). Special purpose register file (similar to SFR in 8051).
Data memory (Register Files):
PIC PIC Microcontroller Microcontroller Instruction SetInstruction Set
Outline Instruction set Instruction description Assembler directives
Instruction Set PIC16Cxx @
PIC16Fxx: 14-bit word (opcode)
Byte-oriented, bit-oriented & literal and control
Instruction Set cont…
Instruction Instruction DescriptionsDescriptions
ADDLW K Add the literal value K to register WREG and
put the result back in the WREG register (W) + k (W) K is an 8-bit value: 0-255 (decimal), 00-FF
(hex) L: literal (actual value) Affect STATUS bits: C, DC, Z
ADDLW K cont…
Instruction: ADDLW 15HBefore After
W = 10H W = 25H
ADDWF f, d Add together contents of WREG and a file
register location (SFR @ GPR). Put the result in the register WREG if d = 0
otherwise it is stored back in register f. (W) + (f) (d) 0 f 127, d [0, 1] Affect STATUS bits: C, DC, Z
ADDWF f, d cont…
Instruction: MOVLW 17HADDWF 5H, 0
Before After
W = 0H5H = 0H
W = 17H5H = 0H
MOVF f, d Move the content of f register upon the status of
d (f) (d) Affect bit ‘Z’ of STATUS register
Instruction: MOVF FSR, 0Before After
W = 09AHFSR = 0H
W = value in FSR registerZ = 1
MOVLW k Load k literal into WREG register k (W) Don’t cares will be assembled as 0’s Not affect bit of STATUS register
Instruction: MOVLW 5AH
Before AfterW = 09AH W = 5AH
MOVWF f Move data from WREG register to f register (W) (f) Not affect bit of STATUS register
Instruction: MOVWF PORTB
Before AfterPORTB = 00H
W = 09AHPORTB = 09AH
W = 09AH
Review
1. Write instructions to move value 34H into the WREG register.
2. Write instructions to add the values 16H and CDH. Place the result in the WREG register.
3. True or false. No value can be moved directly into the WREG register.
4. What is the largest hex value that can be moved into an 8-bit register? What is the decimal equivalent of that hex value?
Review
5. What is the result of the following code and where is it kept?
MOVLW 15HADDLW 13H
6. Which of the following is (are) illegal?(a) MOVLW 500 (b) MOVLW50(c) MOVLW 00 (d) MOVLW255H(e) MOVLW 25H (f) MOVLW F5H(g) MOVLW mybyte, 50H
Review
7. Which of the following is (are) illegal?(a) ADDLW 300H (b) ADDLW 50H(c) ADDLW $500(d) ADDLW 255H(e) ADDLW 12H (f) ADDLW 0F5(g) ADDLW 25H
8. True or false. We have many WREG registers in the PIC16.
9. The literal value in MOVLW is _____ bits wide.
Review
10. The instruction “MOVLW 44H” is a ____-byte instruction.
11. True or false. All the instructions in the PIC16 are 2-cycle instructions.
12. Give the value in WREG for the following:MYCOUNT EQU 15MOVLW MYCOUNT
Review 13. Give the value in fileReg 0x20 for the following:
MYCOUNT EQU 0x95MYREG EQU 0x20MOVLW MYCOUNTMOVWF MYREG
14. Give the value in fileReg 0x63 for the following:MYDATA EQU D’12’MYREG EQU 0x63FACTOR EQU 0x10MOVLW MYDATAADDLW FACTORMOVWF MYREG
Review
15. Find the C, Z and DC flag bits for the following code:MOVLW 9FHADDLW 61H
16. Find the C, Z and DC flag bits for the following:MOVLW 82HADDLW 22H
ANDLW k Logical AND k literal with the content of WREG
register & the result is placed in the WREG register(W) AND k (W)Affect Z bit of STATUS register
Instruction: ANDLW 5FH
Before AfterW = A3H W = 03H
ANDWF f, d AND the WREG register with f register (W) + (f) (d) Affect Z bit of STATUS register
Instruction: ANDWF FSR, 1
Before AfterW = 17H
FSR = 0C2HW = 17H
FSR = 02H
BCF f, b Clear bit ‘b’ in file register 0 (f<b>) Not affect on STATUS register
Instruction: BCF STATUS, 5
Before AfterSTATUS = 0A7H STATUS = 087H
BSF f, b Set bit ‘b’ in f register 1 (f<b>) Not affect on STATUS register
Instruction: BSF INTCON, 7
Before AfterINTCON = 0BH INTCON = 08BH
BTFSS f, b Execute the next instruction if bit ‘b’ in file register ‘f’ is
‘0’, otherwise discard executing next instruction 2-cycle instruction Not affect on STATUS register
Instruction: H BTFSS STATUS, 2 I GOTO LOOP
J ……… ………
Before After
PC = address H PC = Add. J if STATUS<2> = 1,PC = Add. I if STATUS<2> = 0
BTFSC f, b Execute the next instruction if bit ‘b’ in file register ‘f’ is
‘1’, otherwise discard executing next instruction 2-cycle instruction Not affect on STATUS register
Instruction: H BTFSC PORTA, 3 I GOTO LOOP
J ……… ………
Before After
PC = address HPC = Add. J if PORTA<3> = 0, PC = Add. I if PORTA<3> = 1
Review Assume RA3 is an input, represents condition of a door alarm. If it goes LOW, it means that the door is opened. Create a program to monitor the bit continuously. Whenever it goes LOW, send a HIGH pulse to port RB5 and turn on buzzer
BSF 03H, 5BSF 85H, 3BCF 86H, 5BCF 03H, 5
HERE BTFSC 05H, 3GOTO OFFBSF 6H, 5GOTO HERE
OFF BCF 6H, 5GOTO HERE
PICRA3
RB5
Vcc
CALL k Call subroutine (PC) + 1 TOS (top of stack) k PC<10:0> (PCLATCH<4:3>) PC<12:11> 2-cycle instruction Not affect on STATUS register
Instruction: SO CALL THEN ………
………
Before AfterPC = add. SO PC = add. THEN;
TOS = add. SO+1
CLRF f Clear the content of ‘f’ register 00h (f) 1 Z
Instruction: CLRF PORTA
Before AfterPORTA = 5AH PORTA = 00H
Z = 1
CLRW Clear the content WREG register 00h (W) 1 Z
Instruction: CLRW
Before AfterW = 5AH W = 00H
Z = 1
COMP f, d Complement the content of ‘f’ register (f) (d)
Instruction: COMP ONE, 0
Before AfterONE = 13H
W = 02HONE = 13HW = 0ECH
DECF f, d Decrease ‘f’ register (f) – 1 (d)
Instruction: DECF CNT, 1
Before AfterCNT = 01H
Z = 0CNT = 00H
Z = 1
DECFSZ f, d Decrease ‘f’ register and skip the next instruction if the result is 0;
otherwise execute the next instruction (f) – 1 (d), skip if result = 0 2-cycle instruction
Instruction: HERE DECFSZ CNT, 1GOTO HERE
CONT ……… ……..
Before AfterPC = add. HERE CNT = CNT - 1
PC = add. CONT if CNT = 0; PC = add. HERE + 1 if CNT
0
GOTO k
Unconditional branch k PC<10:0> (PCLATCH<4:3>) PC<12:11> 2-cycle instruction
Instruction: HERE GOTO THERE ………
THERE ………Before After
PC = add. HERE PC = add. THERE
Review Toggle all the bits of the Port B by sending to it the values 55H and AAH continuously. Put a time delay in between each issuing of data to Port B.
MYREG EQU 0x08ORG 0
BACK MOVLW 0x55MOVWF PORTBCALL DELAYMOVLW 0xAAMOVWF PORTBCALL DELAYGOTO BACK
;this is the delay subroutineORG 300HDELAY MOVLW 0xFF
MOVWF MYREGAGAIN NOP
NOPDECFSZ MYREG, FGOTO AGAINRETURNEND
INCF f, d Increase the content of ‘f’ register (f) + 1 (d) d is destination
Instruction: INCF SATU, 1
Before AfterSATU = 0FFH SATU = 00H
Z = 1
INCFSZ f, d Increase the content of ‘f’ register and skip the next instruction if the
result is 0; otherwise execute the next instruction (f) + 1 (d), skip if result = 0 2-cycle instruction
Instruction: HERE INCFSZ CNT, 1 GOTO loop
CONT ……… ……..
Before AfterPC = add. HERE CNT = CNT + 1
PC = add. CONT if CNT = 0; else add. HERE + 1
Review The difference between “INCF f, W” & “INCF f, F”
MOVLW OMOVWF Ox20INCF 0x20, WINCF 0x20, WINCF 0x20, WINCF 0x20, FINCF 0x20, FINCF 0x20INCF 0x20INCF 0x20, W
;WREG = 0;0x20 = (0), WREG = 1;0x20 = (0), WREG = 1;0x20 = (0), WREG = 1;0x20 = (1), WREG = 1;0x20 = (2), WREG = 1;0x20 = (3), WREG = 1;0x20 = (4), WREG = 1;0x20 = (4), WREG = 5
IORLW k Inclusive OR literal ‘k’ with the content of WREG
register (W) OR k (W) Affect bit ‘Z’ of STATUS register
Instruction: IORLW 35HBefore After
W = 09AHZ = ?
W = 0BFHZ = 0
IORWF f, d Inclusive OR the content of WREG register with f
register (W) OR (f) (d) Affect bit ‘Z’ of STATUS register
Instruction: IORWF RESULT, 0Before After
RESULT = 13HW = 91H
RESULT = 13HW = 93H
Z = 0
Review
Find the contents of register WREG after execution of the following code:
MOVLW 0IORLW 0x99XORLW 0xFF
NOP No operation
Instruction: NOP
RETFIE Return from interrupt TOS PC 1 GIE (Global Interrupt Enable) Not affect bit of STATUS register
Instruction: RETFIEBefore After
PC = TOSGIE = 1
RETLW k Return with loading literal ‘k’ onto WREG
register, k (W) TOS PC Not affect on STATUS register
Instruction: RETLW 088H
Before AfterW = 09AH W = 088H
RETURN Return from subroutine POP the TOS and load into the PC 2-cycle instruction
Instruction: RETURN
Before AfterPC = TOS
RLF f, d
Rotate left f through carry Affect bit ‘C’ of STATUS register
Instruction: RLF REG, 1Before After
REG = 1111 1111 = 0FFHC = 0
REG = 1111 1110 = 0FEHC = 1
RRF f, d
Rotate right f through carry Affect bit ‘C’ of STATUS register
Instruction: RRF REG, 1
Before AfterREG = 1111 0111 = 0F7H
C = 0REG = 0111 1011 =
07BHC = 1
Review
Find the contents of file register MYREG after execution of the following code:MYREG EQU 0x20
BCF STATUS, CMOVLW 0x26MOVWF MYREGRRF MYREG, FRRF MYREG, FRRF MYREG, F
;C=0;WREG=0010 0110
;MYREG=0001 0011 C=0;MYREG=0000 1001 C=1;MYREG=1000 0100 C=1
Review
Find the contents of file register MYREG after execution of the following code:MREG EQU 0x20
BSF STATUS, CMOVLW 0x15MOVWF MYREGRLF MYREG, FRLF MYREG, FRLF MYREG, FRLF MYREG, F
;C=1;WREG=0001 0101
;MYREG=0010 1011 C=0;MYREG=0101 0110 C=0;MYREG=1010 1100 C=0;MYREG=0101 1000 C=1
SLEEP 00h WDT 0 WDT prescalar 1 TO 0 PD Affect TO & PD bits of STATUS register
Instruction: SLEEP
SUBLW k Subtract WREG register (2’s complement) from literal ‘k’
and put the result onto WREG register k – (W) (W) Affect C, DC & Z bits of STATUS register
Instruction: SUBLW 02HBefore After
W = 01HC = ?Z = ?
W = 01HC = 1Z = 0
SUBWF f, d Subtract WREG register (2’s complement) from f register (f) – (W) (d) Affect C, DC & Z bits of STATUS register
Instruction: SUBWF 02H, 0
Before AfterW = 01HF = 05H
C = ?Z = ?
W = 04HF = 05HC = 1Z = 0
Review
Show the steps involved in the following.MOVLW 0x23SUBLW 0x3F
K = 3F 0011 1111- WREG = 23 0010 0011
1C
0011 1111+ 1101 1101 (2’s complement)1 0001 1100 C = 1, Z = 0 (result is +ve)
SWAPF f, d Exchange the upper & lower nibbles of f register (f<3:0) (d<7:4>), (f<7:4) (d<3:0>) Not affect STATUS register
Instruction: SWAPF ON, 1
Before AfterON = 0F4HW = 09AH
ON = 04FHW = 09AH
Review
Find the contents of the MYREG register in the following code.
MYREG EQU 0x20MOVLW 0x72MOVWF MYREGSWAPF MYREG, F
;WREG = 72H;MYREG = 72H;MYREG = 27H
XORLW k Exclusive OR (XOR) the content of WREG
register with k literal (W) XOR k (W) Store the result in WREG register Affect bit ‘Z’ of STATUS register
Instruction: XORLW 0AFH
Before AfterW = 0B5H W = 01AH
XORWF f, d Exclusive OR (XOR) the content of WREG
register with f register (W) XOR (f) (d) Affect bit ‘Z’ of STATUS register
Instruction: XORWF REG, 1
Before AfterREG = 0AFH
W = 0B5HREG = 01AHW = 0B5H
Review
Show the results of the following:MOVLW 0x54XORLW 0x78
54H = 0 1 0 1 0 1 0 078H = 0 1 1 1 1 0 0 02CH = 0 0 1 0 1 1 0 0
54H XOR 78H = 2CH, Z = 0
Assembler Directives Also known as pseudo-instructions Give directions to assembler EQU, ORG, END
EQU directive: Define a constant value or a fixed address
COUNT EQU 0x25….MOVLW COUNT
Assembler Directives
Using EQU for fixed data assignment:;in hexadecimalDATA1 EQU 39DATA2 EQU 0x39DATA3 EQU 39HDATA4 EQU H’39’DATA5 EQU h’39’
;in binaryDATA6 EQU B’00110101’DATA7 EQU b’00110101’
;in decimalDATA8 EQU D’28’DATA9 EQU d’28’
;in ASCIIDATA10 EQU A’2’DATA11 EQU a’2’DATA12 EQU ‘2’
Assembler Directives
Using EQU for SFR address assignment:COUNTER EQU 0x00PORTB EQU 0x06MOVLW COUNTERMOVWF PORTBINCF PORTB, FINCF PORTB, FINCF PORTB, F
Assembler Directives
Using EQU for RAM address assignment:MYREG EQU 0x12MOVLW 0MOVWF MYREGMOVLW 22HADDWF MYREG, FADDWF MYREG, FADDWF MYREG, F
Assembler Directives
SET directive: Define a constant value or a fixed address Identical with EQU directive, the only difference
is the value assigned by the SET directive may be reassigned later
END directive: Indicate the end of the source (asm) file
cont…
Assembler Directives
LIST directive: Unique to PIC assembler Indicate specific PIC chip for which the program
should be assembledLIST P = 16F84A
#include directive: Tells the PIC assembler to use the libraries
associated with the specific chip to compile the program
cont…
Assembler Directives
_config directive: Tells the assembler the configuration bits for the target
device Incorrect use may cause the chip unusable
CONFIG OSC=HSCONFIG WDT=OFF
radix directive: Indicate numbering system whether it is hexadecimal or
decimal RADIX DEC
cont…