Sunteți pe pagina 1din 7

EE2801 -- Lecture 20

The PIC Instruction Set

EE2801-L20P01

The PIC 16F87X Series Instruction Set (Complete!)


Mnemonic, Operands ADDWF ANDWF CLRF CLRW COMF DECF DECFSZ INCF INCFSZ IORWF MOVF MOVWF NOP RLF RRF SUBWF SWAPF XORWF BCF BSF BTFSC BTFSS ADDLW ANDLW CALL CLRWDT GOTO IORLW MOVLW RETFIE RETLW RETURN SLEEP SUBLW XORLW f, d f, d f f, d f, d f, d f, d f, d f, d f, d f f, d f, d f, d f, d f, d f, b f, b f, b f, b k k k k k k k k k Cycles 14-Bit Opcode MSb BYTE-ORIENTED FILE REGISTER OPERATIONS Add W and f 1 00 0111 dfff AND W with f 1 00 0101 dfff Clear f 1 00 0001 lfff Clear W 1 00 0001 0xxx Complement f 1 00 1001 dfff Decrement f 1 00 0011 dfff Decrement f, Skip if 0 1(2) 00 1011 dfff Increment f 1 00 1010 dfff Increment f, Skip if 0 1(2) 00 1111 dfff Inclusive OR W with f 1 00 0100 dfff Move f 1 00 1000 dfff Move W to f 1 00 0000 lfff No Operation 1 00 0000 0xx0 Rotate Left f through Carry 1 00 1101 dfff Rotate Right f through Carry 1 00 1100 dfff Subtract W from f 1 00 0010 dfff Swap nibbles in f 1 00 1110 dfff Exclusive OR W with f 1 00 0110 dfff BIT-ORIENTED FILE REGISTER OPERATIONS 01 00bb bfff 1 Bit Clear f 01 01bb bfff 1 Bit Set f 01 10bb bfff 1 (2) Bit Test f, Skip if Clear 01 11bb bfff 1 (2) Bit Test f, Skip if Set LITERAL AND CONTROL OPERATIONS Add literal and W 1 11 111x kkkk AND literal with W 1 11 1001 kkkk Call subroutine 2 10 0kkk kkkk Clear Watchdog Timer 1 00 0000 0110 Go to address 2 10 1kkk kkkk Inclusive OR literal with W 1 11 1000 kkkk Move literal to W 1 11 00xx kkkk Return from interrupt 2 00 0000 0000 Return with literal in W 2 11 01xx kkkk Return from Subroutine 2 00 0000 0000 Go into standby mode 1 00 0000 0110 Subtract W from literal 1 11 110x kkkk Exclusive OR literal with W 1 11 1010 kkkk Description LSb Status Affected C,DC,Z Z Z Z Z Z Z Z Z C C C,DC,Z Z Notes

ffff ffff ffff xxxx ffff ffff ffff ffff ffff ffff ffff ffff 0000 ffff ffff ffff ffff ffff ffff ffff ffff ffff kkkk kkkk kkkk 0100 kkkk kkkk kkkk 1001 kkkk 1000 0011 kkkk kkkk

1,2 1,2 2 1,2 1,2 1,2,3 1,2 1,2,3 1,2 1,2 1,2 1,2 1,2 1,2 1,2 1,2 1,2 3 3

C,DC,Z Z TO,PD Z

TO,PD C,DC,Z Z

EE2801-L20P02

Figuring Out The Instruction Syntax


The PIC instruction set is nice, in that once you figure out one instruction youve figured out most of them, but it is a little different than what were used to. The secret codes and how instructions are formed is described in the following tables:

Byte-oriented file register operations 13 8 7 6 OPCODE d f (FILE #) d = 0 for destination W d = 1 for destination f f = 7-bit file register address Bit-oriented file register operations 13 10 9 7 6 OPCODE b (BIT #) b = 3-bit bit address f = 7-bit file register address Literal and control operations

TA B L E 13-1:

OPCODE FIELD DESCRIPTIONS

Field
f W b k x

Descriptio n
Register file address (0x00 to 0x7F) Working register (accumulator)

0 f (FILE #)

Bit address within an 8-bit file register

Literal field, constant data or label

Don't care location (=

or

The assembler will generate code with x =

. It

is the recommended form of use for compatibility with all Microchip software tools.

d PC TO PD

Destination select; d =

: store result in W,

General 13 OPCODE

8 7 k (literal)

d = 1: store result in file register f. Default is d =

k = 8-bit immediate value CALL and GOTO instructions only 13 11 10 OPCODE k (literal) k = 11-bit immediate value

Program Counter

Time-out bit

Power-down bit

EE2801-L20P03

Comparing The Old And The New


Lets look at two equivalent programs. One on our old friend, the 80x86, the other on this new-fangled PIC Microcontroller.

Consider the following x86 program:

.Model small .Data var1 db 33h var2 db 0A4h var3 db ? .Code .8086 start: mov ax, @Data ;Set data segment. mov ds, ax mov mov add mov al, var1 bl, var2 al, bl var3, al ;Set Al to 33h ;Set BL to A4h ;Compute al = al + bl ;Move sum to memory ;Stop program

stop: jmp stop end start

EE2801-L20P04

A Complete, But Simple PIC Program


; Our first PIC Processor Example ; include p16f877.inc ;Include file for register definitions ; ************************************************************ ; equates Bank0Ram equ 0x20 ;Equates mean the same as before! ; ************************************************************ ; variables - Remember, these are in register file memory! cblock Bank0Ram var1 ; 0x20 locations used for variables var2 ; 0x21 var3 ; 0x22 temp location used for counter endc ; ************************************************************ ; start at the reset vector Start org 0x000 nop bcf STATUS,RP0 bcf STATUS,RP1 0x33 var1 0A4h var2 ; ; ; ; ; ; ; ; Set origin at memory address 000 very important - required for debugger go to BANK 0 by setting RP1:RP0 to 0 0.

; Initialize ; movlw movwf movlw movwf

contents of variables. Move Move Move Move literal 33h into W. from W to var1 (initialize var1) literal A4h into W. from W to var2 (initialize var2)

; ; Do the addition. movf var1,W addwf var2,W movwf var3 stop goto End stop

; Get data from var1 into W. ; Calculate W = var1 + var2. ; Store result in var3.

EE2801-L20P05

The Hardest Thing About The PIC!


The hardest thing to understand about the PIC is the conditional jump. There are only two jump instructions, but the way they work is a little strange. Hang on!

BTFSS

Bit Test f, Ski p if Set

BTFSC

Bit Test, Skip if Clear

label

label

CY CY

EE2801-L20P06

A Simple Decrement Loop On The PIC


; Our second PIC Processor Example ; include p16f877.inc ;Include file for register definitions ; ************************************************************ ; equates count equ 0x25 ;Manually assign register address! ; ************************************************************ ; start at the reset vector Start org 0x000 nop bcf STATUS,RP0 bcf STATUS,RP1 ; ; ; ; Set origin at memory address 000 very important - required for debugger go to BANK 0 by setting RP1:RP0 to 0 0.

; Initialize contents of variables. ; movlw 0x0A ; Initialize count to 10 decimal by movwf count ; setting W and moving W -> count. ; ; Do the simple loop. loop decf count,F ; count = count - 1. btfsc STATUS,Z ; Skip next instruction if Z = 0. stop End
EE2801-L20P07

goto goto

stop loop

; End program. ; Continue looping!

S-ar putea să vă placă și