Home ยป PIC Programming ยป Common Instructions Types in PIC Microcontrollers

Common Instructions Types in PIC Microcontrollers

Every microcontroller family has its own set of instructions that are typically carried out in a similar set of operations though using different syntax. This is exhibited in the variation in the internal architecture of various types of microcontrollers. Processors such as those fabricated by Intel have a more extensive instruction set, with more options within each instruction; however, this tends to affect the speed of instruction execution. On the other hand, the PIC microcontrollers utilizes a minimal set of uniform instructions which makes them a good choice for microcontroller hobbyists, learners, as well as providing the much needed speed advantage over other types of microcontrollers in high performance systems.

We can classify PIC instructions into various types as per the operation they perform (Note, this is just a general overview of the instruction types; for specific instruction set for a given PIC microcontroller you are working with, you need to refer to the official documentation for that specific device).

Move Instructions

With the Move instruction type, the contents of a register are copied to another. To move a byte from working (W) register also called accumulator to file register, you will use the following instruction in PIC16F84:

MOVWF f  ;Move W to f file register address

It is important to note, that we cannot move a byte directly from one file register to another in the mid-range PIC microcontrollers. The byte has to go through the working register (W). Therefore, to put a data byte (a literal) into a register, we will use the following instruction to move into the working register first:

MOVLW k  ;Move literal (k) to W

Then we can use the next instruction to move (copy contents of W) to a file register (f):

MOVWF f  ;Move (copy the contents of) W to f file register address

Other instructions include:

MOVF f,w  ;Move F to W, from file register address f, to W register

Register Instructions

Typical register instructions that you may find for example in PIC16F84 include:

CLRW  ;Clear W, sets all the bits to zero (00h)

CLRF f  ;Clear f, f is a file register address

DECF f,d ;Decrement f, d is result destination (0:W register,1: file reg)

INCF f,d ;Increment f, d is result destination

COMF f,d  ;Complement f, d is the result destination

BCF f,b  ;Bit clear f, b is bit number (0 to 7)

BSF, f,b  ;Bit set f, b bit number (0,7)

RLF f,d  ;Rotate f left 1 bit, d is result destination

RRF f,d  ;Rotate f right 1 bit

Arithmetic Instructions

Arithmetic operations (add and subtract) are applied to pairs of 8-bit binary numbers. One must be placed in the working register (W), and the other in a file register. Examples of arithmetic instructions include:

ADDLW k  ;Add literal to W register

ADDWF f,d ;Add W contents to f file register, d result destination

ADDWF f,f ;Add contents of W to f file register, updating contents of f

ADDWF f,1 ;Add contents of W updating f (same above instruction)

SUBWF f,d  ;Subtract W from f, d result destination

SUBLW k  ;Subtract W from literal

Additionally, we may use instructions that clearly specify RAM access operand or the common memory prefix for example in PIC18 microcontrollers:

ADDWF f,f,b   ; add w register contents to f reg in banked memory

Logic Instructions

Logic operations are carried out on the corresponding bit pairs within the two binary numbers in the instruction, giving the result that would be gotten if they were fed to the relevant logic gate. Examples of logic instructions include:

ANDLW k ;AND literal with W

ANDWF f,d  ;AND W with f, d is the result destination

XORLW k  ;Exclusive OR literal with W

XORWF f,d  ;Exclusive OR W with f, d result destination

IORLW k  ;Inclusive OR literal with W

IORWF f,d  ;Inclusive OR W with f

Test, Skip and Jump Instructions

A means is required to make conditional program branches which depend on some input condition or the result of a calculation. For example, programmed jumps are initiated using a bit test and conditional skip, followed by a GOTO or CALL. The bit test can be made on any file register bit. This could be a port bit, to check if an input has changed, or a status bit in a control register. This is illustrated in examples below:

BTFSS f,b ;Bit test f, skip next instruction if set, b bit number(0 to 7)

BTFSC, f,b ;Bit test f, skip next instruction if clear, b bit number

The BTFSC (Bit Test and Skip if Clear) and BTSS (Bit Test and Skip if Set) are used to test the bit and skip the next instruction, or not as per the state of the bit tested.

DECFSZ and INCFSZ are used to execute most of the common type of branch operation โ€“ decrement or increment a register and jump depending on the effect of the result on the zero flag (Z is set if the result in the destination register is zero).

Example of DECFSZ instruction:

DECFSZ f,d  ;Decrement f, skip next instruction if 0

As aforementioned, the bit test and skip may be followed by a single instruction to be carried out conditionally such a GOTO or CALL instruction.

Examples:

GOTO k  ;Go to k, k can be  address or label 

CALL k  ;Call subroutine,  k may be a label

RETURN  ;Return from subroutine

Control Instructions

Control instructions NOP, SLEEP, CLRWDT are used to perform specific tasks in a microcontroller. For example NOP is used to allow an ICD operation to be inserted when the program is downloaded or to pad a timing loop so that it is exactly 1 ms.

SLEEP stops the program and forces the microcontroller to wait for a reset or other interrupt. It is typically used at the end of any program that does not loop back continuously, to prevent the program execution continuing into unused locations.  If the program isnโ€™t stopped, it will run through program memory and restart from the first instruction when the program counter rolls over to 0000.

A control instruction CLRWDT means clear the WDT (watchdog timer). For instance, if the program is stuck in a loop or stops for one reason or other, it will be restarted automatically by the watchdog timer. To stop this from occurring when the program is operating normally, watchdog timer must be reset at regular intervals using CLRWDT. The WDT will wake up the microcontroller from SLEEP mode and thus can be employed to implement intermittent power saving operation.

Examples of control instructions include:

NOP   ; No operation

CLRWDT     ; clear watch-dog timer

SLEEP      ; Go into standby mode

Interrupt Flow Control Instructions

For example:

RETFIE  ;Return from interrupt

Related articles:

Please follow us and share:

Comments

Leave a Reply

Currently trending: