Chapter 2 BEE 3233

download Chapter 2 BEE 3233

of 50

Transcript of Chapter 2 BEE 3233

  • 8/6/2019 Chapter 2 BEE 3233

    1/50

    Chapt er 2Week 2

    Microcontroller PIC16F84Instructions Set

  • 8/6/2019 Chapter 2 BEE 3233

    2/50

    Chessda

    Chapt er 2 Out l ine

    PIC16F84 Instructions Set Overview

    Instruction MOVE Register Operations

    Arithmetic Operations

    Logic Operations

    Test and Skip

    Jump Operations

    Control Operations

  • 8/6/2019 Chapter 2 BEE 3233

    3/50

    Chessda

    PIC16F84 Inst ruc t ions

    Set Overview

    PIC16F84 INSTRUCTION SET BYFUNCTIONAL GROUPS

    F = Any file register (specified by numberor label)

    W = Working register, W

    L = Literal value (follows instruction)

  • 8/6/2019 Chapter 2 BEE 3233

    4/50

    Chessda

    PIC16F84 Inst ruc t ions

    Set Overview (Cont..)

    The PIC16F84 instruction is a 14-bit word

    OPCODE: specifies the instruction type

    Operand (One or more): specify the operation of theinstruction

    The instruction set is highly orthogonal and isgrouped into three basic categories:

    Byte-oriented operations Bit-oriented operations

    Literal and control operations

  • 8/6/2019 Chapter 2 BEE 3233

    5/50

    Chessda

    PIC16F84 Inst ruc t ions

    Set Overview (Cont..)

    Table 2:1: Opcode Field Descriptions

  • 8/6/2019 Chapter 2 BEE 3233

    6/50

    Chessda

    PIC16F84 Inst ruc t ions

    Set Overview (Cont..)

    Table 2.2: General format for PIC instruction

  • 8/6/2019 Chapter 2 BEE 3233

    7/50

  • 8/6/2019 Chapter 2 BEE 3233

    8/50

    Chessda

    Inst ruc t ion Move

    File register

    Working register, W

    Move data from F to W

    Example,MOVF 0D,W

    8 A1 2

    5 F

    0Ch

    0Dh

    0Eh

    F F

    MOVE 0D,W

  • 8/6/2019 Chapter 2 BEE 3233

    9/50

    Chessda

    Inst ruc t ion Move

    (Cont..)

    Working register, W

    Move data from F to W

    Example,MOVF 0D,W

    8 A1 2

    5 F

    0Ch

    0Dh

    0Eh

    8 A

    MOVE 0D,W

    File register

  • 8/6/2019 Chapter 2 BEE 3233

    10/50

    Chessda

    Inst ruc t ion Move

    (Cont..)

    Working register, W

    Move data from W to F

    Example,MOVWF 0E

    8 A1 2

    F F

    0Ch

    0Dh

    0Eh

    3 4

    MOVWF 0E

    File register

  • 8/6/2019 Chapter 2 BEE 3233

    11/50

    Chessda

    Inst ruc t ion Move

    (Cont..)

    Working register, W

    Move data from W to F

    Example,MOVWF 0E

    8 A1 2

    3 4

    0Ch

    0Dh

    0Eh

    3 4

    MOVWF 0E

    File register

  • 8/6/2019 Chapter 2 BEE 3233

    12/50

    Chessda

    Inst ruc t ion Move

    (Cont..)

    Move literal into W

    Example, MOVLW 12

    F FWorking register, W

    1 2Working register, Wafter instructionMOVLW 12

  • 8/6/2019 Chapter 2 BEE 3233

    13/50

    Chessda

    Regist er Operat ions

    Clear W (reset all bits and value to 0)

    Example,CLRW

    3 4Working register, W

    0 0Working register, Wafter instruction CLRW

  • 8/6/2019 Chapter 2 BEE 3233

    14/50

    Chessda

    Regis t er Operat ions

    (Cont..)

    Clear F (reset all bits and value to 0)

    Example,CLRF 0D

    8 A1 2

    3 4

    0Ch

    0Dh

    0Eh

    File register

    0 01 2

    3 4

    0Ch

    0Dh

    0Eh

    File register after

    Instruction CLRF 0D

  • 8/6/2019 Chapter 2 BEE 3233

    15/50

    Chessda

    Regis t er Operat ions

    (Cont..)

    Decrement F (Reduce by 1)

    Example,DECF 0D

    8 A1 2

    3 4

    0Ch

    0Dh

    0Eh

    File register

    8 91 2

    3 4

    0Ch

    0Dh

    0Eh

    File register after

    Instruction DECF 0D

  • 8/6/2019 Chapter 2 BEE 3233

    16/50

    Chessda

    Regis t er Operat ions

    (Cont..)

    Increment F (Increase by 1)

    Example,INCF 0D

    8 A1 2

    3 4

    0Ch

    0Dh

    0Eh

    File register

    8 B1 2

    3 4

    0Ch

    0Dh

    0Eh

    File register after

    Instruction INCF 0D

  • 8/6/2019 Chapter 2 BEE 3233

    17/50

    Chessda

    Regis t er Operat ions

    (Cont..)

    Swap the upper and lower four bits in F

    Example,SWAPF 0D

    8 A1 2

    3 4

    0Ch

    0Dh

    0Eh

    File register

    A 81 2

    3 4

    0Ch

    0Dh

    0Eh

    File register after

    Instruction SWAPF 0D

  • 8/6/2019 Chapter 2 BEE 3233

    18/50

    Chessda

    Regis t er Operat ions

    (Cont..)

    Complement F value (invert all bits)

    Example,COMF 0C

    8 A1 2

    3 4

    0Ch

    0Dh

    0Eh

    File register

    8 AE D

    3 4

    0Ch

    0Dh

    0Eh

    File register after

    Instruction COMF OC

  • 8/6/2019 Chapter 2 BEE 3233

    19/50

    Chessda

    Regis t er Operat ions

    (Cont..)

    Rotate bits left through Carry flag

    Example,RLF 0D

    8 A1 2

    3 4

    0Ch

    0Dh

    0Eh

    File register

    1 41 2

    3 4

    0Ch

    0Dh

    0Eh

    File register after

    Instruction RLF 0D

    F registerC

    C flag is

    set to 1

    Assume C = 0

  • 8/6/2019 Chapter 2 BEE 3233

    20/50

    Chessda

    Regis t er Operat ions

    (Cont..)

    Rotate bits right through Carry flag

    Example,RRF 0D

    8 A1 2

    3 4

    0Ch

    0Dh

    0Eh

    File register

    4 51 2

    3 4

    0Ch

    0Dh

    0Eh

    File register after

    Instruction RRF 0D

    F registerC

    C flag is

    clear to 0

    Assume C = 0

  • 8/6/2019 Chapter 2 BEE 3233

    21/50

    Chessda

    Regis t er Operat ions

    (Cont..)

    Clear (reset to zero) the bit specified

    Example,BCF 0D,03

    8 A1 2

    3 4

    0Ch

    0Dh

    0Eh

    File register

    8 21 2

    3 4

    0Ch

    0Dh

    0Eh

    File register after

    Instruction BCF 0D,03

  • 8/6/2019 Chapter 2 BEE 3233

    22/50

    Chessda

    Regis t er Operat ions

    (Cont..)

    Set (to 1) the bit specified

    Example,BSF 0E,03

    8 A1 2

    3 4

    0Ch

    0Dh

    0Eh

    File register

    8 A1 2

    3 C

    0Ch

    0Dh

    0Eh

    File register after

    Instruction BSF 0E,03

  • 8/6/2019 Chapter 2 BEE 3233

    23/50

    Chessda

    Ari t hm et i c Operat ions

    File register

    Working register, W

    Add F to W

    Example,ADDWF 0C,W

    8 A1 2

    5 F

    0Ch

    0Dh

    0Eh

    3 4

  • 8/6/2019 Chapter 2 BEE 3233

    24/50

    Chessda

    Ari t hm et i c Operat ions

    File register

    Working register, WafterADDWF 0C,W

    Add F to W

    Example,ADDWF 0C,W

    8 A1 2

    5 F

    0Ch

    0Dh

    0Eh

    4 6

  • 8/6/2019 Chapter 2 BEE 3233

    25/50

    Chessda

    Ari t hm et i c Operat ions

    File register

    Working register, W

    Add W to F

    Example,ADDWF 0C

    8 A1 2

    5 F

    0Ch

    0Dh

    0Eh

    3 4

  • 8/6/2019 Chapter 2 BEE 3233

    26/50

    Chessda

    Ari t hm et i c Operat ions

    File registerafterADDWF 0C

    Working register, W

    Add W to F

    Example, ADDWF 0C

    8 A4 6

    5 F

    0Ch

    0Dh

    0Eh

    3 4

  • 8/6/2019 Chapter 2 BEE 3233

    27/50

    Chessda

    Ari t hm et i c Operat ions

    Add L to W

    Example, ADDLW 15

    1 2Working register, W

    2 7Working register, Wafter instructionADDLW 15

  • 8/6/2019 Chapter 2 BEE 3233

    28/50

    Chessda

    Ari t hm et i c Operat ions

    File register

    Working register, W

    Subtract W from F

    Example, SUBWF 0D

    8 A1 2

    5 F

    0Ch

    0Dh

    0Eh

    3 4

  • 8/6/2019 Chapter 2 BEE 3233

    29/50

    Chessda

    Ari t hm et i c Operat ions

    File registerafter SUBWF 0D

    Working register, W

    Subtract W from F

    Example, SUBWF 0D

    5 61 2

    5 F

    0Ch

    0Dh

    0Eh

    3 4

  • 8/6/2019 Chapter 2 BEE 3233

    30/50

    Chessda

    Ari t hm et i c Operat ions

    File register

    Working register, W

    Subtract W from F, placing result in W

    Example, SUBWF 0D,W

    8 A1 2

    5 F

    0Ch

    0Dh

    0Eh

    3 4

  • 8/6/2019 Chapter 2 BEE 3233

    31/50

    Chessda

    Ari t hm et i c Operat ions

    File register

    Working register, Wafter SUBWF 0D,W

    Subtract W from F, placing result in W

    Example, SUBWF 0D,W

    8 A1 2

    5 F

    0Ch

    0Dh

    0Eh

    5 6

    If result 0 , C = 1If result < 0 , C = 0

  • 8/6/2019 Chapter 2 BEE 3233

    32/50

    Chessda

    Ari t hm et i c Operat ions

    Subtract W from L, placing result in W

    Example, SUBLW 13

    1 2Working register, W

    0 1Working register, Wafter instruction SUBLW 13

    If result 0 , C = 1If result < 0 , C = 0

  • 8/6/2019 Chapter 2 BEE 3233

    33/50

    Chessda

    Logic Operat ions

    File register

    Working register, W

    AND the bits of W and F, result in F

    Example, ANDWF 0C

    8 A1 2

    5 F

    0Ch

    0Dh

    0Eh

    3 4

  • 8/6/2019 Chapter 2 BEE 3233

    34/50

    Chessda

    Logic Operat ions

    File register

    afterANDWF 0C

    Working register, W

    AND the bits of W and F, result in F

    Example, ANDWF 0C

    8 A1 0

    5 F

    0Ch

    0Dh

    0Eh

    3 4

  • 8/6/2019 Chapter 2 BEE 3233

    35/50

    Chessda

    Logic Operat ions

    File register

    Working register, W

    AND the bits of W and F, result in WExample,

    ANDWF 0C,W

    8 A1 2

    5 F

    0Ch

    0Dh

    0Eh

    3 4

  • 8/6/2019 Chapter 2 BEE 3233

    36/50

    Chessda

    Logic Operat ions

    File register

    Working register, WafterANDWF 0C,W

    AND the bits of W and F, result in WExample,

    ANDWF 0C,W

    8 A1 2

    5 F

    0Ch

    0Dh

    0Eh

    1 0

  • 8/6/2019 Chapter 2 BEE 3233

    37/50

    Chessda

    Logic Operat ions

    AND the bits of L and W, result in WExample,

    ANDLW AA

    B CWorking register, W

    A 8Working register, Wafter instructionANDLW AA

  • 8/6/2019 Chapter 2 BEE 3233

    38/50

    Chessda

    Logic Operat ions

    File register

    Working register, W

    OR the bits of W and F, result in FExample,

    IORWF 0C

    8 A1 2

    5 F

    0Ch

    0Dh

    0Eh

    3 4

  • 8/6/2019 Chapter 2 BEE 3233

    39/50

    Chessda

    Logic Operat ions

    File register

    after IORWF 0C

    Working register, W

    OR the bits of W and F, result in FExample,

    IORWF 0C

    1 2

    5 F

    0Ch

    0Dh

    0Eh

    3 48 A3 6

    5 F

    0Ch

    0Dh

    0Eh

  • 8/6/2019 Chapter 2 BEE 3233

    40/50

    Chessda

    Logic Operat ions

    File register

    Working register, W

    OR the bits of W and F, result in WExample,

    IORWF 0C,W

    8 A1 2

    5 F

    0Ch

    0Dh

    0Eh

    3 4

  • 8/6/2019 Chapter 2 BEE 3233

    41/50

    Chessda

    Logic Operat ions

    File register

    Working register, Wafter IORWF 0C,W

    OR the bits of W and F, result in WExample,

    IORWF 0C,W

    3 68 A1 2

    5 F

    0Ch

    0Dh

    0Eh

  • 8/6/2019 Chapter 2 BEE 3233

    42/50

    Chessda

    Logic Operat ions

    OR the bits of L and W, result in WExample,

    IORLW AB

    B CWorking register, W

    B FWorking register, Wafter IORLW AB

  • 8/6/2019 Chapter 2 BEE 3233

    43/50

    Chessda

    Logic Operat ions

    File register

    Working register, W

    Exclusive OR the bits of W and F, result in FExample,

    XORWF 0C

    3 68 A1 2

    5 F

    0Ch

    0Dh

    0Eh

    O

  • 8/6/2019 Chapter 2 BEE 3233

    44/50

    Chessda

    Logic Operat ions

    File register

    after XORWF 0C

    Working register, W

    Exclusive OR the bits of W and F, result in FExample,

    XORWF 0C

    3 62 4

    5 F

    0Ch

    0Dh

    0Eh

    8 A

  • 8/6/2019 Chapter 2 BEE 3233

    45/50

    L i O t i

  • 8/6/2019 Chapter 2 BEE 3233

    46/50

    Chessda

    Logic Operat ions

    File register

    Working register, Wafter XORWF 0C,W

    Exclusive OR the bits of W and F, result in WExample,

    XORWF 0C,W

    2 48 A1 2

    5 F

    0Ch

    0Dh

    0Eh

    L i O t i

  • 8/6/2019 Chapter 2 BEE 3233

    47/50

    Chessda

    Logic Operat ions

    Exclusive OR the bits of L and W, result in WExample,

    XORLW AB

    B CWorking register, W

    1 7Working register, Wafter EORLW AB

    T t d Sk i

  • 8/6/2019 Chapter 2 BEE 3233

    48/50

    Chessda

    Test and Sk ip

    Test a bit in F and Skip next instruction if it is Clear (= 0)

    BTFSC 0C,3

    Test a bit in F and Skip next instruction if it is Set (= 1)BTFSS 0C,3

    Decrement F and Skip next instruction if it is now ZeroDECFSZ 0C

    Increment F and Skip next instruction if it is now Zero

    INCFSZ 0C

    J O t i

  • 8/6/2019 Chapter 2 BEE 3233

    49/50

    Chessda

    J um p Opera t ions

    Go to a Labeled Line in the Program GOTO LOOP1

    Jump to the Label at the start of a Subroutine CALL DELAY

    Return at the end of a Subroutine to the next RETURN

    instruction

    Return at the end of a Subroutine with L in W RETLW F9

    Return from Interrupt Service Routine to the RETFIE

    next instruction

    Cont ro l Operat ions

  • 8/6/2019 Chapter 2 BEE 3233

    50/50

    Chessda

    Cont ro l Operat ions

    No Operation delay for 1 cycle NOP

    Go into Standby Mode to save power SLEEP Clear Watchdog Timer to prevent automatic reset CLRWDT

    Load Port Data Direction Register from W TRIS 06

    Load Option Control Register from WOPTION