How to Configure UART in Embedded C: Simple Guide
To configure
UART in Embedded C, you need to set the baud rate, data bits, parity, and stop bits by writing to the UART control registers. Then enable the UART transmitter and receiver to start communication.Syntax
UART configuration involves setting registers for baud rate, frame format, and enabling transmitter/receiver.
- UBRRx: Sets baud rate.
- UCSRnC: Configures data bits, parity, and stop bits.
- UCSRnB: Enables transmitter and receiver.
embedded_c
void UART_Init(unsigned int ubrr) { /* Set baud rate */ UBRR0H = (unsigned char)(ubrr >> 8); UBRR0L = (unsigned char)ubrr; /* Set frame format: 8 data bits, no parity, 1 stop bit */ UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); /* Enable receiver and transmitter */ UCSR0B = (1 << RXEN0) | (1 << TXEN0); }
Example
This example initializes UART at 9600 baud, sends a character, and waits to receive one.
embedded_c
#include <avr/io.h> #define F_CPU 16000000UL #define BAUD 9600 #define MYUBRR ((F_CPU/16/BAUD)-1) void UART_Init(unsigned int ubrr) { UBRR0H = (unsigned char)(ubrr >> 8); UBRR0L = (unsigned char)ubrr; UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // 8-bit data UCSR0B = (1 << RXEN0) | (1 << TXEN0); // Enable RX and TX } void UART_Transmit(unsigned char data) { while (!(UCSR0A & (1 << UDRE0))) {} UDR0 = data; } unsigned char UART_Receive(void) { while (!(UCSR0A & (1 << RXC0))) {} return UDR0; } int main(void) { UART_Init(MYUBRR); UART_Transmit('H'); unsigned char received = UART_Receive(); while(1) {} return 0; }
Common Pitfalls
Common mistakes when configuring UART include:
- Setting wrong baud rate causing communication errors.
- Not enabling transmitter or receiver bits.
- Incorrect frame format (data bits, parity, stop bits) mismatch with the other device.
- Forgetting to wait for transmit buffer to be empty before sending data.
embedded_c
/* Wrong: Not enabling transmitter */ void UART_Init_Wrong(unsigned int ubrr) { UBRR0H = (unsigned char)(ubrr >> 8); UBRR0L = (unsigned char)ubrr; UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); UCSR0B = (1 << RXEN0); // TXEN0 missing } /* Right: Enable both transmitter and receiver */ void UART_Init_Right(unsigned int ubrr) { UBRR0H = (unsigned char)(ubrr >> 8); UBRR0L = (unsigned char)ubrr; UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); UCSR0B = (1 << RXEN0) | (1 << TXEN0); }
Quick Reference
Remember these key UART register bits:
| Register | Bit | Purpose |
|---|---|---|
| UBRR0H/UBRR0L | All bits | Set baud rate value |
| UCSR0C | UCSZ01, UCSZ00 | Set data bits (e.g., 8 bits) |
| UCSR0C | UPM01, UPM00 | Set parity mode |
| UCSR0C | USBS0 | Set stop bits (1 or 2) |
| UCSR0B | RXEN0 | Enable receiver |
| UCSR0B | TXEN0 | Enable transmitter |
| UCSR0A | UDRE0 | Data register empty flag |
| UCSR0A | RXC0 | Receive complete flag |
Key Takeaways
Set baud rate correctly by calculating and writing to UBRR registers.
Configure frame format (data bits, parity, stop bits) in UCSRnC register.
Always enable both transmitter and receiver in UCSRnB register.
Wait for transmit buffer to be empty before sending data.
Match UART settings on both communicating devices to avoid errors.