"

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

  1. 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.
  2. 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.
  3. MOVF (Move File register to W register):
    • Syntax: MOVF f, d
    • Function: Moves the contents of the file register f to the W register if d is 0.
    • Example: MOVF 0x20, W ; Moves the contents of file register 0x20 to the W register.

Arithmetic Instructions

  1. 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 in f if d is 1.
    • Example: ADDWF 0x20, F ; Adds W register to file register 0x20, storing the result in 0x20.
  2. 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 in W if d is 0.
    • Example: SUBWF 0x20, W ; Subtracts W register from file register 0x20, storing the result in W.
  3. INCF (Increment File register):
    • Syntax: INCF f, d
    • Function: Increments the contents of the file register f by one and stores the result in f if d is 1.
    • Example: INCF 0x20, F ; Increments the contents of file register 0x20 by one, storing the result in 0x20.
  4. DECF (Decrement File register):
    • Syntax: DECF f, d
    • Function: Decrements the contents of the file register f by one and stores the result in W if d is 0.
    • Example: DECF 0x20, W ; Decrements the contents of file register 0x20 by one, storing the result in W.

Logical Instructions

  1. 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 in f if d is 1.
    • Example: ANDWF 0x20, F ; Performs AND operation between W register and file register 0x20, storing result in 0x20.
  2. 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 in f if d is 1.
    • Example: IORWF 0x20, F ; Performs OR operation between W register and file register 0x20, storing result in 0x20.
  3. 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 in f if d is 1.
    • Example: XORWF 0x20, F ; Performs XOR operation between W register and file register 0x20, storing result in 0x20.

Bit-Oriented Instructions

  1. BCF (Bit Clear File register):
    • Syntax: BCF f, b
    • Function: Clears the specified bit b in the file register f.
    • Example: BCF 0x20, 2 ; Clears bit 2 of file register 0x20.
  2. BSF (Bit Set File register):
    • Syntax: BSF f, b
    • Function: Sets the specified bit b in the file register f.
    • Example: BSF 0x20, 3 ; Sets bit 3 of file register 0x20.
  3. BTFSC (Bit Test File register, Skip if Clear):
    • Syntax: BTFSC f, b
    • Function: Tests bit b in the file register f 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.
  4. BTFSS (Bit Test File register, Skip if Set):
    • Syntax: BTFSS f, b
    • Function: Tests bit b in the file register f 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.

Control Instructions

  1. NOP (No Operation):
    • Syntax: NOP
    • Function: Executes no operation and moves to the next instruction.
    • Example: NOP ; Simply does nothing for one instruction cycle.
  2. CLRWDT (Clear Watchdog Timer):
    • Syntax: CLRWDT
    • Function: Resets the watchdog timer to prevent a system reset.
    • Example: CLRWDT ; Resets the watchdog timer.
  3. SLEEP:
    • Syntax: SLEEP
    • Function: Puts the microcontroller into sleep mode to save power.
    • Example: SLEEP ; Puts the microcontroller into sleep mode.
  4. RESET:
    • Syntax: RESET
    • Function: Resets the microcontroller.
    • Example: RESET ; Resets the microcontroller.

Branching Instructions

  1. GOTO:
    • Syntax: GOTO k
    • Function: Unconditionally branches to the specified address k.
    • Example: GOTO 0x100 ; Jumps to address 0x100.
  2. CALL:
    • Syntax: CALL k
    • Function: Calls a subroutine at the specified address k.
    • Example: CALL 0x200 ; Calls the subroutine at address 0x200.
  3. RETURN:
    • Syntax: RETURN
    • Function: Returns from a subroutine to the calling function.
    • Example: RETURN ; Returns from a subroutine.
  4. RETFIE (Return from Interrupt):
    • Syntax: RETFIE
    • Function: Returns from an interrupt service routine.
    • Example: RETFIE ; Returns from an interrupt service routine.
  5. 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

 

License

Icon for the Creative Commons Attribution 4.0 International License

Introduction to Microcontrollers Copyright © 2024 by Lake Washington Institute of Technology is licensed under a Creative Commons Attribution 4.0 International License, except where otherwise noted.