cancel
Showing results for 
Search instead for 
Did you mean: 

Input compare trigger interrupt on specific encoder tick value

Adam Davis
Associate II

Hi all.

I have a motor connected to my STM32F7 board and I'm trying to generate an interrupt for every rotation. Each rotation is 2122 ticks but I keep getting an interrupt for every pulse generated from the motor encoders.

My init code is:

static void MX_TIM3_Init(void)
{
 
  /* USER CODE BEGIN TIM3_Init 0 */
	int uwPrescalerValue = 2 * HAL_RCC_GetPCLK1Freq();;
  /* USER CODE END TIM3_Init 0 */
 
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_IC_InitTypeDef sConfigIC = {0};
 
  /* USER CODE BEGIN TIM3_Init 1 */
 
  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 106; // microsecond timer
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 2122 - 1;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_IC_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
  sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
  sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
  sConfigIC.ICFilter = 0;
  if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM3_Init 2 */
  HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);
 
  /* USER CODE END TIM3_Init 2 */
 
}

And interrupt code:

void TIM3_IRQHandler(void)
{
	uint32_t pulse = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_1);
 
	if (pulse == 2122 - 1)
	{
		int s = 0;
	}
	HAL_TIM_IRQHandler(&htim3);
}

Looking at the reference manual, I've tried setting UIE interrupt but TIM3_IRQHandler keeps being triggered. The encoder ticks is counting correctly (hence the IF statement within my interrupt handler). However, I just want this interrupt triggered once per 2122 ticks.

Any help please?

4 REPLIES 4

Read out and check/post the timer registers content.

JW

TDK
Guru

In your IRQ handler, look at TIM3->SR and see why it's getting called. If it's events other than update, disable those interrupts in TIMx->DIER.

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

Fundamentally, is it possible to have a single interrupt once CNT has reached a certain value?

berendi
Principal

> I just want this interrupt triggered once per 2122 ticks.

Configure the timer in external clock mode (ECM1/2), then the counter is incremented once for each incoming pulse. Read the timer functional description / clock selection chapter in the reference manual. There are step-by-step pseudocode examples for both ECM modes, so you don't have to do trial and error black box experiments with CubeMX. Then enable the TIM_DIER_UIE bit.

In the interrupt handler examine and clear the TIM_SR_UIF flag of TIM3->SR, it is much faster than calling HAL_TIM_IRQHandler().