AnsweredAssumed Answered

Quadrature Encoder STM32 Z Capture

Question asked by r.a.001 on Feb 23, 2015
Latest reply on Feb 23, 2015 by r.a.001

I'd like to use an quadrature encoder connected to my STM32F4 Discovery Board.
I have connected the A and B outputs to PC6 and PC7. CH1 and CH2 of TIM8.
It works pretty good, the timer counts.
Now I'd like to use the Z output of the encoder to avoid errors.
I have connected Z to the CH3 of TIM8 (PC8) and configured it as an capture input.
In the capture interrupt I want compare the capture value with the expected value and correct the current TIMx->CNT.

Unfortunately it never enters the TIM8_CC_IRQHandler.

I use this encoder:

I would be very grateful if someone of you would look at my code.

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8; // A, B, Z; CH1, CH2, CH3
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 0x07ff; // Encoder -> 2048 per rotation => 0x07FF = 2047(dez)
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  // Capture Config
  NVIC_InitStructure.NVIC_IRQChannel = TIM8_CC_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannel = ENABLE;
  TIM_ICInitTypeDef TIM_ICInitStructure;
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0x0;
  TIM_ICInit(TIM8, &TIM_ICInitStructure);
  TIM_ClearITPendingBit(TIM8, TIM_IT_CC3);
  // END Capture Config
  TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
  TIM_EncoderInterfaceConfig(TIM8, TIM_EncoderMode_TI2, TIM_ICPolarity_Falling, TIM_ICPolarity_Rising);
void TIM8_CC_IRQHandler (void)
  GPIOD->ODR ^= GPIO_Pin_12; // LED toggle
  GPIOD->ODR ^= GPIO_Pin_11; // Scope control
  if (TIM_GetITStatus(TIM8, TIM_IT_CC3) != RESET)
    // correction
    TIM_ClearITPendingBit(TIM8, TIM_IT_CC3);