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

A steady 20 MHz oscillator feeds ATmega324PB. Why did the prescaler fail to change?

A 20 MHz active oscillator drives an ATmega324PB. A transition from the slow to the fast setting sometimes leaves a timer running at one quarter of the expected rate. The log shows the write statement ran, and the external frequency never changed. The next useful measurement is the chip's divided system clock. Record both clocks and a transition marker to distinguish a rejected prescaler write, stale peripheral settings, and a change of the source.

Measure the external and divided clocks separately

ATmega324PB supports an external clock. Check voltage, logic levels, and speed grade together. This example uses a 20 MHz oscillator under supply conditions that support that rate, with CKOUT programmed on an engineering board. The data sheet says CLKO carries the clock after the prescaler and remains active during reset. The fuse overrides normal pin operation. Check the board's connections before turning a pin already used for something else into an output. A dedicated test board is enough when production units lack access.

Connect one scope input to a suitable external clock point near the MCU and another to CLKO, with an extra channel for a firmware marker or the failed peripheral. Put the marker before and after the protected writes, clear of the gap between them. If a peripheral serves as a marker, check that timing remains interpretable after the rate changes. Save actual edges around the transition and associate the part's CLKPR readback with that capture.

Consider a change from divide by eight to divide by two. The 20 MHz oscillator period is fifty nanoseconds. CLKO starts at 2.5 MHz and a 400-nanosecond period, then reaches ten megahertz and 100 nanoseconds. The data sheet gives the switch interval following the valid write as the old period plus one to two new periods, or approximately 500 to 600 nanoseconds in this example. Two active edges occur in that interval. A single period within it need not match either steady rate. Several microseconds of continuing 400-nanosecond periods suggest a change that failed or was subsequently overwritten.

Count instruction cycles between the writes

CLKPR protection requires writing CLKPCE with the other bits clear, then writing the intended division and clearing the enable within four cycles. The data sheet calls for disabling interrupts during this procedure. At the initial 2.5 MHz clock, four cycles are 1.6 microseconds. This is the deadline between writes, separate from the time for the new rate to take effect. A function call, debug print, or interrupt in the middle can miss the window. The later assignment can execute while hardware retains the original divisor.

Inspect the program image actually used on the hardware. Confirm that the two writes are close enough in instruction cycles. Debug and production builds can differ, and single-step debugging cannot replace an uninterrupted run. Save and restore the interrupt state instead of assuming that the caller entered with interrupts enabled. For firmware with frequent interrupts, record trials with the same board idle and under load. Save successful transition counts and CLKPR readback to confirm the repair covers normal operation.

An engineering build can intentionally exceed the write window to verify that the fixture distinguishes a rejected change from a successful one. Conduct this comparison outside operational communications or real-time control. Restore the correct code and verify both directions under normal load. Use the same firmware and fixture for both oscillator samples so that any remaining difference has a traceable cause.

A capture may initially show ten megahertz and then return to 2.5 MHz. A short record can end before the second transition, obscuring the condition that causes it. Extend the capture to include the power-management task and use register readback markers to locate a second writer. For example, disable automatic slowing on an engineering board and record a manual transition. Restore the automatic policy and check whether any second transition agrees with the state machine. Also capture hardware reset so a second low-speed selection is distinguishable from a reboot.

Check peripheral configuration after CLKO changes

If CLKO reaches ten megahertz and CLKPR confirms the change, inspect the peripheral setup. A timer can have its own divider and a UART can retain a baud-rate value computed for the old system clock. Measure a known pattern on synchronous peripherals and check for separate references on asynchronous resources. If the external clock itself no longer measures twenty megahertz, check the source, the receiver, voltages, and clock selection before rewriting the prescaler procedure.

The acceptance record should retain both steady clock rates, the transition marker, CLKPR readback, and the peripheral period for the same event. If production does not expose CLKO, first establish its relationship to the peripheral waveform on a test board and choose a production observation that leaves normal operation intact. Accept the replacement when the actual system clock and its dependent peripherals both behave correctly.

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.