What is Baud Rate in UART in Embedded C: Simple Explanation
baud rate is the speed at which data bits are sent or received per second. It defines how fast the serial data is transmitted between devices using UART in embedded C programming.How It Works
Imagine two friends talking on walkie-talkies. They must agree on how fast to speak so the other can understand. In UART communication, the baud rate is like the speed of their talk — it tells how many bits of data are sent each second.
When two devices use UART, they must set the same baud rate to communicate correctly. If one talks too fast and the other listens too slow, the message gets scrambled. The baud rate controls timing, ensuring data bits are sent and received in sync.
Example
This example shows how to set the baud rate for UART communication in embedded C for a microcontroller. It configures the UART to 9600 bits per second.
#include <stdint.h> #include <avr/io.h> #define F_CPU 16000000UL // CPU frequency #define BAUD 9600 #define MYUBRR ((F_CPU / 16 / BAUD) - 1) void UART_init(unsigned int ubrr) { UBRR0H = (unsigned char)(ubrr >> 8); // Set baud rate high byte UBRR0L = (unsigned char)ubrr; // Set baud rate low byte UCSR0B = (1 << RXEN0) | (1 << TXEN0); // Enable receiver and transmitter UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // Set frame: 8 data bits, 1 stop bit } int main(void) { UART_init(MYUBRR); // Initialize UART with baud rate 9600 while (1) { // Main loop } return 0; }
When to Use
You use baud rate settings whenever you set up UART communication between microcontrollers, sensors, or computers. It is essential for serial data exchange in embedded systems like Arduino projects, GPS modules, or Bluetooth devices.
Choosing the right baud rate depends on the device capabilities and the distance of communication. Lower baud rates are more reliable over long wires, while higher baud rates allow faster data transfer but need better signal quality.
Key Points
- Baud rate is the number of bits sent per second in UART communication.
- Both devices must use the same baud rate to communicate properly.
- Common baud rates include 9600, 19200, 38400, and 115200.
- Setting baud rate correctly ensures reliable data transfer in embedded systems.