[?] Subscribe To This Site

XML RSS
Add to Google
Add to My Yahoo!
Add to My MSN
Subscribe with Bloglines


Home
Forum
Project Ideas
C Course
Projects Showcase
LCD-KEYS OnePort
PIC Introduction
PIC Programmer
Schematic Tool
PIC Projects
Tips & Techniques
PIC Tutorials
Digital Downloads
Store
My SECRET
Oscilloscopes
About Me
Contact
Terms of Use
Search This Site
Freebies
Articles
Books
Resource Links
Site Map
Video du Jour
Rant/Rave
Privacy Policy
Problem?-Solution
Programmer Types

A handy summary of the medium range PIC microcontroller instruction set.

The medium range PIC microcontroller instruction set has 35 instructions. This is both good and bad since it is easy to learn but it is sometimes difficult to see how to achieve a particular task.

Assembly programming is not the easiest method of computer programming and it typically takes ten times as long to create a program as it does with a high level language.

It is presented here as a reference and not an assembler tutorial as the emphasis of this site is on high level language programming. When you need to do some assembler either to achieve faster operation or reduce the size of the code then this reference is useful as a quick guide.

General pic microcontroller instruction set information

One of the most useful features of the instruction set is that all ram storage locations can be treated as registers (or file registers in PIC speak)- unlike older processors such as a Z80 which has only about 6.

Most instructions take one machine cycle which makes figuring out execution times much easier. Exceptions are conditional jumps, calls and returns which take 2 (conditional jumps take 1 or 2 depending on the result of the condition. So the conditional jump is the only tricky one.

Indirection

Indirection is supported through the FSR (File Select Register) and INDF (Indirection) registers. FSR contains the address to operate with while the INDF register indicates that the register file that the FSR address points at should be used instead (See the example below).

Note: INDF is not a 'real' register - it just indicates that the register at the FSR address should be used.

Indirection lets you point at another variable - FSR holds the address of the variable so it is useful for manipulating arrays of registers e.g.:

Indirection example

        MOVLW  33              ; W=33
        MOVWF  FSR            ; set first address
        INCF  INDF,F            ; increment contents of address 33
        INCF  FSR,F             ; point at next address
        INCF  INDF,F            ; increment contents of address 34
        INCF  FSR,F             ; point at next address
        INCF  INDF,F            ; increment contents of address 35
        INCF  FSR,F             ; point at next address

The loop

Program loops are created using the DECFSZ instruction (Decrement File register and skip next instruction if register is zero) or INCFSZ (Increment file register and skip next instruction if register is zero).

To use these instructions a file register must be used to hold the loop count value (labeled VAL below) e.g.:

Loop example

MOVLW 10 ;W = ten - to loop ten times.
MOVWF VAL ;VAL = W = 10 ; VAL file register gets the value 10.
LOOP1: ;The loop label to jump to if VAL is not zero.
... ;Your assembler instructions.
... ;Your assembler instructions.
... ;Your assembler instructions.
DECFSZ VAL,F ;VAL = VAL - 1. IF zero skip next instruction (so no jump).
GOTO LOOP1 ;VAL is not zero so JUMP to LOOP1.
... ;Here when VAL is zero.
... ;Your assembler instructions.
... ;Your assembler instructions.

PIC microcontroller instruction set

Mnemonic

Code Example

Action

Flags

Description

Loading / clear (medium range PIC microcontroller instruction set)

MOVLW k

 MOVLW 33

W = 33

-

 Move literal into W

MOVF f,d

 MOVF X,W

W = X

Z

 Move f (to W or f)

MOVF f,d

 MOVF X,F

X = X

Z

(Use to set Z flag)

MOVWF f

 MOVWF X

X = W

Z

 Move W to f

CLRF f

 CLRF X

X = 0

Z=1

 Clear f

CLRW -

 CLRW

W = 0

Z=1

 Clear W

Maths / inc / dec (medium range PIC microcontroller instruction set)

ADDWF f,d

ADDWF X,F

X = X ADD W

C,DC,Z

Add W and f

ADDLW k

ADDLW 2

W = W+2

C,DC,Z

Add Literal and W

SUBWF f,d

SUBWF X,F

X = X-W

C,DC,Z

Subtract W from f

SUBLW k

SUBLW 2

W = 2-W (order) .

C,DC,Z

Subtract W from Literal

INCF f,d

INCF X,W

W = X+1

Z

Increment f

DECF f,d

DECF X,F

X = X-1

Z

Decrement f

INCFSZ f,d

INCFSZ X,F

X = X+1, test Z

-

Increment f, Skip if 0

DECFSZ f,d

DEFCZ X,W

W = X-1, test Z

-

Decrement f, Skip if 0

Logical (medium range PIC microcontroller instruction set)

ANDWF f,d

ANDWF X,F

X = X & W

Z

AND W with f

ANDLW k

ANDLW 5

W = W & 5

Z

AND Literal with W

IORWF f,d

IORWF X,W

W = W | X

Z

Inclusive OR W with f

IORLW k

IORLW 33

W = W | 33

Z

Inclusive OR Literal with W

XORWF f,d

XORWF X,W

W = W ^ X

Z

Exclusive OR W with f

XORLW k

XORLW 45

W = W ^ 45

Z

Exclusive OR Literal with W

COMF f,d

COMF X,W

W = ~X

Z

Complement f

RRF f,d

RRF X,W

W = X >> 1

C

Rotate right through carry

RLF f,d

RLF X,F

X = X << 1

C

Rotate left through carry

Bit test/set/clear/conditional jump (medium range PIC microcontroller instruction set)

BCF f,b

BCF X,0

X &= 0x01

-

Bit Clear File

BSF f,b

BSF X,2

X |= 0x04

-

Bit Set File

BTFSS f,b

BTFSS X,2

Skip next if X=1

-

Bit Test file Skip If Set

BTFSC f,b

BTFSC X,0

Skip next if X=0

-

Bit Test file Skip If Clear

Call / return / goto (medium range PIC microcontroller instruction set)

GOTO k

GOTO A_LABEL

Jump to A_LABEL

-

Go to Address

CALL k

CALL A_SUB

Call A_SUB

-

Call Subroutine

RETURN -

RETURN

-

-

Return from Subroutine

RETLW k

RETURN 65

Returns 65

-

Return with Literal in W

RETFIE -

RETFIE

-

GIE Set

Return from interrupt (and enable) interrupts

Extra (medium range PIC microcontroller instruction set)

SWAP f,d

SWAP temp,F

-

-

Swap nibbles in f

NOP -

NOP

-

-

No operation

CLRWDT -

CLRWDT

-

-

CLeaR WatchDog Timer

SLEEP -

SLEEP

-

-

Enter Sleep mode

X - is any valid file register address.

f - a file register
k - a literal (number).
b - a bit number 0-7.
d - destination : F - to file reg, W - to W register.

Note: You can find more details on each instruction in the datasheets for each chip.

You can find projects using the pic microcontroller instruction set here.


Best-microcontroller-projects home page