Skip to main content
Visitor II
February 6, 2024
Solved

STM32F407G-DISC1 Timer2 Channel 1 as Input Capture not working

  • February 6, 2024
  • 3 replies
  • 1138 views

Hello,

I have set Timer2 Channel 1 as input capture for STM32F407G-DISC1 board and generated a code with STM32CobeMX.

Source code link: stm32-intro/timer2_general_input/Core/Src/main_app.c at 5d95347ebcc34a3142ddb65ea15c5016199e8dca · freecode23/stm32-intro · GitHub

1) In generated code, why am I getting HAL_TIM_Base_MspInit instead of HAL_TIM_IC_MspInit ?

2) In my main code I have to start the base timer then only IC callback is working.

3) Code always remains in callback and never come out of it, even it is not going into the while loop after starting the timer.

Thanks!

This topic has been closed for replies.
Best answer by waclawek.jan

From your program on github:

 

 

/**
 * Callback when interrupt is triggered in input capture of timer2.
 */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) {
//	logUART("\ncount is=%d\r\n", count);

	if (!is_capture_done) {
		if (count == 1) {
			input_captures[0] = __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_1);
			count++;
			logUART("\ncount == 1; count++%d\r\n", count);

		} else if (count == 2) {
			input_captures[1] = __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_1);
			count = 1;
			is_capture_done = TRUE;
			logUART("\ncount == 2; capture is done\r\n");
		}
	}
}

 

 

 

Never print (logUART()) in interrupt.

JW

3 replies

mƎALLEm
ST Technical Moderator
February 6, 2024

Hello,


3) Code always remains in callback and never come out of it, even it is not going into the while loop after starting the timer.

 


It could be due to the frequency of the signal you are measuring. So the CPU is overloaded by executing the interrupt.

I invite you to refer to the example provided in STM32CubeF4 located under Projects\STM324xG_EVAL\Examples\TIM\TIM_InputCapture

You can even share your project including .ioc file to let community members to help you efficiently.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
waclawek.jan
waclawek.janBest answer
Super User
February 6, 2024

From your program on github:

 

 

/**
 * Callback when interrupt is triggered in input capture of timer2.
 */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) {
//	logUART("\ncount is=%d\r\n", count);

	if (!is_capture_done) {
		if (count == 1) {
			input_captures[0] = __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_1);
			count++;
			logUART("\ncount == 1; count++%d\r\n", count);

		} else if (count == 2) {
			input_captures[1] = __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_1);
			count = 1;
			is_capture_done = TRUE;
			logUART("\ncount == 2; capture is done\r\n");
		}
	}
}

 

 

 

Never print (logUART()) in interrupt.

JW

Tesla DeLorean
Guru
February 6, 2024

Using long-term blocking functions in interrupt or callback context is never a good plan.

Use some setting or toggling GPIO/LEDs that can be set immediately and leave. High frequency toggling might be beyond visual comprehension, could use two LEDs with inverse phase, or use a scope.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..