Part 4: Instruction Set for 16F18875 Specific to Lab Assignments and Video Lectures
This section provides an overview of the crucial data transfer, arithmetic, logical, and bit-oriented instructions for the 16F18875 microcontroller, which are fundamental to the Lab Assignments and Lecture Videos. Some of these instruction sets might overlap with previous readings. Understanding these instructions is key for effective assembly language programming and practical application in various embedded systems projects.
Data Transfer Instructions
- MOVLW (Move Literal to W register):
- Syntax:
MOVLW k
- Function: Loads the literal value
k
into the W register. - Example:
MOVLW 0x55
; Loads the value 0x55 into the W register.
- Syntax:
- MOVWF (Move W register to File register):
- Syntax:
MOVWF f
- Function: Copies the contents of the W register to the specified file register
f
. - Example:
MOVWF 0x20
; Copies the contents of the W register to file register 0x20.
- Syntax:
- MOVF (Move File register to W register):
- Syntax:
MOVF f, d
- Function: Moves the contents of the file register
f
to the W register ifd
is 0. - Example:
MOVF 0x20, W
; Moves the contents of file register 0x20 to the W register.
- Syntax:
Arithmetic Instructions
- ADDWF (Add W register to File register):
- Syntax:
ADDWF f, d
- Function: Adds the contents of the W register to the file register
f
, and stores the result inf
ifd
is 1. - Example:
ADDWF 0x20, F
; Adds W register to file register 0x20, storing the result in 0x20.
- Syntax:
- SUBWF (Subtract W register from File register):
- Syntax:
SUBWF f, d
- Function: Subtracts the contents of the W register from the file register
f
, and stores the result inW
ifd
is 0. - Example:
SUBWF 0x20, W
; Subtracts W register from file register 0x20, storing the result in W.
- Syntax:
- INCF (Increment File register):
- Syntax:
INCF f, d
- Function: Increments the contents of the file register
f
by one and stores the result inf
ifd
is 1. - Example:
INCF 0x20, F
; Increments the contents of file register 0x20 by one, storing the result in 0x20.
- Syntax:
- DECF (Decrement File register):
- Syntax:
DECF f, d
- Function: Decrements the contents of the file register
f
by one and stores the result inW
ifd
is 0. - Example:
DECF 0x20, W
; Decrements the contents of file register 0x20 by one, storing the result in W.
- Syntax:
Logical Instructions
- ANDWF (AND W register with File register):
- Syntax:
ANDWF f, d
- Function: Performs a bitwise AND operation between the W register and the file register
f
, storing the result inf
ifd
is 1. - Example:
ANDWF 0x20, F
; Performs AND operation between W register and file register 0x20, storing result in 0x20.
- Syntax:
- IORWF (Inclusive OR W register with File register):
- Syntax:
IORWF f, d
- Function: Performs a bitwise OR operation between the W register and the file register
f
, storing the result inf
ifd
is 1. - Example:
IORWF 0x20, F
; Performs OR operation between W register and file register 0x20, storing result in 0x20.
- Syntax:
- XORWF (Exclusive OR W register with File register):
- Syntax:
XORWF f, d
- Function: Performs a bitwise XOR operation between the W register and the file register
f
, storing the result inf
ifd
is 1. - Example:
XORWF 0x20, F
; Performs XOR operation between W register and file register 0x20, storing result in 0x20.
- Syntax:
Bit-Oriented Instructions
- BCF (Bit Clear File register):
- Syntax:
BCF f, b
- Function: Clears the specified bit
b
in the file registerf
. - Example:
BCF 0x20, 2
; Clears bit 2 of file register 0x20.
- Syntax:
- BSF (Bit Set File register):
- Syntax:
BSF f, b
- Function: Sets the specified bit
b
in the file registerf
. - Example:
BSF 0x20, 3
; Sets bit 3 of file register 0x20.
- Syntax:
- BTFSC (Bit Test File register, Skip if Clear):
- Syntax:
BTFSC f, b
- Function: Tests bit
b
in the file registerf
and skips the next instruction if the bit is clear. - Example:
BTFSC 0x20, 4
; Tests bit 4 of file register 0x20, skips next instruction if bit is clear.
- Syntax:
- BTFSS (Bit Test File register, Skip if Set):
- Syntax:
BTFSS f, b
- Function: Tests bit
b
in the file registerf
and skips the next instruction if the bit is set. - Example:
BTFSS 0x20, 5
; Tests bit 5 of file register 0x20, skips next instruction if bit is set.
- Syntax:
Control Instructions
- NOP (No Operation):
- Syntax:
NOP
- Function: Executes no operation and moves to the next instruction.
- Example:
NOP
; Simply does nothing for one instruction cycle.
- Syntax:
- CLRWDT (Clear Watchdog Timer):
- Syntax:
CLRWDT
- Function: Resets the watchdog timer to prevent a system reset.
- Example:
CLRWDT
; Resets the watchdog timer.
- Syntax:
- SLEEP:
- Syntax:
SLEEP
- Function: Puts the microcontroller into sleep mode to save power.
- Example:
SLEEP
; Puts the microcontroller into sleep mode.
- Syntax:
- RESET:
- Syntax:
RESET
- Function: Resets the microcontroller.
- Example:
RESET
; Resets the microcontroller.
- Syntax:
Branching Instructions
- GOTO:
- Syntax:
GOTO k
- Function: Unconditionally branches to the specified address
k
. - Example:
GOTO 0x100
; Jumps to address 0x100.
- Syntax:
- CALL:
- Syntax:
CALL k
- Function: Calls a subroutine at the specified address
k
. - Example:
CALL 0x200
; Calls the subroutine at address 0x200.
- Syntax:
- RETURN:
- Syntax:
RETURN
- Function: Returns from a subroutine to the calling function.
- Example:
RETURN
; Returns from a subroutine.
- Syntax:
- RETFIE (Return from Interrupt):
- Syntax:
RETFIE
- Function: Returns from an interrupt service routine.
- Example:
RETFIE
; Returns from an interrupt service routine.
- Syntax:
- Conditional Branch Instructions:
- BRA: Branches to a relative address.
- BRW: Branches to the address in the W register.
- BC: Branch if Carry bit is set.
- BNC: Branch if Carry bit is not set.
- BZ: Branch if Zero bit is set.
- BNZ: Branch if Zero bit is not set.
Example Programs
- LED Blinking:
START:
BSF TRISB, 0 ; Set RB0 as input
BCF TRISB, 1 ; Set RB1 as output
LOOP:
BTFSS PORTB, 0 ; Check if button is pressed
GOTO SKIP
BSF PORTB, 1 ; Turn on LED
GOTO LOOP
SKIP:
BCF PORTB, 1 ; Turn off LED
GOTO LOOP
END
- Button Press Detection:
START:
BSF TRISB, 0 ; Set RB0 as input
BCF TRISB, 1 ; Set RB1 as output
CHECK_BUTTON:
BTFSS PORTB, 0 ; Check if button is pressed
GOTO BUTTON_PRESSED
GOTO CHECK_BUTTON
BUTTON_PRESSED:
BSF PORTB, 1 ; Turn on LED
GOTO CHECK_BUTTON
END
- Simple Subroutine:
MAIN:
CALL SUBROUTINE
GOTO MAIN
SUBROUTINE:
BSF PORTB, 1 ; Turn on LED
RETURN
END