cancel
Showing results for 
Search instead for 
Did you mean: 

What HAL callbacks are associated with rising/falling edges on TIM_CHx inputs?

gdzilla
Associate II

I'm using an STM32G4 to measure a pulse width, and wanted to set up TIM1_CH1 for rising edges and TIM1_CH2 for falling edges. My plan was to read the timer counter and subtract to obtain pulse width, and reset the counter on rising edges... I've got HAL_TIM_IC_CaptureCallback() running, but am not certain how to tell what event has caused the interrupt... I realize this is a newbie question - appreciate any pointers to the right application notes or documentation too. I'd also be interested in using DMA if there are methods to speed this up.

1 ACCEPTED SOLUTION

Accepted Solutions
raptorhal2
Lead

Use something like this in the callback?

 if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)

View solution in original post

8 REPLIES 8
raptorhal2
Lead

Use something like this in the callback?

 if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)

AScha.3
Chief III

to measure puls width - why not use the matching timer mode for this ?

i use it to get hi and lo time in INT :

HAL_TIM_IC_Start_IT(&htim5, TIM_CHANNEL_2);					// IR remote in start

0693W00000bhLepQAE.png

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

Thanks for this - playing with it this morning... FWIW, the auto-complete in Cube IDE only showed me "ChanneINState" and "ChannelIState" and I assumed that was all I had to work with. I set up the following, and now see rising edges, but never falling edges... PC0 and PC1 are tied together

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef* htim)
{
	HAL_GPIO_TogglePin(LD2_GPIO_Port,GPIO_PIN_5);
	printf("ISR: ");
	// falling edge on channel 2
	if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
	{
		printf("falling edge\n");
	}
	// rising edge on channel 1
	if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
	{
		printf("rising edge\n");
	}
}

I'm just approaching it how I'd done in other chips in the past - no reason in particular... I haven't used matching mode before - I'll check this out, thanks!

fixed my own problem... it helps to start the channel before expecting it to work ; )

raptorhal2
Lead

To answer perhaps why you see only one edge, did you set the edges differently, as in these useful defines?

#define TIM1CH1_ICPOLARITY            TIM_ICPOLARITY_RISING

#define TIM1CH2_ICPOLARITY            TIM_ICPOLARITY_FALLING

Yeah, that was just me being stupid - I never started channel 2, so wasn't seeing it fire.

However I'm now looking at match mode and am not quite certain how to set this up... I've set it up to use CH1 for input capture, rising edge (as you pictured), and CH2 is indirect, but where and how do I calculate the pulsewidth?

I was assuming CH1 rising edge would reset the timer, and CH2 falling edge would allow me to read the timer value. That makes sense, but I don't see how to accomplish that using the setup you've shown me... what exactly am I doing in the interrupt? And am I still using void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef* htim)?

ok, i show you, how it is to set (i needed about 4 hours to find out...)

0693W00000bhM9nQAE.pngthen you get: - in INT -

void TIM5_IRQHandler(void)
{
  /* USER CODE BEGIN TIM5_IRQn 0 */
	static uint16_t pnr, cmdnr, IRpuls[15],IRcmd[5];
 
	uint16_t val=TIM5->CCR2;							// get puls in microsec. (3000us max. timer ARR )

ccr2 is puls hi-time in us ; timer clk is divided to 1 MHz, so 1 tick = 1 us;

timer ARR is max. time, 3000 us here, to avoid too long captures on error spikes.

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