Module 3 : Introduction to PIC Microcontrollers

Lecture 18 : Instruction  Set of PIC Microcontroller

Guidelines from Microchip Technology

For writing assembly language program Microchip Technology has suggested the following guidelines.
  1. Write instruction mnemonics in lower case. (e.g., movwf)
  2. Write the special register names, RAM variable names and bit names in upper case. (e.g., PCL, RP0, etc.)
  3. Write instructions and subroutine labels in mixed case. (e.g., Mainline, LoopTime)
Instruction Set:

The instruction set for PIC16C74A consists of only 35 instructions. Some of these instructions are byte oriented instructions and some are bit oriented instructions.

The byte oriented instructions that require two parameters (For example, movf f, F(W)) expect the f to be replaced by the name of a special purpose register (e.g., PORTA) or the name of a RAM variable (e.g., NUM1), which serves as the source of the operand. 'f' stands for file register. The F(W) parameter is the destination of the result of the operation. It should be replaced by:

F, if the destination is to be the source register.
W, if the destination is to be the working register (i.e., Accumulator or W register).

The bit oriented instructions also expect parameters (e.g., btfsc f, b). Here 'f' is to be replaced by the name of a special purpose register or the name of a RAM variable. The 'b' parameter is to be replaced by a bit number ranging from 0 to 7.

For example:

Z equ 2
btfsc STATUS, Z

Z has been equated to 2. Here, the instruction will test the Z bit of the STATUS register and will skip the next instruction if Z bit is clear.

The literal instructions require an operand having a known value (e.g., 0AH) or a label that represents a known value.

For example:

NUM equ 0AH ;                      Assigns 0AH to the label NUM ( a constant )
movlw NUM ;                          will move 0AH to the W register.

Every instruction fits in a single 14-bit word. In addition, every instruction also executes in a single cycle, unless it changes the content of the Program Counter. These features are due to the fact that PIC micro controller has been designed on the principles of RISC (Reduced Instruction Set Computer) architecture.

Instruction set:

Mnemonics

Description

Instruction Cycles

bcf f, b

Clear bit b of register f

1

bsf f, b

Set bit b of register f

1

clrw

Clear working register W

1

clrf f

Clear f

1

movlw k

Move literal 'k' to W

1

movwf f

Move W to f

1

movf f, F(W)

Move f to F or W

1

swapf f, F(W)

Swap nibbles of f, putting result in F or W

1

andlw k

And literal value into W

1

andwf f, F(W)

And W with F and put the result in W or F

1

andwf f, F(W)

And W with F and put the result in W or F

1

iorlw k

inclusive-OR literal value into W

1

iorwf f, F(W)

inclusive-OR W with f and put the result in F or W

1

xorlw k

Exclusive-OR literal value into W

1

xorwf f, F(W)

Exclusive-OR W with f and put the result in F or W

1

addlw k

Add the literal value to W and store the result in W

1

addwf f, F(W)

Add W to f and store the result in F or W

1

sublw k

Subtract the literal value from W and store the result in W

1

subwf f, F(W)

Subtract f from W and store the result in F or W

1

rlf f, F(W)

Copy f into F or W; rotate F or W left through the carry bit

1

rrf f, F(W)

Copy f into F or W; rotate F or W right through the carry bit

1

btfsc f, b

Test 'b' bit of the register f and skip the next instruction if bit is clear

1 / 2

btfss f, b

Test 'b' bit of the register f and skip the next instruction if bit is set

1 / 2

decfsz f, F(W)

Decrement f and copy the result to F or W; skip the next instruction if the result is zero

1 / 2

incfcz f, F(W)

Increment f and copy the result to F or W; skip the next instruction if the result is zero

1 / 2

goto label

Go to the instruction with the label "label"

2

call label

Go to the subroutine "label", push the Program Counter in the stack

2

retrun

Return from the subroutine, POP the Program Counter from the stack

2

retlw k

Retrun from the subroutine, POP the Program Counter from the stack; put k in W

2

retie

Return from Interrupt Service Routine and re-enable interrupt

2

clrwdt

Clear Watch Dog Timer

1

sleep

Go into sleep/ stand by mode

1

nop

No operation

1

Encoding of instruction:

As has been discussed, each instruction is of 14-bit long. These 14-bits contain both op-code and the operand. Some examples of instruction encoding are shown here.

Example-1:

bcf f, b              Clear 'b' bit of register 'f'

Operands:                         0 ≤ f   ≤ 127
                                            0 ≤ b ≤ 7

Encoding:

The instruction is executed in one instruction cycle, i.e., 4 clock cycles. The activities in various clock cycles are as follows.

Example-2:

goto K              Go to label 'k' instruction

Operand:                        0 ≤ K ≤ 2047 (11-bit address is specified)
Operation:                      K PC <10:0>
                                         PCLATH <4:3> PC <12:11>
Encoding:

Since this instruction requires modification of program Counter, it takes two instruction cycles for execution.

Q-Cycle activities are shown as follows.