Chp 5 Pic Micro Controller Instruction Set

75
PIC PIC Microcontroller Microcontroller

Transcript of Chp 5 Pic Micro Controller Instruction Set

Page 1: Chp 5 Pic Micro Controller Instruction Set

PIC PIC MicrocontrollerMicrocontroller

Page 2: Chp 5 Pic Micro Controller Instruction Set

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

Page 3: Chp 5 Pic Micro Controller Instruction Set

The architectures of various PIC microcontrollers can be divided

Low - end PIC Architectures

Mid range PIC Architectures

ARCHITECTURES

Page 4: Chp 5 Pic Micro Controller Instruction Set

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

Page 5: Chp 5 Pic Micro Controller Instruction Set

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

Page 6: Chp 5 Pic Micro Controller Instruction Set

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

Page 7: Chp 5 Pic Micro Controller Instruction Set
Page 8: Chp 5 Pic Micro Controller Instruction Set
Page 9: Chp 5 Pic Micro Controller Instruction Set

Program Memory map

Page 10: Chp 5 Pic Micro Controller Instruction Set

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):

Page 11: Chp 5 Pic Micro Controller Instruction Set
Page 12: Chp 5 Pic Micro Controller Instruction Set

PIC PIC Microcontroller Microcontroller Instruction SetInstruction Set

Page 13: Chp 5 Pic Micro Controller Instruction Set

Outline Instruction set Instruction description Assembler directives

Page 14: Chp 5 Pic Micro Controller Instruction Set

Instruction Set PIC16Cxx @

PIC16Fxx: 14-bit word (opcode)

Byte-oriented, bit-oriented & literal and control

Page 15: Chp 5 Pic Micro Controller Instruction Set

Instruction Set cont…

Page 16: Chp 5 Pic Micro Controller Instruction Set
Page 17: Chp 5 Pic Micro Controller Instruction Set

Instruction Instruction DescriptionsDescriptions

Page 18: Chp 5 Pic Micro Controller Instruction Set

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

Page 19: Chp 5 Pic Micro Controller Instruction Set

ADDLW K cont…

Instruction: ADDLW 15HBefore After

W = 10H W = 25H

Page 20: Chp 5 Pic Micro Controller Instruction Set

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

Page 21: Chp 5 Pic Micro Controller Instruction Set

ADDWF f, d cont…

Instruction: MOVLW 17HADDWF 5H, 0

Before After

W = 0H5H = 0H

W = 17H5H = 0H

Page 22: Chp 5 Pic Micro Controller Instruction Set

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

Page 23: Chp 5 Pic Micro Controller Instruction Set

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

Page 24: Chp 5 Pic Micro Controller Instruction Set

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

Page 25: Chp 5 Pic Micro Controller Instruction Set

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?

Page 26: Chp 5 Pic Micro Controller Instruction Set

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

Page 27: Chp 5 Pic Micro Controller Instruction Set

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.

Page 28: Chp 5 Pic Micro Controller Instruction Set

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

Page 29: Chp 5 Pic Micro Controller Instruction Set

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

Page 30: Chp 5 Pic Micro Controller Instruction Set

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

Page 31: Chp 5 Pic Micro Controller Instruction Set

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

Page 32: Chp 5 Pic Micro Controller Instruction Set

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

Page 33: Chp 5 Pic Micro Controller Instruction Set

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

Page 34: Chp 5 Pic Micro Controller Instruction Set

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

Page 35: Chp 5 Pic Micro Controller Instruction Set

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

Page 36: Chp 5 Pic Micro Controller Instruction Set

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

Page 37: Chp 5 Pic Micro Controller Instruction Set

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

Page 38: Chp 5 Pic Micro Controller Instruction Set

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

Page 39: Chp 5 Pic Micro Controller Instruction Set

CLRF f Clear the content of ‘f’ register 00h (f) 1 Z

Instruction: CLRF PORTA

Before AfterPORTA = 5AH PORTA = 00H

Z = 1

Page 40: Chp 5 Pic Micro Controller Instruction Set

CLRW Clear the content WREG register 00h (W) 1 Z

Instruction: CLRW

Before AfterW = 5AH W = 00H

Z = 1

Page 41: Chp 5 Pic Micro Controller Instruction Set

COMP f, d Complement the content of ‘f’ register (f) (d)

Instruction: COMP ONE, 0

Before AfterONE = 13H

W = 02HONE = 13HW = 0ECH

Page 42: Chp 5 Pic Micro Controller Instruction Set

DECF f, d Decrease ‘f’ register (f) – 1 (d)

Instruction: DECF CNT, 1

Before AfterCNT = 01H

Z = 0CNT = 00H

Z = 1

Page 43: Chp 5 Pic Micro Controller Instruction Set

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

Page 44: Chp 5 Pic Micro Controller Instruction Set

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

Page 45: Chp 5 Pic Micro Controller Instruction Set

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

Page 46: Chp 5 Pic Micro Controller Instruction Set

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

Page 47: Chp 5 Pic Micro Controller Instruction Set

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

Page 48: Chp 5 Pic Micro Controller Instruction Set

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

Page 49: Chp 5 Pic Micro Controller Instruction Set

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

Page 50: Chp 5 Pic Micro Controller Instruction Set

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

Page 51: Chp 5 Pic Micro Controller Instruction Set

Review

Find the contents of register WREG after execution of the following code:

MOVLW 0IORLW 0x99XORLW 0xFF

Page 52: Chp 5 Pic Micro Controller Instruction Set

NOP No operation

Instruction: NOP

Page 53: Chp 5 Pic Micro Controller Instruction Set

RETFIE Return from interrupt TOS PC 1 GIE (Global Interrupt Enable) Not affect bit of STATUS register

Instruction: RETFIEBefore After

PC = TOSGIE = 1

Page 54: Chp 5 Pic Micro Controller Instruction Set

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

Page 55: Chp 5 Pic Micro Controller Instruction Set

RETURN Return from subroutine POP the TOS and load into the PC 2-cycle instruction

Instruction: RETURN

Before AfterPC = TOS

Page 56: Chp 5 Pic Micro Controller Instruction Set

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

Page 57: Chp 5 Pic Micro Controller Instruction Set

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

Page 58: Chp 5 Pic Micro Controller Instruction Set

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

Page 59: Chp 5 Pic Micro Controller Instruction Set

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

Page 60: Chp 5 Pic Micro Controller Instruction Set

SLEEP 00h WDT 0 WDT prescalar 1 TO 0 PD Affect TO & PD bits of STATUS register

Instruction: SLEEP

Page 61: Chp 5 Pic Micro Controller Instruction Set

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

Page 62: Chp 5 Pic Micro Controller Instruction Set

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

Page 63: Chp 5 Pic Micro Controller Instruction Set

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)

Page 64: Chp 5 Pic Micro Controller Instruction Set

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

Page 65: Chp 5 Pic Micro Controller Instruction Set

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

Page 66: Chp 5 Pic Micro Controller Instruction Set

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

Page 67: Chp 5 Pic Micro Controller Instruction Set

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

Page 68: Chp 5 Pic Micro Controller Instruction Set

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

Page 69: Chp 5 Pic Micro Controller Instruction Set

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

Page 70: Chp 5 Pic Micro Controller Instruction Set

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’

Page 71: Chp 5 Pic Micro Controller Instruction Set

Assembler Directives

Using EQU for SFR address assignment:COUNTER EQU 0x00PORTB EQU 0x06MOVLW COUNTERMOVWF PORTBINCF PORTB, FINCF PORTB, FINCF PORTB, F

Page 72: Chp 5 Pic Micro Controller Instruction Set

Assembler Directives

Using EQU for RAM address assignment:MYREG EQU 0x12MOVLW 0MOVWF MYREGMOVLW 22HADDWF MYREG, FADDWF MYREG, FADDWF MYREG, F

Page 73: Chp 5 Pic Micro Controller Instruction Set

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…

Page 74: Chp 5 Pic Micro Controller Instruction Set

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…

Page 75: Chp 5 Pic Micro Controller Instruction Set

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…