dsPIC33A Oscillator Module Deep Dive
Frequency Drift Detection Example
| Application Example | Clock Monitor Example Project |
In this section, we will show you how to configure the Clock Module for Frequency Drift Detection operation.
The following video provides a short overview of this example:
Here we see a basic example of the Clock Monitor Module configured for frequency drift detection.
CMxCONbits.WIDTH is set to 0, selecting Rising Edge-to-Rising Edge measurement mode. In this mode, WINPR[31:0] defines the accumulation time window generated by the Time Window Generator (TWG).
In this configuration:
- The Reference Clock drives the TWG
- The Monitor Clock drives the counter
Example configuration:
- Reference Clock = 8 MHz (125 ns)
- Monitor Clock = 160 MHz (6.25 ns)
- WINPR[31:0] = 2999, which functionally corresponds to 3000 TWG clock cycles
Since the reference clock operates at 8 MHz, each TWG period is 125 ns. During each window, the 160 MHz monitored clock ideally produces 20 counts.
With WINPR set to 3000, the expected accumulated count is:
- 20 counts × 3000 windows = 60000 counts
At the end of the accumulation window, the captured count is stored in the CMxBUF register for evaluation. The 160 MHz Monitor Clock provides a measurement resolution of 6.25 ns per count, meaning overall measurement precision is determined by the Monitor Clock frequency.
The Warning, Fail, and Saturation threshold registers are programmed using the desired threshold count limits.
For best measurement resolution, the Monitor Clock frequency should be significantly higher than the Reference Clock frequency.
Converting Captured Counts to Frequency
Click Play ► to listen to commentary on how to convert the captured count to the frequency of the monitor clock:

This can be computed using a single C statement as shown:
Example Setup Code
Click Play ► to listen to commentary on how to set up the frequency drift detection measurement in C.

This code is available in the Clock Monitor example project section.
ISR Examples
Click Play ► to listen to commentary on how to set up Interrupt Service Routine (ISR) routines for frequency drift detection measurement.

Frequency Drift Measurement Operation - Key Notes
Click Play ► to listen to commentary on key issues to be aware of when performing frequency drift measurement using the clock monitor.
When the Accumulation Period expires, the current count value is copied to CMxBUF:
- The counter is then reset to 0 to start the next accumulation period.
If CMxBUF[31:0] is not updated at the next accumulation period expiration, the monitored clock is considered to have failed catastrophically, and the CMxCON.ON bit is cleared. The application must correct the condition and then re-enable the module.
- If a High Fail Threshold or Low Fail Threshold event occurs, the CMxCON.ON bit is cleared. The application must correct the condition and then re-enable the module.
Disabling of Threshold Detection
- Setting CMxHFAIL and/or CMxHWARN to 0xFFFFFFFF disables the high-threshold detection.
- Setting CMxLFAIL and/or CMxLWARN to 0x00000000 disables the low-threshold detection.
Start-up Test:
- If the selected Reference Clock is missing at startup, the accumulation window never starts and the TRIG ”CMxSATbits.TRIG” bit remains cleared.
- Software can detect this condition by checking the TRIG bit after system startup. No interrupt or other event is generated.
Run-time Test:
- If the Reference Clock initially toggles and sets the TRIG bit, but later slows significantly or stops toggling, the accumulation window is extended by the same amount.
- In some cases, this causes the counter to saturate before capture occurs, which sets the SATD bit.
- Note that a set SATD bit does not uniquely indicate this condition. The Reference Clock must be operating for SATD to be set.