← Back to Book Detail

Part 3: Advanced Instructions and Practical Applications for PIC16F18875 Assembl (16/12) -- Introduction to Microcontrollers

Browse
133%

Part 3: Advanced Instructions and Practical Applications for PIC16F18875 Assembl

Part 3: Advanced Instructions and Practical Applications for PIC16F18875 Assembly Programming 1. Advanced Instructions The PIC16F18875 microcontroller includes several advanced instructions that enhance its functionality and allow for more complex operations. These instructions are essential for efficient and powerful assembly programming. 1.1 Rotate and Shift Instructions Rotate and shift instructions are used for bit manipulation, which is crucial in applications such as cryptography, data compression, and error detection. - RLF f,d: Rotate Left through Carry RLF PORTA, F ; Rotate PORTA left through carry, store in PORTA - RRF f,d: Rotate Right through Carry RRF PORTB, W ; Rotate PORTB right through carry, store in W - SWAPF f,d: Swap Nibbles in f SWAPF TEMP, F ; Swap nibbles in TEMP, store in TEMP 1.2 Bit-Oriented Instructions Bit-oriented instructions allow for precise control over individual bits in registers, which is essential for tasks like setting flags, toggling LEDs, or reading sensor states. - BSF f,b: Bit Set f BSF STATUS, Z ; Set Zero flag in STATUS - BCF f,b: Bit Clear f BCF STATUS, C ; Clear Carry flag in STATUS - BTFSC f,b: Bit Test f, Skip if Clear BTFSC PORTA, 1 ; Test bit 1 of PORTA, skip next if clear - BTFSS f,b: Bit Test f, Skip if Set BTFSS PORTB, 0 ; Test bit 0 of PORTB, skip next if set 1.3 Literal and Control Instructions These instructions are used for immediate data manipulation and controlling the flow of the program. - MOVLW k: Move Literal to W MOVLW 0x55 ; Move 0x55 to W - ADDLW k: Add Literal to W ADDLW 0x0A ; Add 10 to W - SUBLW k: Subtract W from Literal SUBLW 0x20 ; Subtract W from 32, store in W - RETFIE: Return from Interrupt RETFIE ; Return from interrupt, enable global interrupts 2. Practical Applications Understanding how to apply these instructions in real-world scenarios is crucial for effective programming. Here are some practical applications of the PIC16F18875 instruction set. 2.1 Implementing Delays Delays are essential in many embedded applications, such as blinking LEDs or creating time intervals for sensor readings. ; Delay subroutine (approx. 1 millisecond delay) DELAY_MS: MOVLW D'250' ; Load W with 250 MOVWF COUNT1 ; Move W to COUNT1 MOVWF COUNT2 ; Move W to COUNT2 DELAY_LOOP: DECFSZ COUNT1, F ; Decrement COUNT1, skip if zero GOTO DELAY_LOOP ; Repeat loop DECFSZ COUNT2, F ; Decrement COUNT2, skip if zero GOTO DELAY_LOOP ; Repeat loop RETURN ; Return from subroutine 2.2 Reading and Writing to I/O Ports Controlling I/O ports is fundamental in embedded systems for tasks such as reading sensor data or controlling actuators. ; Initialize PORTA as output BANKSEL TRISA CLRF TRISA ; Clear TRISA register (set PORTA as output) ; Set RA0 highBSF LATA, 0 ; Set bit 0 of LATA (turn on LED connected to RA0) ; Read RA1 and store result in WBANKSEL PORTA BTFSC PORTA, 1 ; Test bit 1 of PORTA GOTO BIT_SET GOTO BIT_CLEAR BIT_SET:MOVLW 0x01 ; Load W with 1 GOTO CONTINUE BIT_CLEAR: MOVLW 0x00 ; Load W with 0 CONTINU
← Previous Chapter Next Chapter →