Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 346 of 9365. Communication Protocols and Data TransferFree lesson

Configuring UART Baud Rate Generators

Calculate and set the peripheral clock prescalers to achieve specific target baud rates while minimizing clock frequency deviation errors.

In this lesson, you will learn how to calculate the precise prescaler values required to match your hardware peripheral clock to your desired UART baud rate while minimizing timing errors.

The Role of the Prescaler

Because UART is asynchronous, both the transmitter and receiver must agree on the exact duration of a single bit. Your microcontroller’s UART peripheral derives its timing from the system clock (or a dedicated peripheral clock), which usually runs at a much higher frequency than the target baud rate (e.g., 80 MHz vs. 115,200 bits per second).

To bridge this gap, we use a Baud Rate Generator (BRG), which acts as a frequency divider. The hardware takes the input clock (fCKf_{CK}) and divides it by a value defined by a prescaler register to produce the sampling clock.

Calculating the Value

The fundamental formula for setting your baud rate is: BaudRate=fCK8×(2−OVER8)×USARTDIVBaud Rate = \frac{f_{CK}}{8 \times (2 - OVER8) \times USARTDIV}

  • fCKf_{CK}: The clock frequency of the peripheral.
  • USARTDIVUSARTDIV: The value you write to your configuration register.
  • OVER8OVER8: A configuration bit that changes the sampling rate (usually set to 0 for standard oversampling by 16).

To solve for the register value (USARTDIVUSARTDIV): USARTDIV=fCKBaudRate×16USARTDIV = \frac{f_{CK}}{Baud Rate \times 16}

Minimizing Error

In practice, USARTDIVUSARTDIV is often a non-integer. Because hardware registers only accept discrete values, rounding your result introduces a deviation error. If the error exceeds 2–3%, the bit sampling will drift, leading to framing errors or garbled data.

For example, if your fCKf_{CK} is 16 MHz and you target 115,200 baud: USARTDIV=16,000,000/(115,200×16)≈8.68USARTDIV = 16,000,000 / (115,200 \times 16) \approx 8.68

You would round this to 9. The actual baud rate then becomes 16,000,000/(9×16)≈111,11116,000,000 / (9 \times 16) \approx 111,111. The percentage error is: Error=∣Target−ActualTarget∣×100≈3.5%Error = \left| \frac{Target - Actual}{Target} \right| \times 100 \approx 3.5\%

If this error is too high, you must adjust your system clock frequency or use a different crystal oscillator to ensure the division results in an integer or a value closer to the hardware's fractional capabilities.

Key Takeaway: The baud rate generator relies on integer division of the peripheral clock; always calculate the percentage error between your target and actual rate to ensure communication remains within the acceptable tolerance for UART framing.