The PIC16 instruction set
The instruction set is your window into controlling these chips to do your bidding. Knowing what the chips understand helps you come up with a plan of attack to solve your particular problem.
We need to get some basic info out of the way. The way variables are handled are through registers, these are 8-bit data buckets in the MCU. Also, the PIC’s used here are loosely an Accumulator based architecture, meaning most all operations have one destination, the accumulator known as the W register in PIC land, although you can usually redirect the result to a register… Your other registers are in the “register file” which is your data RAM.
Alright, now we’re getting to the interesting stuff. The instruction set is the list of all commands that the MCU understands. I’ll list them out and briefly describe what they do and what they are used for.
There is also a STATUS register in this chip, various bits of it respond to various operations. Consult the datasheet for more information on this.
- f = name/address of register
- b = bit # (0 – 7)
- d = destination (0 for W, 1 for the register specified in f)
- k = literal value, an immediate, a number or address
Byte-oriented file register operations
Instruction | Description |
---|---|
ADDWF f,d | adds W and f together |
ANDWF f,d | bitwise AND: W & f |
CLRF | clears f (sets it to 0 = 0x00 = 0000 0000) |
CLRW | clears W (sets it to 0 = 0x00 = 0000 0000) |
COMF f,d | compliment f (inverts. COMF of 0010 0101 = 1101 1010) |
DECF f,d | decrement f (f = f – 1) |
DECFSZ f,d | decrement f, skip next instruction if result is zero |
INCF f,d | increment f (f = f + 1) |
INCFSZ f,d | increment f, skip next instruction if result is zero |
IORWF f,d | bitwise OR: W | f |
MOVF f,d | move f (used to load a register into W: MOVF temp1,w) |
MOVWF f | move W into f |
NOP | no-op. no operation occurs this cycle, useful in delays or waits |
RLF f,d | bitwise rotate left through the carry bit |
RRF f,d | bitwise rotate right through the carry bit |
SUBWF f,d | subtracts W from f |
SWAPF f,d | swap nibbles in f (nibble = 4bits, half-byte) |
XORWF f,d | bitwise XOR: W ^ f |
Bit-oriented file register operations
Instruction | Description |
---|---|
BCF f,b | clears bit ‘b’ in register f |
BSF f,b | sets bit ‘b’ in register f |
BTFSC f,b | test bit ‘b’ in register f, if it is clear -> skip the next instruction |
BTFSS f,b | test bit ‘b’ in register f, if it is set -> skip the next instruction |
Literal and control operations
Instruction | Description |
---|---|
ADDLW k | add k to W |
ANDLW k | bitwise AND: W & k -> W |
CALL k | call a subroutine at address k |
CLRWDT | clear the ‘watchdog timer’ |
GOTO k | goto or jump to address k |
IORLW k | bitwise OR: W | k -> W |
MOVLW k | move literal (immediate) k into W |
RETFIE | return from interrupt |
RETLW k | return from subroutine with literal k in W |
RETURN | return from subroutine |
SLEEP | go to sleep mode (low-power standby) |
SUBLW k | subtract W from literal (immediate) k |
XORLW k | bitwise XOR: W ^ k -> W |