Part 2: Detailed PIC16F18875 Instruction Set and Programming Techniques
1. Arithmetic and Logic Instructions
detailed explanation of key instructions:
- ADDWF f,d: Add W and f
ADDWF PORTA, W ; Add PORTA to W, store result in W.
- ANDWF f,d: AND W with f
ANDWF STATUS, F ; AND W with STATUS, store in STATUS
- COMF f,d: Complement f
COMF COUNT, F ; Complement COUNT, store in COUNT
2. Data Movement Instructions
- MOVF f,d: Move f
MOVF TEMP, W ; Move TEMP to W
- SWAPF f,d: Swap nibbles in f
SWAPF PORTB, F ; Swap nibbles in PORTB, store in PORTB
3. Bit Manipulation Instructions
- BSF f,b: Bit Set f
BSF PORTA, 3 ; Set bit 3 in PORTA
- BTFSC f, b: Bit Test f, Skip if Clear
BTFSC STATUS, Z ; Test Zero flag, skip next if clear.
4. Program Control Instructions
- CALL k: Call subroutine
CALL DELAY ; Call subroutine named DELAY
- GOTO k: Go to address
GOTO MAIN ; Jump to label MAIN
5. Special Instructions and Features
- OPTION: Load OPTION register (for compatibility with older PICs)
- TRIS f: Load TRIS register (also for compatibility)
6. Advanced Programming Techniques
Efficient Register Usage
- Use the Working (W) register for temporary storage to minimize memory access.
- Utilize the STATUS register flags (Z, C, DC) for conditional operations.
Bank Selection
The PIC16F18875 has multiple register banks. Use the BANKSEL
directive for efficient bank switching:
BANKSEL TRISA ; Select bank containing TRISA
MOVLW 0xFF
MOVWF TRISA ; Set PORTA as all inputs
Interrupt Handling
Implement interrupt service routines (ISRs) using the RETFIE
instruction:
ORG 0x0004 ; Interrupt vector location
GOTO ISR ; Jump to ISR
; ... (main program)
ISR:
; Interrupt handling code
RETFIE ; Return from interrupt
Loop Optimization
Use decrement and skip instructions for efficient loops:
MOVLW 10 ; Load loop counter
MOVWF COUNTER
LOOP:
; Loop body
DECFSZ COUNTER, F
GOTO LOOP
; Continue after loop
Bit Manipulation for I/O Control
Use bit-oriented instructions for efficient I/O control:
BSF LATA, 2 ; Set RA2 high (turn on LED)
BCF LATA, 2 ; Clear RA2 low (turn off LED)
BTFSC PORTA, 1 ; Check if RA1 is low (button pressed)
GOTO BUTTON_PRESSED
Summary
While the instruction set is compact, it provides all the necessary tools for complex embedded system development. Regular practice and reference to the official Microchip documentation will help in becoming proficient in PIC assembly programming. Remember to always consult the latest PIC16F18875 datasheet and programming manual for the most up-to-date and detailed information on instructions and their usage.
Below are the lecture videos to support the reading and lab assignments.
Refrences:
[1] https://ww1.microchip.com/downloads/en/DeviceDoc/31029a.pdf
[2] https://forum.microchip.com/s/topic/a5C3l000000MbYLEA0/t374413
[3] https://ww1.microchip.com/downloads/en/DeviceDoc/PIC16%28L%29F18855_75%20Data%20Sheet_DS40001802E.pdf
[4] https://www.microchip.com/en-us/product/pic16f18875
[5] https://www.instructables.com/Programming-PIC-Microcontrollers/