chp5 pic microcontroller instruction set copy

64
PIC PIC Microcontroller Microcontroller Instruction Set Instruction Set

Upload: mkazree

Post on 12-Jan-2015

6.921 views

Category:

Education


23 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Chp5 pic microcontroller instruction set   copy

PIC PIC Microcontroller Microcontroller Instruction SetInstruction Set

Page 2: Chp5 pic microcontroller instruction set   copy

Outline Instruction set Instruction description Assembler directives

Page 3: Chp5 pic microcontroller instruction set   copy

Instruction Set PIC16Cxx @

PIC16Fxx: 14-bit word (opcode)

Byte-oriented, bit-oriented & literal and control

Page 4: Chp5 pic microcontroller instruction set   copy

Instruction Set cont…

Page 5: Chp5 pic microcontroller instruction set   copy
Page 6: Chp5 pic microcontroller instruction set   copy

Instruction Instruction DescriptionsDescriptions

Page 7: Chp5 pic microcontroller instruction set   copy

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 8: Chp5 pic microcontroller instruction set   copy

ADDLW K cont…

Instruction: ADDLW 15H

Before After

W = 10H W = 25H

Page 9: Chp5 pic microcontroller instruction set   copy

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 10: Chp5 pic microcontroller instruction set   copy

ADDWF f, d cont…

Instruction: MOVLW 17HADDWF 5H, 0

Before After

W = 0H

5H = 0H

W = 17H5H = 0H

Page 11: Chp5 pic microcontroller instruction set   copy

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, 0

Before After

W = 09AH

FSR = 0HW = value in FSR

registerZ = 1

Page 12: Chp5 pic microcontroller instruction set   copy

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 After

W = 09AH W = 5AH

Page 13: Chp5 pic microcontroller instruction set   copy

MOVWF f

Move data from WREG register to f register (W) (f) Not affect bit of STATUS register

Instruction: MOVWF PORTB

Before After

PORTB = 00H

W = 09AHPORTB = 09AH

W = 09AH

Page 14: Chp5 pic microcontroller instruction set   copy

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 15: Chp5 pic microcontroller instruction set   copy

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 16: Chp5 pic microcontroller instruction set   copy

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 17: Chp5 pic microcontroller instruction set   copy

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 18: Chp5 pic microcontroller instruction set   copy

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 19: Chp5 pic microcontroller instruction set   copy

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 20: Chp5 pic microcontroller instruction set   copy

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 After

W = A3H W = 03H

Page 21: Chp5 pic microcontroller instruction set   copy

ANDWF f, d

AND the WREG register with f register (W) + (f) (d) Affect Z bit of STATUS register

Instruction: ANDWF FSR, 1

Before After

W = 17H

FSR = 0C2HW = 17H

FSR = 02H

Page 22: Chp5 pic microcontroller instruction set   copy

BCF f, b

Clear bit ‘b’ in file register 0 (f<b>) Not affect on STATUS register

Instruction: BCF STATUS, 5

Before After

STATUS = 0A7H STATUS = 087H

Page 23: Chp5 pic microcontroller instruction set   copy

BSF f, b

Set bit ‘b’ in f register 1 (f<b>) Not affect on STATUS register

Instruction: BSF INTCON, 7

Before After

INTCON = 0BH INTCON = 08BH

Page 24: Chp5 pic microcontroller instruction set   copy

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 HPC = Add. J if STATUS<2> = 1,PC = Add. I if STATUS<2> = 0

Page 25: Chp5 pic microcontroller instruction set   copy

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 26: Chp5 pic microcontroller instruction set   copy

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

PIC

RA3

RB5

Vcc

Page 27: Chp5 pic microcontroller instruction set   copy

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 After

PC = add. SO PC = add. THEN; TOS = add. SO+1

Page 28: Chp5 pic microcontroller instruction set   copy

CLRF f

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

Instruction: CLRF PORTA

Before After

PORTA = 5AH PORTA = 00HZ = 1

Page 29: Chp5 pic microcontroller instruction set   copy

CLRW

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

Instruction: CLRW

Before After

W = 5AH W = 00HZ = 1

Page 30: Chp5 pic microcontroller instruction set   copy

COMP f, d

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

Instruction: COMP ONE, 0

Before After

ONE = 13H

W = 02HONE = 13HW = 0ECH

Page 31: Chp5 pic microcontroller instruction set   copy

DECF f, d

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

Instruction: DECF CNT, 1

Before After

CNT = 01H

Z = 0CNT = 00H

Z = 1

Page 32: Chp5 pic microcontroller instruction set   copy

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 After

PC = add. HERE CNT = CNT - 1PC = add. CONT if CNT = 0;

PC = add. HERE + 1 if CNT 0

Page 33: Chp5 pic microcontroller instruction set   copy

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 34: Chp5 pic microcontroller instruction set   copy

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 35: Chp5 pic microcontroller instruction set   copy

INCF f, d Increase the content of ‘f’ register (f) + 1 (d) d is destination

Instruction: INCF SATU, 1

Before After

SATU = 0FFH SATU = 00HZ = 1

Page 36: Chp5 pic microcontroller instruction set   copy

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 After

PC = add. HERE CNT = CNT + 1PC = add. CONT if CNT =

0; else add. HERE + 1

Page 37: Chp5 pic microcontroller instruction set   copy

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 38: Chp5 pic microcontroller instruction set   copy

IORLW k

Inclusive OR literal ‘k’ with the content of WREG register

(W) OR k (W) Affect bit ‘Z’ of STATUS register

Instruction: IORLW 35H

Before After

W = 09AH

Z = ?W = 0BFH

Z = 0

Page 39: Chp5 pic microcontroller instruction set   copy

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, 0

Before After

RESULT = 13H

W = 91HRESULT = 13H

W = 93HZ = 0

Page 40: Chp5 pic microcontroller instruction set   copy

Review

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

MOVLW 0IORLW 0x99XORLW 0xFF

Page 41: Chp5 pic microcontroller instruction set   copy

NOP

No operation

Instruction: NOP

Page 42: Chp5 pic microcontroller instruction set   copy

RETFIE

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

Instruction: RETFIE

Before After

PC = TOSGIE = 1

Page 43: Chp5 pic microcontroller instruction set   copy

RETLW k

Return with loading literal ‘k’ onto WREG register, k (W)

TOS PC Not affect on STATUS register

Instruction: RETLW 088H

Before After

W = 09AH W = 088H

Page 44: Chp5 pic microcontroller instruction set   copy

RETURN

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

Instruction: RETURN

Before After

PC = TOS

Page 45: Chp5 pic microcontroller instruction set   copy

RLF f, d

Rotate left f through carry Affect bit ‘C’ of STATUS register

Instruction: RLF REG, 1

Before After

REG = 1111 1111 = 0FFH

C = 0REG = 1111 1110 =

0FEHC = 1

Page 46: Chp5 pic microcontroller instruction set   copy

RRF f, d

Rotate right f through carry Affect bit ‘C’ of STATUS register

Instruction: RRF REG, 1

Before After

REG = 1111 0111 = 0F7H

C = 0REG = 0111 1011 =

07BHC = 1

Page 47: Chp5 pic microcontroller instruction set   copy

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 48: Chp5 pic microcontroller instruction set   copy

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 49: Chp5 pic microcontroller instruction set   copy

SLEEP

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

Instruction: SLEEP

Page 50: Chp5 pic microcontroller instruction set   copy

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 02H

Before After

W = 01H

C = ?

Z = ?

W = 01HC = 1Z = 0

Page 51: Chp5 pic microcontroller instruction set   copy

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 After

W = 01H

F = 05H

C = ?

Z = ?

W = 04HF = 05HC = 1Z = 0

Page 52: Chp5 pic microcontroller instruction set   copy

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 53: Chp5 pic microcontroller instruction set   copy

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 After

ON = 0F4H

W = 09AHON = 04FHW = 09AH

Page 54: Chp5 pic microcontroller instruction set   copy

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 55: Chp5 pic microcontroller instruction set   copy

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 After

W = 0B5H W = 01AH

Page 56: Chp5 pic microcontroller instruction set   copy

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 After

REG = 0AFH

W = 0B5HREG = 01AHW = 0B5H

Page 57: Chp5 pic microcontroller instruction set   copy

Review

Show the results of the following:MOVLW 0x54XORLW 0x78

54H = 0 1 0 1 0 1 0 0

78H = 0 1 1 1 1 0 0 0

2CH = 0 0 1 0 1 1 0 0

54H XOR 78H = 2CH, Z = 0

Page 58: Chp5 pic microcontroller instruction set   copy

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 59: Chp5 pic microcontroller instruction set   copy

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 60: Chp5 pic microcontroller instruction set   copy

Assembler Directives

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

Page 61: Chp5 pic microcontroller instruction set   copy

Assembler Directives

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

Page 62: Chp5 pic microcontroller instruction set   copy

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 63: Chp5 pic microcontroller instruction set   copy

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 64: Chp5 pic microcontroller instruction set   copy

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…