PIC Instruction Set

64
PIC PIC Microcontroller Microcontroller Instruction Set Instruction Set

description

PIC Instruction Set

Transcript of PIC Instruction Set

  • PIC Microcontroller Instruction Set

  • OutlineInstruction setInstruction descriptionAssembler directives

  • Instruction SetPIC16Cxx @ PIC16Fxx: 14-bit word (opcode)Byte-oriented, bit-oriented & literal and control

  • Instruction Set cont

  • Instruction Descriptions

  • ADDLW KAdd 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 contInstruction:ADDLW 15HW = 25H

    BeforeAfterW = 10H

  • ADDWF f, dAdd 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 contInstruction:MOVLW 17HADDWF 5H, 0W = 17H5H = 0H

    BeforeAfterW = 0H5H = 0H

  • MOVFf, d Move the content of f register upon the status of d(f) (d)Affect bit Z of STATUS register

    Instruction: MOVF FSR, 0W = value in FSR registerZ = 1

    BeforeAfterW = 09AHFSR = 0H

  • MOVLW k Load k literal into WREG registerk (W)Dont cares will be assembled as 0sNot affect bit of STATUS register

    Instruction: MOVLW 5AHW = 5AH

    BeforeAfterW = 09AH

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

    Instruction: MOVWF PORTBPORTB = 09AHW = 09AH

    BeforeAfterPORTB = 00HW = 09AH

  • Review Write instructions to move value 34H into the WREG register.Write instructions to add the values 16H and CDH. Place the result in the WREG register.True or false. No value can be moved directly into the WREG register.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?MOVLW15HADDLW13HWhich of the following is (are) illegal?(a) MOVLW 500(b) MOVLW50(c) MOVLW 00(d) MOVLW255H(e) MOVLW 25H(f) MOVLWF5H(g) MOVLW mybyte, 50H

  • Review Which of the following is (are) illegal?(a) ADDLW 300H(b) ADDLW50H(c) ADDLW $500(d) ADDLW 255H(e) ADDLW 12H(f) ADDLW 0F5(g) ADDLW 25H8.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:MYCOUNTEQU15MOVLWMYCOUNT

  • Review 13.Give the value in fileReg 0x20 for the following:MYCOUNTEQU0x95MYREGEQU0x20MOVLW MYCOUNTMOVWFMYREG

    14.Give the value in fileReg 0x63 for the following:MYDATAEQUD12MYREGEQU0x63FACTOREQU0x10MOVLWMYDATAADDLWFACTORMOVWFMYREG

  • Review 15.Find the C, Z and DC flag bits for the following code:MOVLW 9FHADDLW61H

    16.Find the C, Z and DC flag bits for the following:MOVLW82HADDLW22H

  • 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 5FHW = 03H

    BeforeAfterW = A3H

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

    Instruction:ANDWF FSR, 1W = 17HFSR = 02H

    BeforeAfterW = 17HFSR = 0C2H

  • BCF f, b Clear bit b in file register0 (f)Not affect on STATUS register

    Instruction:BCF STATUS, 5STATUS = 087H

    BeforeAfterSTATUS = 0A7H

  • BSF f, b Set bit b in f register1 (f)Not affect on STATUS register

    Instruction:BSF INTCON, 7INTCON = 08BH

    BeforeAfterINTCON = 0BH

  • BTFSS f, b Execute the next instruction if bit b in file register f is 0, otherwise discard executing next instruction2-cycle instructionNot affect on STATUS register

    Instruction: H BTFSS STATUS, 2 I GOTO LOOP J PC = Add. J if STATUS = 1,PC = Add. I if STATUS = 0

    BeforeAfterPC = address H

  • BTFSC f, b Execute the next instruction if bit b in file register f is 1, otherwise discard executing next instruction2-cycle instructionNot affect on STATUS register

    Instruction: H BTFSC PORTA, 3 I GOTO LOOP J PC = Add. J if PORTA = 0, PC = Add. I if PORTA = 1

    BeforeAfterPC = address H

  • 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 buzzerBSF03H, 5BSF85H, 3BCF86H, 5BCF03H, 5HEREBTFSC05H, 3GOTO OFFBSF6H, 5GOTOHEREOFFBCF6H, 5GOTO HERE

  • CALL k Call subroutine(PC) + 1 TOS (top of stack)k PC (PCLATCH) PC2-cycle instructionNot affect on STATUS register

    Instruction: SO CALL THEN PC = add. THEN; TOS = add. SO+1

    BeforeAfterPC = add. SO

  • CLRF f Clear the content of f register00h (f)1 Z

    Instruction: CLRF PORTAPORTA = 00HZ = 1

    BeforeAfterPORTA = 5AH

  • CLRW Clear the content WREG register00h (W)1 Z

    Instruction: CLRWW = 00HZ = 1

    BeforeAfterW = 5AH

  • COMPf, d Complement the content of f register(f) (d)

    Instruction: COMPONE, 0ONE = 13HW = 0ECH

    BeforeAfterONE = 13HW = 02H

  • DECFf, d Decrease f register(f) 1 (d)

    Instruction: DECFCNT, 1CNT = 00HZ = 1

    BeforeAfterCNT = 01HZ = 0

  • 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 = 02-cycle instruction

    Instruction: HERE DECFSZ CNT, 1GOTO HERE CONT ..CNT = CNT - 1PC = add. CONT if CNT = 0; PC = add. HERE + 1 if CNT 0

    BeforeAfterPC = add. HERE

  • GOTOk Unconditional branchk PC (PCLATCH) PC2-cycle instruction

    Instruction: HERE GOTO THERE THERE PC = add. THERE

    BeforeAfterPC = add. HERE

  • 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.MYREGEQU0x08ORG0BACKMOVLW0x55MOVWFPORTBCALLDELAYMOVLW0xAAMOVWFPORTBCALLDELAYGOTOBACK;this is the delay subroutineORG300HDELAYMOVLW0xFFMOVWFMYREGAGAINNOPNOPDECFSZMYREG, FGOTOAGAINRETURNEND

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

    Instruction: INCFSATU, 1SATU = 00HZ = 1

    BeforeAfterSATU = 0FFH

  • 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 = 02-cycle instruction

    Instruction: HERE INCFSZ CNT, 1 GOTO loop CONT ..CNT = CNT + 1PC = add. CONT if CNT = 0; else add. HERE + 1

    BeforeAfterPC = add. HERE

  • Review The difference between INCF f, W & INCF f, F

    MOVLWOMOVWFOx20INCF0x20, WINCF0x20, WINCF0x20, WINCF0x20, FINCF0x20, FINCF0x20INCF0x20INCF0x20, 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 35HW = 0BFHZ = 0

    BeforeAfterW = 09AHZ = ?

  • 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, 0RESULT = 13HW = 93HZ = 0

    BeforeAfterRESULT = 13HW = 91H

  • Review Find the contents of register WREG after execution of the following code:MOVLW 0IORLW0x99XORLW0xFF

  • NOP No operation

    Instruction: NOP

  • RETFIE Return from interruptTOS PC1 GIE (Global Interrupt Enable)Not affect bit of STATUS register

    Instruction: RETFIEPC = TOSGIE = 1

    BeforeAfter

  • RETLW k Return with loading literal k onto WREG register, k (W)TOS PCNot affect on STATUS register

    Instruction: RETLW 088HW = 088H

    BeforeAfterW = 09AH

  • RETURN Return from subroutinePOP the TOS and load into the PC2-cycle instruction

    Instruction: RETURNPC = TOS

    BeforeAfter

  • RLFf, d

    Rotate left f through carryAffect bit C of STATUS register

    Instruction: RLFREG, 1REG = 1111 1110 = 0FEHC = 1

    BeforeAfterREG = 1111 1111 = 0FFHC = 0

  • RRFf, d

    Rotate right f through carryAffect bit C of STATUS register

    Instruction: RRFREG, 1REG = 0111 1011 = 07BHC = 1

    BeforeAfterREG = 1111 0111 = 0F7HC = 0

  • Review Find the contents of file register MYREG after execution of the following code:MYREGEQU0x20BCFSTATUS, CMOVLW0x26MOVWFMYREGRRFMYREG, FRRFMYREG, FRRFMYREG, 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:MREGEQU0x20BSFSTATUS, CMOVLW0x15MOVWFMYREGRLFMYREG, FRLFMYREG, FRLFMYREG, FRLFMYREG, 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 WDT0 WDT prescalar1 TO0 PDAffect TO & PD bits of STATUS register

    Instruction: SLEEP

  • SUBLWk Subtract WREG register (2s complement) from literal k and put the result onto WREG registerk (W) (W)Affect C, DC & Z bits of STATUS register

    Instruction: SUBLW 02HW = 01HC = 1Z = 0

    BeforeAfterW = 01HC = ?Z = ?

  • SUBWF f, d Subtract WREG register (2s complement) from f register (f) (W) (d)Affect C, DC & Z bits of STATUS register

    Instruction: SUBWF 02H, 0W = 04HF = 05HC = 1Z = 0

    BeforeAfterW = 01HF = 05HC = ?Z = ?

  • Review Show the steps involved in the following.MOVLW0x23SUBLW0x3F K= 3F0011 1111- WREG= 230010 0011 1C 0011 1111+ 1101 1101 (2s complement)1 0001 1100 C = 1, Z = 0 (result is +ve)

  • SWAPF f, d Exchange the upper & lower nibbles of f register(f
  • Review Find the contents of the MYREG register in the following code.MYREGEQU0x20MOVLW0x72MOVWFMYREGSWAPFMYREG, 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 registerAffect bit Z of STATUS register

    Instruction: XORLW 0AFHW = 01AH

    BeforeAfterW = 0B5H

  • 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, 1REG = 01AHW = 0B5H

    BeforeAfterREG = 0AFHW = 0B5H

  • Review Show the results of the following:MOVLW0x54XORLW0x7854H = 0 1 0 1 0 1 0 078H = 0 1 1 1 1 0 0 02CH = 0 0 1 0 1 1 0 054H 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 addressCOUNTEQU0x25.MOVLWCOUNT

  • Assembler Directives Using EQU for fixed data assignment:;in hexadecimalDATA1EQU39DATA2EQU0x39DATA3EQU39HDATA4EQUH39DATA5EQUh39

    ;in binaryDATA6EQUB00110101DATA7EQUb00110101

    ;in decimalDATA8EQUD28DATA9EQUd28

    ;in ASCIIDATA10EQUA2DATA11EQUa2DATA12EQU2

  • Assembler Directives Using EQU for SFR address assignment:COUNTEREQU0x00PORTBEQU0x06MOVLWCOUNTERMOVWFPORTBINCFPORTB, FINCFPORTB, FINCFPORTB, F

  • Assembler Directives Using EQU for RAM address assignment:MYREGEQU0x12MOVLW0MOVWFMYREGMOVLW22HADDWFMYREG, FADDWFMYREG, FADDWFMYREG, F

  • Assembler Directives SET directive:Define a constant value or a fixed addressIdentical 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) filecont

  • Assembler Directives LIST directive:Unique to PIC assemblerIndicate 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 deviceIncorrect use may cause the chip unusable CONFIGOSC=HSCONFIGWDT=OFF

    radix directive:Indicate numbering system whether it is hexadecimal or decimal RADIXDECcont

    ****************************************************************