OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
Instructions
1. Instructions
There are only 35 instruction sets for PIC Microcontroller which makes the use of PIC as
compared to other microcontroller more advantageous. These 35 instruction sets are
discussed below along with their descriptions. These 35 instructions can also be used in some
combination
Before starting with the instruction set, let us know some of the used letters.
F: Any file register (specified by address or label)
L: Literal value (follows instruction)
W: Working Register
Labels:
o Register labels must be declared in include file or by register label equate (e.g.
GPR1 EQU 0C)
o Bit labels must be declared in include file or by bit label equate (e.g. bit1 EQU 3)
o Address labels must be placed at the left margin of the source code file (e.g. start,
delay)
The instruction is subdivided into seven different instruction types. These types include:
MOVE
The contents of a register are copied to another. Notice that we cannot move a byte directly
from one file register to another, it has to go via the working register. To put data into the
system from the program (a literal) we must use MOVLW to place the literal into W initially.
It can then be moved to another register as required. The syntax is not symmetrical; to move a
byte from W to a file register, MOVWF is used. To move it the other way, MOVF F,W is
used, where F is any file register address. This means that MOVF F,F is also available. This
may seem pointless, but in fact can be used to test a register without changing it.
REGISTER
Register operations affect only a single register, and all except CLRW (clear W) operate on
file registers. Clear sets all bits to zero (00h), decrement decreases the value by 1 and
increment increases it by 1. Swap exchanges the upper and lower four bits (nibbles).
Complement inverts all the bits, which in effect negates the number. Rotate moves all bits left
or right, including the carry flag in this process (see below for flags). Clear and set a bit
operate on a selected bit, where the register and bit need to be specified in the instruction.
ARITHMETIC & LOGIC
Addition and subtraction in binary gives the same result as in decimal or hex. If the result
generates an extra bit (e.g. FF FF 1FE), or requires a borrow (e.g. 1FE–FF FF), the carry
flag is used. Logic operations are carried out on bit pairs in two numbers to give the result
which would be obtained if they were fed to the corresponding logic gate (e.g. 00001111 and
01010101 00000101). If necessary, reference should be made to an introductory text for
2. further details of arithmetic and logical operations, and conversion between number systems.
Some examples will be discussed later.
TEST, SKIP & JUMP
A mechanism is needed to make decisions (conditional program branches) which depend on
some input condition or the result of a calculation. 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. BTFSC (Bit Test and Skip if Clear) and BTFSS (Bit Test and Skip if Set) are
used to test the bit and skip the next instruction, or not, according to the state of the bit tested.
DECFSZ and INCFSZ embody a commonly used test – decrement or increment a register
and jump depending on the effect of the result on the zero flag (Z is set if result 0).
Decrement is probably used more often (see BIN4 delay routine), but increment also works
because when a register is incremented from the maximum value (FFh) it goes to zero (00h).
The bit test and skip may be followed by a single instruction to be carried out conditionally,
but GOTO and CALL allow a block of conditional code. Using GOTO label simply transfers
the program execution point to some other point in the program indicated by a label in the
first column of the source code line, but CALL label means that the program returns to the
instruction following the CALL when RETURN is encountered at the end of the subroutine.
Another option, which is useful for making program data tables, is RETLW (Return with
Literal in W). See the KEYPAD program later for an example of this. RETFIE (Return From
Interrupt) is explained below.
CONTROL
NOP simply does nothing for one instruction cycle (four clock cycles). This may seem
pointless, but is in fact very useful for putting short delays in the program so that, for
example, external hardware can be synchronised or a delay loop adjusted for an exact time
interval. In the LCD driver program (Chapter 4), NOP is used to allow in-circuit debugging
to be incorporated later when the program is downloaded, and to pad a timing loop so that it
is exactly 1 ms. SLEEP stops the program, such that it can be restarted with an external
interrupt. It should also be used at the end of any program that does not loop back
continuously, to prevent the program execution continuing into unused locations. The unused
locations contain the code 3FFF (all 1 s), which is a valid instruction (ADDLW FF). If the
program is not stopped, it will run through, repeating this instruction, and start again when
the program counter rolls over to 0000. CLRWDT means clear the watchdog timer. If the
program gets stuck in a loop or stops for any other reason, it will be restarted automatically
by the watchdog timer. To stop this happening when the program is operating normally, the
watchdog timer must be reset at regular intervals of less than, say, 10 ms, within the program
loop, using CLRWDT.
3. Instruction Types Description Example
Move
Move data from F to W MOVF GPR1,W
Move data from W to F MOVWF GPR1
Move literal into W MOVLW num1
Test the register data MOVF GPR1,F
Register
Clear W (reset all bits and value to 0) CLRW
Clear F (reset all bits and value to 0) CLRF GPR1
Decrement F (reduce by 1) DECF GPR1
Increment F (increase by 1) INCF GPR1
Swap the upper and lower four bits in F SWAPF GPR1
Complement F value (invert all bits) COMF GPR1
Rotate bits Left through carry flag RLF GPR1
Rotate bits Right through carry flag RRF GPR1
Clear ( =0) the bit specified BCF GPR1,but1
Set ( =1) the bit specified BSF GPR1,but1
Arithmetic
Add W to F, with carry out ADDWF GPR1
Add F to W, with carry out ADDWF GPR1,W
Add L to W, with carry out ADDLW num1
Subtract W from F, using borrow SUBWF GPR1
Subtract W from F, placing result in W SUBWF GPR1,W
Subtract W from L, placing result in W SUBLW num1
Logic
AND the bits of W and F, result in F ANDWF GPR1
AND the bits of W and F, result in W ANDWF GPR1,W
AND the bits of L and W, result in W ANDLW num1
OR the bits of W and F, result in F IORWF GPR1
OR the bits of W and F, result in W IORWF GPR1,W
OR the bits of L and W, result in W IORLW num1
Exclusive OR the bits of W and F, result in F XORWF GPR1
Exclusive OR the bits of W and F, result in W XORWF GPR1,W
Exclusive OR the bits of L and W XORLW num1
Test & Skip
Test a bit in F and Skip next instruction if it is
Clear (=0)
BTFSC GPR1,but1
Test a bit in F and Skip next instruction if it is
Set ( =1)
BTFSS GPR1,but1
Decrement F and Skip next instruction if F=0 DECFSZ GPR1
Increment F and Skip next instruction if F=0 INCFSZ GPR1
Jump
Go to a labelled line in the program GOTO start
Jump to the label at the start of a subroutine CALL delay
Return at the end of a subroutine to the next
instruction
RETURN
Return at the end of a subroutine with L in W RETLW num1
Return From Interrupt service routine RETFIE
Control
No Operation - delay for 1 cycle NOP
Go into standby mode to save power SLEEP
Clear watchdog timer to prevent automatic reset CLRWDT
References
1. Interfacing PIC Microcontroller Embedded Design by interactive simulation by martin bates