Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
UART (Universal Asynchronous Receiver-Transmitter) is a crucial communication protocol widely used in embedded systems for serial communication. In the context of microchip microcontrollers, UART is essential for enabling communication between the microcontroller and other devices such as sensors, computers, and other microcontrollers. This article will guide you through the process of implementing UART_Receive on a microchip microcontroller, highlighting its importance and providing practical examples to help you get started.
Examples:
First, you need to configure the UART module on your microchip microcontroller. This involves setting the baud rate, enabling the UART module, and configuring the RX pin.
#include <xc.h>
#define _XTAL_FREQ 20000000 // Define the system clock frequency
void UART_Init(long baud_rate) {
unsigned int x;
x = (_XTAL_FREQ - baud_rate*64)/(baud_rate*64); // Calculate the baud rate generator value
if(x > 255) {
x = (_XTAL_FREQ - baud_rate*16)/(baud_rate*16); // Adjust for high-speed mode
BRGH = 1; // Set high-speed mode
}
if(x < 256) {
SPBRG = x; // Set the baud rate generator register
SYNC = 0; // Set asynchronous mode
SPEN = 1; // Enable the serial port
TXEN = 1; // Enable transmission
CREN = 1; // Enable continuous reception
TX9 = 0; // Set 8-bit transmission
RX9 = 0; // Set 8-bit reception
}
}
void main() {
UART_Init(9600); // Initialize UART with 9600 baud rate
while(1) {
// Main loop
}
}
Next, implement the function to receive data via UART. This function will wait for data to be received and then return the received byte.
char UART_Receive() {
while(!RCIF); // Wait until the reception is complete
return RCREG; // Return the received byte from the receive register
}
void main() {
UART_Init(9600); // Initialize UART with 9600 baud rate
char received_data;
while(1) {
received_data = UART_Receive(); // Receive data
// Process the received data
}
}
To handle UART reception more efficiently, you can use interrupts. First, configure the UART module to enable receive interrupts.
#include <xc.h>
#define _XTAL_FREQ 20000000 // Define the system clock frequency
void UART_Init(long baud_rate) {
unsigned int x;
x = (_XTAL_FREQ - baud_rate*64)/(baud_rate*64); // Calculate the baud rate generator value
if(x > 255) {
x = (_XTAL_FREQ - baud_rate*16)/(baud_rate*16); // Adjust for high-speed mode
BRGH = 1; // Set high-speed mode
}
if(x < 256) {
SPBRG = x; // Set the baud rate generator register
SYNC = 0; // Set asynchronous mode
SPEN = 1; // Enable the serial port
TXEN = 1; // Enable transmission
CREN = 1; // Enable continuous reception
TX9 = 0; // Set 8-bit transmission
RX9 = 0; // Set 8-bit reception
RCIE = 1; // Enable UART receive interrupt
PEIE = 1; // Enable peripheral interrupts
GIE = 1; // Enable global interrupts
}
}
void __interrupt() ISR() {
if(RCIF) {
char received_data = RCREG; // Read the received data
// Process the received data
RCIF = 0; // Clear the receive interrupt flag
}
}
void main() {
UART_Init(9600); // Initialize UART with 9600 baud rate
while(1) {
// Main loop
}
}