WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit eac3e82

Browse files
ABOSTMfpistm
authored andcommitted
Fix issue introduced by 'Fix frequency computation when there is no more signal'
It happends that frequency was measured at 0 whereas signal was present. It is due to the fact that rollover is not abnormal: timer is never reset to avoid missing some cpu cycles in the computation. Loss of signal is detected by 2 consecutives rollover without rising edge.
1 parent d69c715 commit eac3e82

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

examples/Peripherals/HardwareTimer/Frequency_Dutycycle_measurement/Frequency_Dutycycle_measurement.ino

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
uint32_t channelRising, channelFalling;
1414
volatile uint32_t FrequencyMeasured, DutycycleMeasured, LastPeriodCapture = 0, CurrentCapture, HighStateMeasured;
1515
uint32_t input_freq = 0;
16+
volatile uint32_t rolloverCompareCount = 0;
1617
HardwareTimer *MyTim;
1718

1819
/**
@@ -36,14 +37,20 @@ void TIMINPUT_Capture_Rising_IT_callback(HardwareTimer*)
3637
}
3738

3839
LastPeriodCapture = CurrentCapture;
40+
rolloverCompareCount = 0;
3941
}
4042

4143
/* In case of timer rollover, frequency is to low to be measured set values to 0
4244
To reduce minimum frequency, it is possible to increase prescaler. But this is at a cost of precision. */
4345
void Rollover_IT_callback(HardwareTimer*)
4446
{
45-
FrequencyMeasured = 0;
46-
DutycycleMeasured = 0;
47+
rolloverCompareCount++;
48+
49+
if (rolloverCompareCount > 1)
50+
{
51+
FrequencyMeasured = 0;
52+
DutycycleMeasured = 0;
53+
}
4754
}
4855

4956
/**

examples/Peripherals/HardwareTimer/InputCapture/InputCapture.ino

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
uint32_t channel;
1212
volatile uint32_t FrequencyMeasured, LastCapture = 0, CurrentCapture;
1313
uint32_t input_freq = 0;
14+
volatile uint32_t rolloverCompareCount = 0;
1415
HardwareTimer *MyTim;
1516

1617
void InputCapture_IT_callback(HardwareTimer*)
@@ -25,13 +26,20 @@ void InputCapture_IT_callback(HardwareTimer*)
2526
FrequencyMeasured = input_freq / (0x10000 + CurrentCapture - LastCapture);
2627
}
2728
LastCapture = CurrentCapture;
29+
rolloverCompareCount = 0;
2830
}
2931

3032
/* In case of timer rollover, frequency is to low to be measured set value to 0
3133
To reduce minimum frequency, it is possible to increase prescaler. But this is at a cost of precision. */
3234
void Rollover_IT_callback(HardwareTimer*)
3335
{
34-
FrequencyMeasured = 0;
36+
rolloverCompareCount++;
37+
38+
if (rolloverCompareCount > 1)
39+
{
40+
FrequencyMeasured = 0;
41+
}
42+
3543
}
3644

3745
void setup()

0 commit comments

Comments
 (0)