Showing results for 
Search instead for 
Did you mean: 

Issue in Input capture for overflow condition.

Associate II

Good morning community,
I am capturing the frequency using waveform generator in STM32.
Board: STM32F401RE Nucleo.
It is working at underflow condition.
Condition is,
Reference clock is 25MHz and counter period is 65535. So, minimum frequency is 382Hz and below 381Hz it is not working.

It is not working as per my overflow condition. 

Here is my reference code: 


void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim1)
  if (! is_capture_done)
    if (count == 1)
      input_captures[0] = __HAL_TIM_GET_COMPARE(htim1, TIM_CHANNEL_1);
      input_captures[1] = __HAL_TIM_GET_COMPARE(htim1, TIM_CHANNEL_1);
      count = 1;
      is_capture_done = 1;


And main function code is:


  while (1)

    /* USER CODE BEGIN 3 */
	  if(is_capture_done == 1)
	  			  if (input_captures[1] > input_captures[0])
	  				capture_difference = input_captures[1] - input_captures[0];
	  			  else if (input_captures[1] < input_captures[0])
	  				capture_difference = ((0XFFFF - input_captures[0]) + input_captures[1]) + 1;
	  			  // calculate the frequency by capture difference
	  			  float refClock = (HAL_RCC_GetPCLK2Freq());
	  			  timer2_cnt_freq = refClock / capture_difference;
	  			  is_capture_done = 0;


I am telling that elseif part can handle the overflow condition? If not, then how can I handle it? And what is minimum frequency that can be handle by controller?
Here I am sharing my project for your reference.


Enable Update interrupts for given timer, and in its handler count the number of Updates (i.e. overflows of the timer).

In the handler for Capture, extend the captured value by the number of overflows into a 32-bit total value. You have to be very careful with the corner cases, i.e. if capture happens close to the overflow. I don't think you can use the Cube's "callbacks" to solve this, and it's a genuinely nontrivial problem.

It's simpler and in many ways better to use a suitable prescaler (decreasing resolution in exchange for increasing range); or use a 32-bit timer such as TIM2 or TIM5.





When you repost, consider that you're addressing the same community with each post. If you're not getting satisfactory results, consider looking at why and possibly improving your question.


> Reference clock is 25MHz and counter period is 65535. So, minimum frequency is 382Hz and below 381Hz it is not working.

If your timer update frequency is 382 Hz, you will not be able to capture input frequencies below this effectively.

If you need to capture slower frequencies, increase the timer prescaler or slow down the timer by some other means.

Consider using a 32-bit timer like TIM2 or TIM5 instead.

If you feel a post has answered your question, please click "Accept as Solution".