Skip to content

Commit 835be18

Browse files
ABOSTMfpistm
authored andcommitted
HardwareTimer: Fix frequency computation when there is no more signal
When HardwareTimer is used in input capture mode, we need to handle the case where timer counter rollover. Let's set values to 0 in both examples: * InputCapture * Frequency_Dutycycle_measurement FIX 789
1 parent 9b7956a commit 835be18

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

examples/Peripherals/HardwareTimer/Frequency_Dutycycle_measurement/Frequency_Dutycycle_measurement.ino

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ void TIMINPUT_Capture_Rising_IT_callback(HardwareTimer*)
3838
LastPeriodCapture = CurrentCapture;
3939
}
4040

41+
/* In case of timer rollover, frequency is to low to be measured set values to 0
42+
To reduce minimum frequency, it is possible to increase prescaler. But this is at a cost of precision. */
43+
void Rollover_IT_callback(HardwareTimer*)
44+
{
45+
FrequencyMeasured = 0;
46+
DutycycleMeasured = 0;
47+
}
4148

4249
/**
4350
@brief Input capture interrupt callback : Compute frequency and dutycycle of input signal
@@ -103,6 +110,7 @@ void setup()
103110
MyTim->setOverflow(0x10000); // Max Period value to have the largest possible time to detect rising edge and avoid timer rollover
104111
MyTim->attachInterrupt(channelRising, TIMINPUT_Capture_Rising_IT_callback);
105112
MyTim->attachInterrupt(channelFalling, TIMINPUT_Capture_Falling_IT_callback);
113+
MyTim->attachInterrupt(Rollover_IT_callback);
106114

107115
MyTim->resume();
108116

examples/Peripherals/HardwareTimer/InputCapture/InputCapture.ino

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ void InputCapture_IT_callback(HardwareTimer*)
2727
LastCapture = CurrentCapture;
2828
}
2929

30+
/* In case of timer rollover, frequency is to low to be measured set value to 0
31+
To reduce minimum frequency, it is possible to increase prescaler. But this is at a cost of precision. */
32+
void Rollover_IT_callback(HardwareTimer*)
33+
{
34+
FrequencyMeasured = 0;
35+
}
3036

3137
void setup()
3238
{
@@ -53,6 +59,7 @@ void setup()
5359
MyTim->setPrescaleFactor(PrescalerFactor);
5460
MyTim->setOverflow(0x10000); // Max Period value to have the largest possible time to detect rising edge and avoid timer rollover
5561
MyTim->attachInterrupt(channel, InputCapture_IT_callback);
62+
MyTim->attachInterrupt(Rollover_IT_callback);
5663
MyTim->resume();
5764

5865
// Compute this scale factor only once

0 commit comments

Comments
 (0)