<< The Fribotte Home page >>

[Home] [Who are we?] [Robot] [Tech DB] [e=m6 cup] [Forum] [Reports] [Links]

Introduction
This page on PIC 16F84 programming should help you to program your PIC more easily. It takes the most frequent combinations and translates them into one or more instructions for the PIC.
To do so, I use annotations inspired of more advanced languages :
  • W : the PIC accumulator.
  • a : a constant.
  • M,M1,M2 : addresses in RAM.

Caution : For some functions, I modify the value of the accumulator W.

If some points are not clear, if you miss some combinations or worse, if you find mistakes despite all we checked, please let us know by email, or in our forum.

 

The Pic 16F84
Assignment
W=0   CLRW  
M=0   CLRF M
W=a   MOVLW a
M=a W=a
M=W
MOVLW a
MOVWF M
M=W   MOVWF M
W=M   MOVF M,W
M1=M2 W=M2
M1=W
MOVF M2,W
MOVWF M1
     
Addition
W=W+1   ADDLW 1  
M=W+1 W=W+1
M=W
ADDLW 1
MOVWF M
W=M+1   INCF M,W
M=M+1   INCF M,F
W=W+a   ADDLW a
M=W+a W=W+a
M=W
ADDLW a
MOVWF M
W=M+a W=a
W=W+M
MOVLW a
ADDWF M,W
M=M+a W=a
M=W+M
MOVLW a
ADDWF M,F
W=W+M   ADDWF M,W
M=W+M   ADDWF M,F
W=M1+M2 W=M2
W=W+M1
MOVF M2,W
ADDWF M1,W
M1=M1+M2 W=M2
M1=W+M1
MOVF M2,W
ADDWF M1,F
     
Subtraction
W=-W   SUBLW 0  
M=-W W=-W
M=W
SUBLW 0
MOVWF M
W=-M W=M
W=-W
MOVF M,W
SUBLW 0
M1=-M2 W=M2
W=-W
M1=W
MOVF M2,W
SUBLW 0
MOVWF M1
W=W-1 W=W+ (-1) ADDLW 0xFF
M=W-1 W=W+ (-1)
M=W
ADDLW 0xFF
MOVWF M
W=M-1   DECF M,W
M=M-1   DECF M,F
W=W-a W=W+ (-a) ADDLW -a
M=W-a W=W+ (-a)
M=W
ADDLW -a
MOVWF M
W=a-W   SUBLW a
M=a-W W=a-W
M=W
SUBLW a
MOVWF M
W=M-a W=a
W=M-W
MOVLW a
SUBWF M,W
M=M-a W=a
M=M-W
MOVLW a
SUBWF M,F
W=a-M W=M
W=a-W
MOVF M,W
SUBLW a
M=a-M W=M
W=a-W
M=W
MOVF M,W
SUBLW a
MOVWF M
W=W-M W=M-W
W=-W
SUBWF M,W
SUBLW 0
M=W-M W=M-W
W=-W
M=W
SUBWF M,W
SUBLW 0
MOVWF M
W=M-W   SUBWF M,W
M=M-W   SUBWF M,F
W=M1-M2 W=M2
W=M1-W
MOVF M2,W
SUBWF M1,W
M1=M1-M2 W=M2
M1=M1-W
MOVF M2,W
SUBWF M1,F
M2=M1-M2 W=M2
W=M1-W
M2=W
MOVF M2,W
SUBWF M1,W
MOVWF M2
     
Logical Operation OR
W=W OR a   IORLW a

Truth table of OR function :

E1

E2

 

S

0

0

 

0

0

1

 

1

1

0

 

1

1

1

 

1

It is also used to change bits to 1.
if E2=1 then S always equals 1
if E2=0 then S is unchanged.

M=W OR a W=W OR a
M=W
IORLW a
MOVWF M
W=M OR a W=M
W=W OR a
MOVF M,W
IORLW a
M=M OR a W=a
M=M OR W
MOVLW a
IORWF M,F
W=W OR M   IORWF M,W
M=W OR M   IORWF M,F
W=M1 OR M2 W=M2
W=W OR M1
MOVF M2,W
IORWF M1,W
M1=M1 OR M2 W=M2
M1=W OR M1
MOVF M2,W
IORWF M1,F
     
Logical Operation AND
W=W AND a   ANDLW a

Truth table of AND function :

E1

E2

 

S

0

0

 

0

0

1

 

0

1

0

 

0

1

1

 

1

It is also used to change bits to 0.
if E2=0 then S always equals 0
if E2=1 then S is unchanged.

M=W AND a W=W AND a
M=W
ANDLW a
MOVWF M
W=M AND a W=M
W=W AND a
MOVF M,W
ANDLW a
M=M AND a W=a
M=M AND W
MOVLW a
ANDWF M,F
W=W AND M   ANDWF M,W
M=W AND M   ANDWF M,F
W=M1 AND M2 W=M2
W=W AND M1
MOVF M2,W
ANDWF M1,W
M1=M1 AND M2 W=M2
M1=W AND M1
MOVF M2,W
ANDWF M1,F
     
Logical Operation Exclusive OR ( XOR )
W=W XOR a   XORLW a

Truth table of XOR function :

E1

E2

 

S

0

0

 

0

0

1

 

1

1

0

 

0

1

1

 

1

It is also used to permute bits.
if E2=1 then S=NOT S
if E2=0 then S is unchanged.

M=W XOR a W=W XOR a
M=W
XORLW a
MOVWF M
W=M XOR a W=M
W=W XOR a
MOVF M,W
XORLW a
M=M XOR a W=a
M=M XOR W
MOVLW a
XORWF M,F
W=W XOR M   XORWF M,W
M=W XOR M   XORWF M,F
W=M1 XOR M2 W=M2
W=W XOR M1
MOVF M2,W
XORWF M1,W
M1=M1 XOR M2 W=M2
M1=W XOR M1
MOVF M2,W
XORWF M1,F
     
Logical Operation NOT
W=NOT W   XORLW 0xFF

Truth table of NOT function :

E

 

S

0

 

1

1

 

0

It is also used to permute bits.

M=NOT W W=NOT W
M=W
XORLW 0xFF
MOVWF M
W=NOT M   COMF M,W
M=NOT M   COMF M,F
M1=NOT M2 W=NOT M2
M1=W
COMF M,W
MOVWF M1
     
Bits assignement with b the bit number from 0 to 7
W(b)=0   ANDLW B'11111111'
with bit b equals 0
If you have to modify many bits it is better to use to OR or AND functions.
M(b)=0   BCF M,b
W(b)=1   IORLW B'00000000'
with bit b equals 1
M(b)=1   BSF M,b
 
Jump
Jump to a Label   GOTO Label  
Call of the function Label   CALL Label
Returns of a function   RETURN
Returns of the result of a function W=a
RETURN
RETLW a
 
Conditionnal jump
Note : To simplify the table, when the condition is true, a jump is made ( GOTO ) but you can replace it by a function call ( CALL ) or by a line of code and only one.
if M(b) = 1 goto Label   BTFSC M,b
GOTO Label
 
if M(b) = 0 goto Label   BTFSS M,b
GOTO Label
 
M=M-1
if M <> 0 goto Label
  DECFSZ M,F
GOTO Label
 
W=M-1
if W <> 0 goto Label
  DECFSZ M,W
GOTO Label
M=M+1
if M <> 256 goto Label
  INCFSZ M,F
GOTO Label
W=M+1
if M <> 256 goto Label
  INCFSZ M,W
GOTO Label
 
Res represents the result of the last instruction.
if Res = 0 goto Label   BTFSC STATUS,Z
GOTO Label
 
if Res <> 0 goto Label   BTFSS STATUS,Z
GOTO Label
 
After the SUBWF or SUBLW instruction, you can test :
if Res < 0 goto Label   BTFSS STATUS,C
GOTO Label
 
if Res >= 0 goto Label   BTFSC STATUS,C
GOTO Label
 
After the ADDWF or ADDLW instructions, you can test :
if Res > 255 goto Label   BTFSC STATUS,C
GOTO Label
 
if Res <= 255 goto Label   BTFSS STATUS,C
GOTO Label
 
Other frequent cases ( To jump when the condition is false : replace BTFSC by BTFSS )
if W = 0 goto Label W = W + 0
if Res = 0 goto Label
ADDLW 0
BTFSC STATUS,Z
GOTO Label
 
if M = 0 goto Label W=M
if Res = 0 goto Label
MOVF M,W
BTFSC STATUS,Z
GOTO Label
if W = a goto Label W = a - W
if Res = 0 goto Label
SUBLW a
BTFSC STATUS,Z
GOTO Label
if M = a goto Label W = a
W = M - W
if Res = 0 goto Label
MOVLW a
SUBWF M,W
BTFSC STATUS,Z
GOTO Label
if W = M goto Label W = M - W
if Res = 0 goto Label
SUBWF M,W
BTFSC STATUS,Z
GOTO Label
if M1 = M2 goto Label W = M2
W = M1 - W
if Res = 0 goto Label
MOVF M2,W
SUBWF M1,W
BTFSC STATUS,Z
GOTO Label
 
if M < 0 goto Label   BTFSC M,7
GOTO Label
 
if M >= 128 goto Label   BTFSC M,7
GOTO Label
if W <= a goto Label W = a - W
if Res >= 0 goto Label
SUBLW a
BTFSC STATUS,C
GOTO Label
if M >= a goto Label W = a
W = M - W
if Res >= 0 goto Label
MOVLW a
SUBWF M,W
BTFSC STATUS,C
GOTO Label
if W <= M goto Label W = M - W
if Res >= 0 goto Label
SUBWF M,W
BTFSC STATUS,C
GOTO Label
if M1 >= M2 goto Label W = M2
W = M1 - W
if Res >= 0 goto Label
MOVF M2,W
SUBWF M1,W
BTFSC STATUS,C
GOTO Label
 
Other functions
Right rotation with carry   RLF M,W
or RLF M,F
 
Left rotation with carry   RRF M,W
or RRF M,F
No opération   NOP
Permutation   SWAPF M,W
or SWAPF M,F
Clear Watchdog   CLRWDT
Sleep mode   SLEEP
 

 


Last modified : 8 feb 2000