Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
#define pin D2

uint32_t channelRising, channelFalling;
volatile uint32_t FrequencyMeasured, DutycycleMeasured, LastPeriodCapture = 0, CurrentCapture, HighStateMeasured;
volatile uint32_t FrequencyMeasured, DutycycleMeasured, LastPeriodCapture = 0, CurrentCapture;
volatile uint64_t HighStateMeasured;
uint32_t input_freq = 0;
uint64_t capturePeriod;
volatile uint32_t rolloverCompareCount = 0;
HardwareTimer *MyTim;

Expand All @@ -32,18 +34,11 @@ HardwareTimer *MyTim;
void TIMINPUT_Capture_Rising_IT_callback(void)
{
CurrentCapture = MyTim->getCaptureCompare(channelRising);
/* frequency computation */
if (CurrentCapture > LastPeriodCapture)
{
FrequencyMeasured = input_freq / (CurrentCapture - LastPeriodCapture);
DutycycleMeasured = (HighStateMeasured * 100) / (CurrentCapture - LastPeriodCapture);
}
else if (CurrentCapture <= LastPeriodCapture)
{
/* 0x1000 is max overflow value */
FrequencyMeasured = input_freq / (0x10000 + CurrentCapture - LastPeriodCapture);
DutycycleMeasured = (HighStateMeasured * 100) / (0x10000 + CurrentCapture - LastPeriodCapture);
}
const uint64_t ticks = CurrentCapture > LastPeriodCapture ?
uint64_t(CurrentCapture) - LastPeriodCapture :
capturePeriod + CurrentCapture - LastPeriodCapture;
FrequencyMeasured = input_freq / ticks;
DutycycleMeasured = (HighStateMeasured * 100) / ticks;

LastPeriodCapture = CurrentCapture;
rolloverCompareCount = 0;
Expand Down Expand Up @@ -71,15 +66,9 @@ void TIMINPUT_Capture_Falling_IT_callback(void)
/* prepare DutyCycle computation */
CurrentCapture = MyTim->getCaptureCompare(channelFalling);

if (CurrentCapture > LastPeriodCapture)
{
HighStateMeasured = CurrentCapture - LastPeriodCapture;
}
else if (CurrentCapture <= LastPeriodCapture)
{
/* 0x1000 is max overflow value */
HighStateMeasured = 0x10000 + CurrentCapture - LastPeriodCapture;
}
HighStateMeasured = CurrentCapture > LastPeriodCapture ?
uint64_t(CurrentCapture) - LastPeriodCapture :
capturePeriod + CurrentCapture - LastPeriodCapture;
}


Expand Down Expand Up @@ -115,15 +104,21 @@ void setup()
// Configure rising edge detection to measure frequency
MyTim->setMode(channelRising, TIMER_INPUT_FREQ_DUTY_MEASUREMENT, pin);

// With a PrescalerFactor = 1, the minimum frequency value to measure is : TIM counter clock / CCR MAX
// = (SystemCoreClock) / 65535
// Example on Nucleo_L476RG with systemClock at 80MHz, the minimum frequency is around 1,2 khz
// To reduce minimum frequency, it is possible to increase prescaler. But this is at a cost of precision.
// At prescale factor 1, the minimum measurable frequency is the timer
// clock divided by its full period. A larger prescale factor lowers that
// frequency at the cost of precision.
// The maximum frequency depends on processing of both interruptions and thus depend on board used
// Example on Nucleo_L476RG with systemClock at 80MHz the interruptions processing is around 10 microseconds and thus Max frequency is around 100kHz
uint32_t PrescalerFactor = 1;
MyTim->setPrescaleFactor(PrescalerFactor);
MyTim->setOverflow(0x10000); // Max Period value to have the largest possible time to detect rising edge and avoid timer rollover
uint64_t maxPeriod = 0x10000;
#if defined(IS_TIM_32B_COUNTER_INSTANCE)
if (sizeof(MyTim->getOverflow()) == sizeof(uint64_t) && IS_TIM_32B_COUNTER_INSTANCE(Instance)) {
maxPeriod = uint64_t{1} << 32;
}
#endif
MyTim->setOverflow(maxPeriod);
capturePeriod = MyTim->getOverflow();
MyTim->attachInterrupt(channelRising, TIMINPUT_Capture_Rising_IT_callback);
MyTim->attachInterrupt(channelFalling, TIMINPUT_Capture_Falling_IT_callback);
MyTim->attachInterrupt(Rollover_IT_callback);
Expand Down
29 changes: 16 additions & 13 deletions examples/Peripherals/HardwareTimer/InputCapture/InputCapture.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,17 @@
uint32_t channel;
volatile uint32_t FrequencyMeasured, LastCapture = 0, CurrentCapture;
uint32_t input_freq = 0;
uint64_t capturePeriod;
volatile uint32_t rolloverCompareCount = 0;
HardwareTimer *MyTim;

void InputCapture_IT_callback(void)
{
CurrentCapture = MyTim->getCaptureCompare(channel);
/* frequency computation */
if (CurrentCapture > LastCapture) {
FrequencyMeasured = input_freq / (CurrentCapture - LastCapture);
}
else if (CurrentCapture <= LastCapture) {
/* 0x1000 is max overflow value */
FrequencyMeasured = input_freq / (0x10000 + CurrentCapture - LastCapture);
}
const uint64_t ticks = CurrentCapture > LastCapture ?
uint64_t(CurrentCapture) - LastCapture :
capturePeriod + CurrentCapture - LastCapture;
FrequencyMeasured = input_freq / ticks;
LastCapture = CurrentCapture;
rolloverCompareCount = 0;
}
Expand Down Expand Up @@ -66,15 +63,21 @@ void setup()
// Configure rising edge detection to measure frequency
MyTim->setMode(channel, TIMER_INPUT_CAPTURE_RISING, pin);

// With a PrescalerFactor = 1, the minimum frequency value to measure is : TIM counter clock / CCR MAX
// = (SystemCoreClock) / 65535
// Example on Nucleo_L476RG with systemClock at 80MHz, the minimum frequency is around 1,2 khz
// To reduce minimum frequency, it is possible to increase prescaler. But this is at a cost of precision.
// At prescale factor 1, the minimum measurable frequency is the timer
// clock divided by its full period. A larger prescale factor lowers that
// frequency at the cost of precision.
// The maximum frequency depends on processing of the interruption and thus depend on board used
// Example on Nucleo_L476RG with systemClock at 80MHz the interruption processing is around 4,5 microseconds and thus Max frequency is around 220kHz
uint32_t PrescalerFactor = 1;
MyTim->setPrescaleFactor(PrescalerFactor);
MyTim->setOverflow(0x10000); // Max Period value to have the largest possible time to detect rising edge and avoid timer rollover
uint64_t maxPeriod = 0x10000;
#if defined(IS_TIM_32B_COUNTER_INSTANCE)
if (sizeof(MyTim->getOverflow()) == sizeof(uint64_t) && IS_TIM_32B_COUNTER_INSTANCE(Instance)) {
maxPeriod = uint64_t{1} << 32;
}
#endif
MyTim->setOverflow(maxPeriod);
capturePeriod = MyTim->getOverflow();
MyTim->attachInterrupt(channel, InputCapture_IT_callback);
MyTim->attachInterrupt(Rollover_IT_callback);
MyTim->resume();
Expand Down