AnsweredAssumed Answered

STM32 HAL Nucleo F446RE Quadrature Encoder

Question asked by Max Baeuml on Jan 21, 2018
Latest reply on Jan 23, 2018 by Max Baeuml

i have a problem with the quadrature encoder mode on tim3.

1) tim3 counts on every rising edge on the first signal. the CNT register counts up and i read the value with 1hz and then i set the register to 0.

All looks good but...

When i look on the oscilloscope the frequency is half as big as the value from the CNT register output (1hz).

if i devide the CNT value by two the result is correct.



count on rising edge


2) tim3 counts on both edges on the first signal. the CNT register output (1hz) is completely wrong.

count on bothedge

my config is:

GPIO_InitTypeDef sInitEncoderPin1;
sInitEncoderPin1.Pin                    =;  // A GPIO_PIN_6
sInitEncoderPin1.Mode                   = GPIO_MODE_AF_PP;
sInitEncoderPin1.Pull                   = GPIO_PULLUP;
sInitEncoderPin1.Speed                  = GPIO_SPEED_HIGH;
sInitEncoderPin1.Alternate              = altFunctionEncoder; // GPIO_AF2_TIM3 

GPIO_InitTypeDef sInitEncoderPin2;
sInitEncoderPin2.Pin                    =; // A GPIO_PIN_7
sInitEncoderPin2.Mode                   = GPIO_MODE_AF_PP;
sInitEncoderPin2.Pull                   = GPIO_PULLUP;
sInitEncoderPin2.Speed                  = GPIO_SPEED_HIGH;
sInitEncoderPin2.Alternate              = altFunctionEncoder; // GPIO_AF2_TIM3  

HAL_GPIO_Init(GPIOA, &sInitEncoderPin1);
HAL_GPIO_Init(GPIOA, &sInitEncoderPin2); 

encoderTimer.Init.Period                        = 0xffff;
encoderTimer.Init.Prescaler                     = 0;
encoderTimer.Init.CounterMode                   = TIM_COUNTERMODE_UP;
encoderTimer.Init.ClockDivision                 = TIM_CLOCKDIVISION_DIV1;
encoderTimer.Init.RepetitionCounter             = 0; 

HAL_NVIC_SetPriority(SysTick_IRQn, 0, 1); 

encoder.EncoderMode                             = TIM_ENCODERMODE_TI1;
encoder.IC1Filter                               = 0x0f;
encoder.IC1Prescaler                            = TIM_ICPSC_DIV1;
encoder.IC1Selection                            = TIM_ICSELECTION_DIRECTTI; 
encoder.IC2Filter                               = 0x0f;
encoder.IC2Polarity                             = TIM_INPUTCHANNELPOLARITY_RISING;
encoder.IC2Prescaler                            = TIM_ICPSC_DIV1;
encoder.IC2Selection                            = TIM_ICSELECTION_DIRECTTI; 

HAL_TIM_Encoder_Init(&encoderTimer, &encoder); 
HAL_TIM_Encoder_Start_IT(&encoderTimer, TIM_CHANNEL_ALL);