cancel
Showing results for 
Search instead for 
Did you mean: 

Quadrature Encoder

mbfried
Associate II
Posted on November 20, 2009 at 17:22

Quadrature Encoder

5 REPLIES 5
mbfried
Associate II
Posted on May 17, 2011 at 13:31

Hi all. I'm sorry if this message seems repetitive, but I can't seem to solve my issue. I've looked at the other code in the forum, but I don't understand how the GPIO pins are linked to the TIM. Here is the code I am working with.

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 ;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOE, &GPIO_InitStructure);

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_Period = 65535;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

TIM_EncoderInterfaceConfig(TIM2,TIM_EncoderMode_TI12,

TIM_ICPolarity_RisingTIM_ICPolarity_Rising);

TIM_Cmd(TIM2,ENABLE);

while(1)

{

count = TIM2 -> CNT;

}

My count cycles between 0 and 65535. It seems to occur when it should be incrementing, but it cycles instead of increasing/decreasing. I feel like I'm missing a function that tells the TIMx what GPIO pins to look at. Any insight would be greatly appreciated.

swhite
Associate III
Posted on May 17, 2011 at 13:31

Quote:

On 20-11-2009 at 19:13, Anonymous wrote:

TIM_EncoderInterfaceConfig(TIM2,TIM_EncoderMode_TI12, TIM_ICPolarity_RisingTIM_ICPolarity_Rising);

Just so happens I got the code for a rotary encoder working a few hours ago. Mine counts both ways (except I'm using timer 4 with remapped pins--PD12 & PD13).

I think your problem is that for Timer 2 the TIM2_CH1 and TIM2_CH2 pins are PA0 and PA1 respectively (PA15 & PB3 remapped). You're trying to use PE10 (TIM1_CH2N) and PE11 (TIM1_CH2).

mbfried
Associate II
Posted on May 17, 2011 at 13:31

Thanks, that would make sense. I'm using an eval kit, with other components connected through PA0 and PA1, so I found unused ones. Sorry for the newbie question, but can I just remap to PE10 and PE11 to work, or do I have to use PA0 and PA1?

swhite
Associate III
Posted on May 17, 2011 at 13:31

The remaps for PA0/PA1 are PA15/PB3 (as I'd already posted). The trouble with those is that they're shared with the JTAG signals.

I developed/tested my code on an STM3210B-EVAL board and used PD12/13 (remapped) and Timer 4. On that board those pins are already used but for switches which was ok in my case.

mbfried
Associate II
Posted on May 17, 2011 at 13:31

Thanks a ton. You saved me a lot of frustration.