UART Serial Communication with PIC Microcontrollers: Theory and Practice
Pic Serial Communication Assembly Code: A Beginner's Guide
Serial communication is a method of transferring data bit by bit over a serial bus. It is widely used in embedded systems, telecommunication, and data transmission applications. In this article, we will learn how to use serial communication with PIC microcontrollers using assembly code.
Pic Serial Communication Assembly Code
PIC microcontrollers are popular and versatile devices that can be programmed in different languages, such as C, BASIC, or assembly. Assembly language is the lowest level of programming, where each instruction corresponds to a machine code that the microcontroller can execute directly. Assembly language gives us full control over the hardware and allows us to optimize the performance and memory usage of our programs.
One of the advantages of PIC microcontrollers is that they have built-in modules for serial communication, such as UART (Universal Asynchronous Receiver Transmitter), SPI (Serial Peripheral Interface), or I2C (Inter-Integrated Circuit). In this article, we will focus on UART, which is one of the most common and simple serial communication protocols.
What is UART?
UART stands for Universal Asynchronous Receiver Transmitter. It is a hardware device that converts parallel data into serial data and vice versa. UART can operate in asynchronous mode, where the sender and receiver do not share a common clock signal, or in synchronous mode, where they do.
UART has two main pins: TX (transmit) and RX (receive). The TX pin sends serial data out of the microcontroller, while the RX pin receives serial data into the microcontroller. The data is sent and received in frames, which consist of a start bit, 8 data bits, and a stop bit. Optionally, a parity bit can be added for error detection.
The baud rate is the speed of serial communication, measured in bits per second (bps). The baud rate of the sender and receiver must match for successful communication. The baud rate can be calculated as follows:
baud rate = Fosc / (4 * (SPBRG + 1))
where Fosc is the oscillator frequency of the microcontroller and SPBRG is a register that determines the baud rate.
How to use UART with PIC assembly code?
To use UART with PIC assembly code, we need to configure some registers and enable some interrupts. Here are the main steps:
Configure PORTA as I/O port and set all pins as output.
Configure CMCON register to disable analog comparator.
Configure PIE1 register to enable or disable peripheral interrupts.
Configure INTCON register to enable or disable general interrupts.
Configure SPBRG register to set the desired baud rate.
Configure TXSTA register to set the transmission parameters.
Configure RCSTA register to enable serial port and receive mode.
Create an interrupt service routine to handle received data.
Create a main loop to send data or wait for button press.
The following code shows an example of using UART with PIC assembly code. It uses two PIC16F877A microcontrollers connected via TX and RX pins. One microcontroller acts as a transmitter and sends a byte when a button is pressed. The other microcontroller acts as a receiver and displays the received byte on an LCD screen.
;TRANSMITTER CODE
PROCESSOR 16F876A
INCLUDE
__config _XT_OSC & _WDT_OFF & _PWRTE_OFF & _CP_OFF & _LVP_OFF & _BODEN_OFF
TMP_TX EQU 0x20 ;VALUE TO HOLD TRANSMITTED VALUE
ORG 0x000
GOTO MAIN
ORG 0x004 ;INTERRUPT VECTOR
GOTO INTERRUPT
MAIN:
;---USE PORTB AS I/O
CLRF PORTB
BSF STATUS,RP0 ;SWITCH TO BANK1
MOVLW D'255' ;USE PORTB AS I/O
MOVWF TRISB ;SET ALL PINS OF PORTB AS INPUT
BCF OPTION_REG,7
;---CONFIGURE PERIPHERAL INTERRUPTS
MOVLW B'00000100' ;DISABLE ALL PERIPHERAL INTERRUPTS EXCEPT TRANSMITTER
MOVWF PIE1 ;PERIPHERAL INTERRUPT ENABLE/DISABLE
;---CONFIGURE GENERAL INTERRUPTS
MOVLW B'01000000' ;DISABLE ALL INTERRUPTS EXCEPT PERIPHERAL
MOVWF INTCON ;INTERRUPT CONTROL REGISTER
;---CONFIGURE SPBRG FOR DESIRED BAUD RATE
MOVLW D'51' ;WE WILL USE 4800bps
MOVWF SPBRG ;BAUD AT 4MHZ
;---CONFIGURE TXSTA
MOVLW B'00100100' ;CONFIGURE TXSTA AS :
MOVWF TXSTA ;
;8 BIT TRANSMISSION - 6.BIT
;TRANSMIT ENABLED - 5.BIT
;ASYNCHRONOUS MODE - 4.BIT
;ENABLE HIGH SPEED BAUD RATE - 2.BIT
BCF STATUS,RP0 ;SWITCH TO BANK0
MOVLW B'10000000' ;ENABLE SERIAL PORT
MOVWF RCSTA ;RECEIVE STATUS REG
CLRF TMP_TX
BSF INTCON,7 ;ENABLE ALL UNMASKED INTERRUPTS
BSF TXSTA,5 ;ENABLE USART TRANSMIT
MAIN_LOOP: ;CONTINOUS LOOP
BTFSC PORTB,4 ;CHECK IF THE BUTTON IS PRESSED
GOTO MAIN_LOOP ;IF NOT GOTO CONTINOUS LOOP
INCF TMP_TX ;INCREMENT TMP_TX BY ONE
MOVF TMP_TX,W ;LOAD TMP_TX TO W REGISTER
MOVWF TXREG ;MOVE W TO TXREG TO TRANSMIT THE DATA
GOTO MAIN_LOOP ;CONTINOUS LOOP
INTERRUPT:
BCF INTCON,7 ;DISABLE ALL INTERRUPTS
BTFSS PIR1,4 ;CHECK IF THE TXIF FLAG IS SET
GOTO QUIT_INT ;IF NOT RETURN BACK TO THE MAIN LOOP
BCF PIR1,4 ;CLEAR THE FLAG
RETFIE
QUIT_INT:
RETFIE
END
;RECEIVER CODE
PROCESSOR 16F876A
INCLUDE
__config _XT_OSC & _WDT_OFF & _PWRTE_OFF & _CP_OFF & _LVP_OFF & _BODEN_OFF
TMP_RX EQU 0x20 ;VALUE TO HOLD RECEIVED VALUE
COL_CNT EQU 0x21
LINE_CNT EQU 0x22
ORG 0x000
GOTO MAIN
ORG 0x004 ;INTERRUPT VECTOR
GOTO INTERRUPT
INCLUDE
MAIN:
CALL LCD_INIT ;FIRST OF ALL WE HAVE TO INITIALIZE LCD
CLRF LINE_CNT
MOVLW 0xFF
MOVWF COL_CNT
;---USE PORTA AS I/O
CLRF PORTA
MOVLW D'7' ;USE PORTA AS I/O
MOVWF CMCON ;
BSF STATUS,RP0 ;SWITCH TO BANK1
CLRF TRISA ;SET ALL PINS OF PORTA AS OUTPUT
BCF OPTION_REG,7
;---CONFIGURE PERIPHERAL INTERRUPTS
MOVLW B'00100000' ;DISABLE ALL PERIPHERAL INTERRUPTS EXCEPT RECEIVER
MOVWF PIE1 ;PERIPHERAL INTERRUPT ENABLE/DISABLE
;---CONFIGURE GENERAL INTERRUPTS
MOVLW B'01000000' ;DISABLE ALL INTERRUPTS EXCEPT PERIPHERAL
MOVWF INTCON ;INTERRUPT CONTROL REGISTER
;---CONFIGURE SPBRG FOR DESIRED BAUD RATE
MOVLW D'51' ;WE WILL USE 4800bps
MOVWF SPBRG ;BAUD AT 4MHZ
;---CONFIGURE TXSTA
MOVLW B'00100100' ;CONFIGURE TXSTA AS :
MOVWF TXSTA ;
;8 BIT TRANSMISSION - 6.BIT
;TRANSMIT ENABLED - 5.BIT
;ASYNCHRONOUS MODE - 4.BIT
;ENABLE HIGH SPEED BAUD RATE - 2.BIT
BCF STATUS,RP0 ;SWITCH TO BANK0
MOVLW B'10000000' ;ENABLE SERIAL PORT
MOVWF RCSTA ;RECEIVE STATUS REG
CLRF TMP_RX
BSF INTCON,7 ;ENABLE ALL UNMASKED INTERRUPTS
BSF RCSTA,4 ;ENABLE USART RECEIVE
MAIN_LOOP: ;CONTINOUS LOOP
GOTO MAIN_LOOP
INTERRUPT:
BCF INTCON,7 ;DISABLE ALL INTERRUPTS
BTFSS PIR1,5 ;CHECK IF THE RCIF FLAG IS SET
GOTO QUIT_INT ;IF NOT RETURN BACK TO THE MAIN LOOP
MOVF RCREG,W ;MOVE THE RECEIVED BYTE TO
Conclusion
In this article, we have learned how to use serial communication with PIC microcontrollers using assembly code. We have seen how to configure the UART module and how to send and receive data using interrupts. We have also created a simple application that demonstrates the communication between two PIC microcontrollers.
Serial communication is a useful and powerful technique that can enable us to communicate with other devices, such as sensors, LCDs, computers, or other microcontrollers. By using assembly code, we can optimize our programs and have full control over the hardware. However, assembly code can also be complex and tedious to write and debug. Therefore, it is important to understand the basics of serial communication and UART before attempting to use it in our projects.
We hope you have enjoyed this article and learned something new. If you have any questions or comments, please feel free to share them below. Happy coding! 4e3182286b
https://soundcloud.com/nattalia-nunez/grasshopper-rhino-6-download-crack-hot
https://soundcloud.com/stanglthuw0/hotel-billing-software-free-download-full-version-with-crack-top