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

A 16 MHz crystal stays steady. Why does an ATmega328PB timestamp jump by 128 microseconds?

An ATmega328PB uses a 16 MHz crystal to time external markers from a reference source. The scope shows stable 1 ms spacing, but software sometimes reports a long interval followed by a short one. Preserve the raw timer values when this happens. A low byte and a high byte from different hardware states can create an apparent timing jump while the oscillator remains stable.

Locate the stage where the discrepancy appears

Apply a known pulse interval from an independent source within the board's electrical conditions. Observe the receiver pin and an available buffered clock or hardware timing witness, avoiding direct loading of the crystal pins. Set a known timer prescaler and mode. Record capture sequence, register value, overflow state and relevant interrupt timing. If the input never reaches the capture pin or the event is absent, check that stage first. If events occur but software reports inconsistent values, inspect register access.

Microchip documents that the input capture event copies the counter into ICR1. The AVR data bus is eight bits wide, so the CPU reads the sixteen-bit value in two operations. Reading the low byte latches the high byte into the timer's temporary register. The following high-byte access reads that latched value. The order is necessary. An interrupt between the two accesses can still overwrite the temporary register if the interrupt uses an affected register in the same timer.

Check emitted code for the device's required low-then-high sequence. A device-defined sixteen-bit read will normally provide the supported byte order, but the main task and interrupt may share a timer. The datasheet also distinguishes accesses that use the temporary register from those that do not. The type in source code does not by itself protect the pair against interruption.

Convert the byte error into a timing error

Suppose the 16 MHz clock really reaches the timer and the prescaler is eight. One count is 0.50 μs. An event correctly captured as hexadecimal 3412 has a low byte of 12. If an intervening timer access changes the latched high byte to 35, software reads 3512. The difference is hexadecimal 0100, or 256 counts and 128 μs. Other corrupted bytes can produce larger jumps.

For markers every 1.000 ms the normal difference is 2,000 counts. If the middle timestamp is 256 counts high, the first interval reports 1.128 ms and the next 0.872 ms. Their sum is still 2.000 ms, so an average can look correct. A step in whole byte increments is a reason to inspect the readout before treating the interval as oscillator ppm.

Check actual timer wrap separately. At a 2 MHz tick rate, a sixteen-bit timer rolls over once in 32.768 ms. If software extends the count, or if capture and overflow occur close together, apply the documented event ordering. Start with a known interval shorter than one wrap and then verify wrap handling separately. Both defects can make a timestamp jump while requiring different corrections.

Reproduce the interrupt interaction and repeat under real workload

On controlled engineering firmware, keep pulse timing fixed and add an interrupt that accesses the same timer. Compare the normal read, the case with that interrupt and a read protected as the manufacturer describes. Mark firmware events where practical and check whether the error only appears between the two byte accesses. Restore the original interrupt state after the brief protected read. Avoid blocking normal interrupts for substantially longer than the access needs.

If the error remains, check whether a newer edge replaced the capture value before software retrieved it. Save hardware capture counts, software log length and input marker sequence. A full hardware count with missing log entries points to logging or buffer timing. A missing hardware capture calls for checking input conditions and configuration. Keep an independent record of the reference marker interval through all tests.

Qualify both the original and the candidate 16 MHz crystals with the same controller setup. Test realistic interrupt traffic and normal load, and verify that the corrected readout has not reduced capture coverage. If the reference waveform and board clock remain steady and the read correction clears the error, address the measurement firmware first. Test oscillator tolerance separately using a direct, validated frequency measurement.

If software saves only converted decimal times, the structure of the low byte is harder to recover. Keep raw hexadecimal snapshots and nearby hardware event IDs in a diagnostic build. Recalculate off-board and check for errors in multiples of 256 counts. Keep serial logging load comparable because continuous printing can move the interrupt timing.

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.