cancel
Showing results for 
Search instead for 
Did you mean: 

Generate a single interrupt on TIM3_IRQHandler() when CCR is reached instead of every rising pulse

Adam Davis
Associate II

Hi all.

I have a motor encoder connected to TIM3 and I'm trying to generate a single interrupt once

1848 ticks have been reached. However, TIM3_IRQHandler() keeps being triggered for every rising edge of the pulse. I just want a single interrupt triggered. once 1848 ticks have been reached. I could check for the CCR flag within the TIM3_IRQHandler(), however, the constant interrupting of TIM3_IRQHandler() will hinder my performance.

This is my initialising routine

uwPrescalerValue = 62640;
	
	/* USER CODE END TIM3_Init 1 */
	htim3.Instance = TIM3;
	htim3.Init.Prescaler = uwPrescalerValue;
	htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
	htim3.Init.Period = 1848 - 1;
	htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
	htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
	
	sConfig.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
	sConfig.ICSelection = TIM_ICSELECTION_DIRECTTI;
	sConfig.ICPrescaler = TIM_ICPSC_DIV8;
	sConfig.ICFilter = 0;
	
	HAL_TIM_IC_Init(&htim3);
	
	if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfig, TIM_CHANNEL_1) != HAL_OK)
	{
	Error_Handler();
	}
	
	HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);

Any help would be great and some code examples please. Thanks

2 REPLIES 2
Adam Davis
Associate II

I'm starting to think the interrupt vector to trigger upon a capture and compare is TIM1_CC_IRQHandler(). However, timer 3 doesn't have a dedicated interrupt handler, so I guess I have no other choice other than checking for the capture compate interrupt within my TIM3_IRQHandler() interrupt vector ?

First, always start with saying what STM32 model you use. Timers are similar, but there may be differences; and for example interrupts differ between families.

> I have a motor encoder connected to TIM3

So you have the connected the encoder's two outputs to TIM3_CH1 and TIM3_CH2; and TIM3 is set to one of the three encoder modes in the slave-mode controller (TIMx_SMCR.SMS) correct?

So then you certainly don't want to have a nonzero TIMx_PSC prescaler, and you also don't want to prescale any channel's input.

I don't speak the Cube/HAL gibberish, but you also don't want to enable the channel 1 or channel 2 interrupt - as they are set as inputs and probably capture is enabled, they will fire at every pulse. This is the interrupt enabled by HAL_TIM_IC_Start_IT(). Instead, you want to enable the update interrupt, which is triggered when the counter "crosses zero" (maybe HAL_TIM_Base_Start_IT() instead of what you may have already HAL_TIM_Base_Start()).

Read the reference manual.

JW