0
votes

I try to interface a rotary encoder with STM32F7. A timer (TIM1) should count a value between 0-100 and trigger an interrupt every time the value is incremented or decremented.

The counting works but I only can configure an interrupt on every update event. (if counter overflows/underflows)

How can I configure such a timer?

enter image description here

static void MX_TIM1_Init(void)
{

  TIM_Encoder_InitTypeDef sConfig;
  TIM_MasterConfigTypeDef sMasterConfig;

  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 0;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 99;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
  sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
  sConfig.IC1Filter = 0;
  sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
  sConfig.IC2Filter = 0;
  if (HAL_TIM_Encoder_Init(&htim1, &sConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}
3

3 Answers

0
votes

You can use Edge Detector Trigger TI1F_ED Example for STM32F030F4P6:

/* Trigger Edge Detector */
/* 100: TI1 Edge Detector (TI1F_ED) */
TIM3->SMCR &= ~(TIM_SMCR_TS_0 | TIM_SMCR_TS_1);
TIM3->SMCR |= TIM_SMCR_TS_2;
/* 1: Trigger interrupt enabled. */
TIM3->DIER |= TIM_DIER_TIE;
NVIC_EnableIRQ(TIM3_IRQn);

void TIM3_IRQHandler(void){
    if(TIM3->SR & TIM_SR_TIF){
        
    /* Here code */
    
        /* Interrupt enabled */
        TIM3->SR &= ~TIM_SR_TIF;
    }
}

More infromation: https://www.youtube.com/watch?v=1CPk9UD4440 https://cxemka.com/71-podklyuchenie-enkodera-dupa-k-stm32-cmsis-i-hal.html

-1
votes

I believe that the capture interrupt mechanism would work in encoder mode, so setting TIM1->DIER |= TIM_DIER_CC1IE should generate an interrupt every time the counter is changing.

If the above doesn't work, then set up an EXTI interrupt on the other input pin, i.e. the one mapped to channel 2, which is not changing the counter directly. This way you would get a stable reading of the counter value.

-2
votes

It is not possible directly - but you can set the PWM with and then set the EXTI interrupt on the that line.