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

Why PIC Timer2 postscaling leaves PWM frequency unchanged

A fan fixture uses PIC18F46K22 PWM. It has a 20 MHz passive crystal and a verified 20 MHz system clock. Changing the Timer2 postscaler slows the interrupt but leaves the PWM rate unchanged. The timer block diagram explains this directly. Identify whether the physical output period or the software control-update rate needs adjustment. The timer provides separate timing paths for them.

Find where PWM and interrupts branch

In this example Timer2 provides the CCP PWM time base. It counts from one quarter of the system oscillator frequency after prescaling. When the count matches the period register, the next increment resets the timer, begins a new PWM period and loads buffered duty data. The data sheet places the postscaler on the interrupt event path. PWM uses every match. The interrupt condition can be produced only after a selected number of matches.

Read back the timer selected by the actual CCP. This device family offers more than one PWM timer resource. Changing Timer2 will not change a PWM based on another timer. Mark interrupt entry on a debug pin and capture it alongside the real fan PWM. If the debug pin is merely toggled, its complete square wave spans two interrupts. Account for that separately from the PWM frequency.

Calculate a directly reproducible configuration

With clock multiplication disabled, this example runs at the nominal 20 MHz passive crystal frequency. The oscillator period is 50 ns, and the timer base before prescaling is 200 ns. Select a Timer2 prescale of four and PR2 of 124. The PWM period is 125 × 200 ns × 4 = 100 µs, or 10 kHz. A postscale of 16 produces a nominal interrupt condition every 16 matches, or 1.6 ms and 625 Hz. A postscale of eight makes that interval 0.8 ms while PWM stays at 100 µs.

To target 5 kHz, set PR2 to 249 under the same conditions. It doubles the PWM period to 200 µs. Keeping the eight-to-one postscaler also returns the interrupt interval to 1.6 ms. Recalculate duty data if the percentage should stay fixed. A 50% duty code in this example changes from 250 to 500 as the period doubles. Reusing the old value can change the drive fraction even when the frequency is correct.

Verify steady settings and then their transitions

Start at a recognizable moderate duty. Capture PWM and interrupt timing, then change the postscaler alone. In a second trial, keep the postscaler and update both period and duty. Save the written and read-back settings so another initialization cannot hide what changed. If the timer clocks multiple PWM outputs, capture another channel as well. Outputs sharing that timer inherit its period changes. For an unaffected channel, verify its timer assignment.

For a live update, capture the last old cycle, the transition and the first new cycles. The data sheet describes double-buffered duty. The timing of the register writes and the latch boundary can affect which values appear next. A system that requires smooth adjustment should check shortest and longest transitional pulses with the real load. Keep this test separate from an average frequency measurement, which can miss a one-time pulse.

Count real PWM periods between interrupt markers. If the count occasionally differs, check whether software masked or deferred the interrupt. Hardware matches continue even when the service routine is delayed. Integration and speed estimates should use the actual software timing that produced them. Record physical output and software update as independent metrics.

Keep debug markers short and repeat the output test without them. If the shipping build uses different compiler options, verify its update boundary too. Save the decoded period and divider settings from actual startup rather than only the control panel's PWM label. That makes it clear which path a configuration change touched.

For a replacement crystal, keep a system-clock measurement and both observed periods from the same board. If both change proportionally, check the timebase. If only the control update changes, inspect postscale and interrupt scheduling.

Record the 20 MHz passive crystal, effective system clock, selected timer, both dividers and scope points. Specify physical PWM period and software control refresh independently. A replacement crystal or system clock setting requires both calculations and both measurements again. One capture showing both periods makes the distinction readily verifiable.

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.