Specialist Supplier of Crystals & Frequency Control Componentssales@mecxtal.com.cn0755-26500570
MERLIN home / Rednote / Passive Crystals and Oscillator Circuits

Why does an ATmega328P UART miss 115200 baud with an accurate 20 MHz crystal?

An ATmega328P instrument exchanges data reliably at 9600 baud, but occasionally reports framing errors at 115200. The main clock measures close to 20 MHz. Before buying a tighter crystal, separate oscillator tolerance from the UART's integer division error. A perfect 20 MHz clock still produces a bit interval different from the target if the divider cannot express the desired ratio.

Check the actual system clock and divisors

In asynchronous normal mode, the manufacturer's formula divides the system clock by 16 and then by UBRR plus one. Double-speed mode changes 16 to 8. Use the clock that actually reaches the peripheral after any system prescaler. Record the U2X and UBRR readbacks. Check the bootloader and application separately if they configure the port independently.

At exactly 20 MHz, normal mode with UBRR 10 produces 20,000,000 ÷ 176, or about 113,636 baud. Double-speed mode with UBRR 21 has the same denominator and the same rate. Both are about 1.36% below 115,200. The receive sampling method changes with U2X, so use the data-sheet error limits and the peer's requirements when selecting a mode.

The ideal value of UBRR plus one in normal mode is about 10.851. Truncating to 10 produces 125,000 baud. Compare the two adjacent integer choices rather than assuming the formula as written in a macro will round to the closest useful rate. A correct frequency constant does not resolve the choice of integer.

Compare clock tolerance against the measured error

An example tolerance of 20 ppm at 20 MHz means ±400 Hz at the reference. That contributes about ±2.27 baud around 113,636. The integer divider accounts for about 1,564 baud of difference from the target. A tighter oscillator reduces its own contribution. To reduce the fixed error, review actual rate requirements and approved clock frequencies.

An 18.432 MHz crystal with normal-mode UBRR 9 gives a nominal 115,200 baud if the MCU and supply conditions allow that clock. A 16 MHz board gives about 111,111 baud with normal-mode UBRR 8, or 117,647 baud with double-speed UBRR 16. These are approximately −3.55% and +2.12%. Recalculate for each rate and board instead of reusing one U2X setting everywhere.

For a bench measurement, transmit repeated 0x55 bytes. Measure multiple bit intervals across the alternating edges. At 115,200 baud a bit is about 8.681 µs. At the calculated 20 MHz setting it is 8.8 µs. If the measured value follows the divider, check the peer's timing margin. If edges shift through a level translator or cable, capture the same transmission at both ends. A decoder's configured baud rate is not a measurement of the physical bit interval.

Calculate the mismatch between the two ends

Suppose a transmitter runs at 113,636 baud and the receiving clock is 1% higher than its 115,200 target, equivalent to 116,352 baud. Relative to the receiver, the transmitter is about 2.33% slow. If both ends run slow, the errors can partly cancel. Include both directions of tolerance in qualification. Use the applicable receiver limit for the frame length and sampling mode, with additional margin for signal edges and temperature.

With eight data bits, no parity and one stop bit, an ideal stop-bit center is about 9.5 bit intervals after the start edge. A difference between 8.8 µs and 8.681 µs per bit separates those ideal centers by about 1.13 µs. This calculation illustrates cumulative phase error and omits the implementation's synchronization and sampling details. Test both 0x00 and 0x55 and preserve the trace around the stop bit. A fault that changes with cable and test pattern also calls for a signal-integrity check.

Qualify the oscillator and firmware together

A frequency change also affects timers, SPI and other peripherals. Document supply and temperature, check the permitted frequency for the MCU variant, and test both directions with the actual peer and frame format. Include the failing direction, physical bit times and conditions in the log. If the error is limited to one receiver, compare its timing and threshold observations before asking suppliers for a different tolerance.

For a 20 MHz crystal, retain the load, ESR, complete order code and temperature range. A same-frequency active oscillator requires an approved external-input circuit, but the same divider still produces the same nominal baud rate. Keep the actual system clock, divisor readback and measured bit width in the board test report. Record the firmware build used with each board revision so a later repair cannot accidentally reuse the settings for a 16 MHz board.

References

WeChat

MERLIN · TIMING COMPONENTS

Scan with WeChat to contact Sales

Add our WeCom contacts from your usual WeChat app.

On desktop, scan with your phone. On mobile, save the QR image and select it from your album in WeChat Scan.