Introduction to USART
1. USART Definition
USART (Universal Synchronous/Asynchronous Receiver/Transmitter) is a communication protocol used to transfer data between electronic devices, such as microcontrollers, sensors, and other components. This protocol is highly flexible as it supports two main modes:
- Synchronous
- Asynchronous
2. Operating Modes
A. Asynchronous Mode
In this mode, the USART module transmits data without an external clock signal. Synchronization is achieved using data frames consisting of:
- Start Bit: Indicates the beginning of transmission.
- Data Bits: Contains the primary information (typically 5-9 bits).
- Parity Bit (Optional): Used for error detection (Even, Odd, or None).
- Stop Bit: Indicates the end of transmission.
Characteristics: Suitable for long-distance communication or between devices that do not share the same clock.
B. Synchronous Mode
Uses a clock signal to synchronize data transfer between the transmitter and the receiver.
- Requires the same clock configuration on both devices.
- Allows for faster and more reliable data transfer compared to asynchronous mode.
Characteristics: Ideal for multimedia applications or high-speed bulk data transfers.
3. Configuration and Baud Rate
To use USART, several parameters must be defined:
- Baud Rate: The speed of data transmission (bits per second).
- Data Format: The number of data bits, parity, and stop bits.
- Interrupt: Enables notifications when data is finished being sent or received.
Baud Rate Calculation Formula (UBRR)
The UBRR (USART Baud Rate Register) value is calculated based on the CPU clock frequency ($F_{CPU}$) and the desired Baud Rate:
$$UBRR = \frac{F_{CPU}}{16 \times \text{BAUD}} - 1$$
Calculation Example: If $F_{CPU} = 16 \text{ MHz}$ and the target $\text{BAUD} = 9600 \text{ bps}$: $$UBRR = \frac{16,000,000}{16 \times 9600} - 1$$ $$UBRR = 104.16 - 1 \approx 103$$