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 () 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:
- : The clock frequency of the peripheral.
- : The value you write to your configuration register.
- : A configuration bit that changes the sampling rate (usually set to 0 for standard oversampling by 16).
To solve for the register value ():
Minimizing Error
In practice, 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 is 16 MHz and you target 115,200 baud:
You would round this to 9. The actual baud rate then becomes . The percentage error is:
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.