cancel
Showing results for 
Search instead for 
Did you mean: 

Quadrature Encoder (STM32F4) counts not working properly at high speeds

kasun_duminda92
Associate III
Posted on March 31, 2014 at 10:45

I am using the TIM1 in the stm32f4 in encoder interface mode to get position. The two chanels of my rotary quadrature encoder are connected to the pins A8 and A9 through a comparator. The outputs from the comparator can be seen as two nice square waves on a scope.

At low speeds the timer counts perfectly well. But at slightly moderate speeds the timer losses a lot of counts and does not work properly. At high speeds it performs very badly. I configured the GPIO pins as such;

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_TIM1);

The timer was configured as such;

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; // Maximal
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM1, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_Cmd(TIM1, ENABLE);

Why is the timer loosing counts? Should I also configure input capture for the two channels of TIM1? What am I doing wrong here? #stm32f4 #discovery #encoder-interface #timers #qei
4 REPLIES 4
Bill Lewis
Associate III
Posted on April 22, 2014 at 19:36

Mine wouldn't work properly even at fairly low speeds, with

GPIO_Speed_2MHz

. I changed to 10MHz and it improved quite a bit. Bill
jpeacock2399
Associate II
Posted on April 22, 2014 at 21:24

Check your digital filter setting:

TIM_ICInitStructure.

TIM_ICFilter

in the timer configuration.  I use rotary optical encoders, single ended on a short cable, at up to 40KHz with a filter setting of 5 and GPIO at 2MHz.  I see attenuation when running at higher filter settings so you may need to check the filter calculations for whatever speed your inputs run at.

  Jack Peacock
gbarbour
Associate III
Posted on April 23, 2014 at 14:46

You might also want to check the hardware side of your setup. If the wires are too long or there is unexpected capacitance or inductance in the circuit the square wave output from the encoder could be turned into spikes too short for the MCU to identify.

Just a thought.

Bill Lewis
Associate III
Posted on April 23, 2014 at 15:01

Any chance you could paste in some code on how you configure this filter?

I'm not using TIM_ICInitTypeDef at all currently.

Bill