dsPIC33A Oscillator Module Deep Dive

Application Example

Last modified by Microchip on 2026/10/19 09:17

   Clock Selection Example  Frequency Drift Detection Example   

Overview

In this example, we’ll look at a typical Frequency Drift Detection configuration using the Clock Monitor Module, highlighting the use of the Accumulation Time Window.

Click Play ► to listen to commentary that provides an overview of this configuration:

Example frequency drift detection

Window Select (WINSEL) selects CLKGEN2 as the reference clock operating at 8 MHz, while Counter Select (CNTSEL) selects Clock Generator 11 operating at 160 MHz as the monitored clock.

The reference clock is used to create a programmable measurement window. The window duration is defined by the WINPR[31:0] bits in the CMxWINPR register.

During this measurement window, the monitored clock advances an internal counter, allowing the module to accumulate the number of monitored clock cycles that occur within the known time period.

In this example:

  • The reference clock period is 8 MHz (125 ns)
  • The monitored clock frequency is 160 MHz (6.25 ns)
  • Therefore, we expect approximately 20 monitored clock cycles to be counted during each single measurement window

At the end of each accumulation period:

  • The captured count value is transferred into the CMxBUF register
  • The internal counter is cleared
  • A new measurement cycle immediately begins

The captured count can then be compared against programmable threshold limits to detect frequency drift or clock fault conditions.

We’ll review the required Clock Monitor configuration steps in more detail in the following sections.

Accumulation Time Window

Definition

The accumulation time window defines how long the monitored clock is counted before the measurement result is captured.

Accumulation time is measured in Time Window Generator (TWG), clock cycles.

Click Play ► to listen to commentary that defines the accumulation time window:

accumulation time window

The WINPR[31:0] register sets the accumulation window length using the equation:

  • Accumulation Window = WINPR + 1 TWG clock cycles

For example:

  • If WINPR = 0, the accumulation window is 1 TWG clock cycle
  • If WINPR = 65,535, the accumulation window is 65,536 TWG clock cycles

This distinction is important because the accumulation window is always one cycle greater than the programmed WINPR value.

Longer accumulation windows allow more monitored clock cycles to be counted, improving measurement precision and frequency resolution. This is especially important in applications requiring fine frequency drift detection or high-resolution measurements.

Detection Latency Considerations

Click Play ► to listen to commentary on latency in detecting clock errors:

  • Longer time windows accumulate more monitored clock cycles, improving measurement resolution and slightly improving accuracy.
  • The trade-off is higher detection latency, which depends on the frequency relationship between the reference and monitored clocks.
  • If the monitored clock is much faster than the reference clock, a small prescaler (WINPR[31:0]) can reduce detection latency.

Accumulation Time Window Modes

Watch the short video below describing both basic accumulation time window modes:

Here we see the two primary Clock Monitor measurement modes:

  • Frequency Drift Detection, which is rising edge to rising edge
  • Pulse Width and Duty Cycle Measurement, which is rising edge to next falling edge

The configuration flow for both modes is very similar. However, the upper section of each diagram highlights the key configuration differences between the two measurement modes.

The overall functional operation is also similar between the modes, with only a few operational differences.

For Pulse Width and Duty Cycle Measurement mode, the main operational differences occur in steps 4, 5, and 6, where the module handles edge detection and timing measurements differently than in Frequency Drift Detection mode. As you will see steps 1 through 3 are effectively the same.

Understanding these differences helps determine which mode is best suited for the application requirements.

Pulse Width/Duty Cycle Measurement Example

Watch the short video below describing usage of the Pulse Width/Duty Cycle mode:

The key difference from Frequency Drift Detection mode is that the clock assignments are reversed:

  • The Reference Clock now drives the counter
  • The Monitor Clock (PW/DC signal) now drives the TWG

CMxCONbits.WIDTH is set to ‘1’, which selects the Rising Edge-to-Falling Edge measurement mode. In this mode, the WINPR[31:0] register is ignored because the measurement window is controlled directly by the monitored clock edges of the PW/DC signal.

Example configuration:

  • Reference Clock = 20 MHz
  • Monitor Clock = 1 MHz

In this example, the 20 MHz reference clock provides a timing resolution of 50 ns per count. This means the pulse width and duty cycle measurement accuracy is determined by the reference clock frequency. As shown in the timing diagram, the monitored clock defines the measurement window from the rising edge to the falling edge.

The warning, fail and saturation threshold limit registers reflect the number of counts of the 50 ns time periods.

For optimal measurement resolution, the reference clock frequency should be much higher than the monitor clock frequency.

Back to Top