If the timer clears its counter at each capture edge, a register holds the duration that just ended, not a timestamp on a continuous counter. Identify the counting mode first. Then decide whether to subtract two readings or add consecutive high and low intervals.
The same capture register can represent two different timing quantities
Industrial acquisition boards commonly use programmable timers to measure an external square wave's period and high duty cycle. An internal timebase drives the counter, and hardware captures its value on rising or falling edges. If the counter runs continuously, successive captures are coordinates in time. Subtract them to obtain an interval, accounting for rollover where appropriate. If the counter is reset at each captured edge, the next capture measures the interval since the previous edge. The units are the same, but the arithmetic changes.
The Renesas RA6M5 general PWM timer, or GPT, is an example. Its hardware manual distinguishes capture registers from buffer registers. An example shows capture on both rising and falling edges of the input, with the counter also cleared on both edges. A capture event transfers the count to a capture register and, as configured, transfers an earlier capture into a buffer. Verify the actual firmware settings for whether the counter clears, which event triggers it and how capture values enter the buffer. The register name alone does not justify reusing an old formula, and the example is not the default behavior of all channels.
Worked example: subtracting the high and low counts loses twelve microseconds
Suppose a board has an effective 24 MHz timer count rate and the external waveform is high for 6 μs and low for 14 μs. Here 24 MHz means the frequency that actually drives the timer after division, not a universal Renesas system clock setting. Assume adequate counter width, no rollover and no missed edges. At 24 ticks per microsecond, the high interval lasts 144 ticks and the low interval 336 ticks.
| Counting mode | Readings over one aligned cycle | Period calculation |
|---|---|---|
| Free running, with a starting count of 1,000 | Rising 1,000, falling 1,144, next rising 1,480 | Subtract like-polarity timestamps to get 480 |
| Clear at each captured edge | The falling edge ends the high interval at 144; the next rising edge ends the low interval at 336 | Add adjacent high and low intervals to get 480 |
If legacy code treats 336 and 144 as cumulative timestamps, it calculates 336−144=192. At 24 MHz that is only 8 μs, 12 μs short of the actual 20 μs. Add both intervals instead: (144+336)÷24 MHz=20 μs, or 50 kHz. The high duty cycle is 144÷480=30%. These are idealized examples, not capture latency or minimum pulse width specifications. Synchronization delays, prescaler quantization and counter reload behavior must be checked against the manual and bench data.
There is a subtler error: adding both counts gives the correct period even if the high and low intervals are reversed. The log then shows 70% instead of 30%. At the receiving pin, a falling edge ends the high interval and a rising edge ends the low interval. If an earlier stage inverts the signal, decide whether the software should report duty cycle at the receiver or at the sensor. A test with clearly unequal high and low intervals exposes this error more readily than a nearly symmetrical waveform.
Separate the first capture from steady capture after reset
The timer may start partway through a high or low interval, so the first capture might not describe a complete segment. Confirm the polarity and how synchronization is established. Discard records that do not yet have a defined starting edge in the selected mode. Do not label odd reads high and even reads low solely by read order. Restarting, pausing or missing an edge can change that order. Where hardware settings allow, retain capture source or edge type and check that neighboring records form a valid pair. If a complete pair cannot be established, mark the record invalid and realign.
Check what the buffer actually stores. It may help firmware retrieve a value before subsequent edges, but it cannot turn a duration into an absolute timestamp. Check register order, buffer settings and processing delay. Observe the external signal and a board debug marker on an independent instrument to verify they refer to the same cycle. A final frequency number alone cannot distinguish incorrect arithmetic, reversed polarity and an intermittent missed edge. Test low duty cycle, high duty cycle and the first capture after restart. If the project needs changing frequency or recovery from a missing pulse, verify realignment and error reporting separately.
Save a reproducible input and timebase before swapping the oscillator or controller
| Check | Evidence to retain | Mistake it prevents |
|---|---|---|
| Oscillator and timer rate | Full order code, main clock, clock tree and rate after division | Using the oscillator's label as the timer tick rate |
| Counting mode | Free running or event clear, startup state and rollover rules | Treating two durations as timestamps |
| Edge relationship | Capture source, inversion and actual input polarity | Reporting reversed duty cycle despite a correct period |
| Capture record | Raw registers, buffer configuration, reset timing and reference waveform | Mistaking a missed edge or partial first interval for frequency error |
| Final validation | True period, high and low intervals, unpaired data and retest conditions | Keeping representative tests for each calculation |
If procurement is offered a tighter oscillator to solve this fault, first verify that the original timebase is out of tolerance. A more accurate part can improve the tick-to-time conversion, but it cannot turn 192 ticks into 480. If both timebases meet the actual budget, correct the configuration and arithmetic first. If a component changes independently, qualify its full code, output interface and design conditions. Save the raw waveform and raw counts for both modes in the same report. That makes future clock-divider or controller changes traceable without guessing from a single frequency column.
For a change review, drive the board at a fixed period and vary only duty cycle. A working receiver should see one count rise while the other falls, with the sum nearly constant. If the reported period follows duty cycle instead, inspect the raw counts and arithmetic together. In a second test, hold duty cycle constant and change the period. Both interval counts should scale together. Neither test requires changing the oscillator. This avoids masking the arithmetic fault by adjusting the timebase.
State whether the reported value is latched before a counter clear or obtained through another event path. If a controller substitution or firmware change alters the event sequence, recheck the derivation. Two features named capture and clear do not necessarily have identical timing.
Frequently asked questions
Does a more accurate oscillator automatically reduce a dual-edge measurement error?
It reduces timebase error, not incorrect subtraction or reversal of high and low intervals. Verify the count interpretation and arithmetic before budgeting the remaining error from the oscillator.
If I read one high count and one low count, can I always add them?
Only when they are adjacent segments of a complete period, use the same timebase and were captured without a configuration change. Discard or realign partial startup intervals, missing captures and readings across mode changes first.
