2026-04-15 5:39 AM - last edited on 2026-04-15 8:22 AM by Sara BEN HADJ YAHYA
Hello,
I used STM32CubeMX2 to configure a timer (timer 2, channel 4) for input capture. I'm starting the timer and then applying the signal on the timer input pin. Since I configured the TIM interrupt, the interrupt callbacks do occur.
void HAL_TIM_InputCaptureCallback(hal_tim_handle_t *htim,
hal_tim_channel_t channel) {
if (htim == mx_tim2_gethandle() && channel == HAL_TIM_CHANNEL_4) {
uint32_t value = HAL_TIM_IC_ReadChannelCapturedValue(htim,
HAL_TIM_CHANNEL_4);
#ifdef DEBUG
SWD_printf("ICTIM: %d\n", value);
#endif
}
}
The problem that I am having is that HAL_TIM_IC_ReadChannelCapturedValue always returns 0. The number of callbacks is actually correct (it matches the number of positive and negative edges of the signal). The timer frequency is 10,000Hz (the period is 0.1ms) and the signal pulses are between 2ms and 9ms so the timer is not overflowing. I would expect timer readings to be between 20 and 90.
I attached the timer configuration screenshots.
I wonder what am I doing wrong,
-Gil
Solved! Go to Solution.
2026-04-15 5:49 AM
The problem was that I called HAL_TIM_IC_StartChannel_IT:
HAL_TIM_IC_StartChannel_IT(mx_tim2_gethandle(), HAL_TIM_CHANNEL_4)but I did not call:
HAL_TIM_Start(mx_tim2_gethandle());
The example from TIM input capture Github was very useful.
Sorry for the trouble,
Gil
2026-04-15 5:49 AM
The problem was that I called HAL_TIM_IC_StartChannel_IT:
HAL_TIM_IC_StartChannel_IT(mx_tim2_gethandle(), HAL_TIM_CHANNEL_4)but I did not call:
HAL_TIM_Start(mx_tim2_gethandle());
The example from TIM input capture Github was very useful.
Sorry for the trouble,
Gil
2026-04-15 8:38 AM
Hello,
this thread has been moved to the STM32 MCUs Embedded software forum because it relates only to HAL2, not STM32CubeMX2. This will help improve visibility and ensure the right experts are involved.
Regards,
Sara.