cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4: In encoder mode, TIM2 counts only in one direction

jiss
Associate
Posted on September 10, 2014 at 08:54

Hi All,

I have a quadrature encoder interfaced to my STM32F427 using TIM2.

In encoder mode, TIM2 counts up in one direction of the motor but does not count up/down in the other direction.

I am adding my init code below, which I think is enough to get the encoder running (After going through STM32F4xx_StdPeriph_Driver and the forum here).

Notes:

1) I have verified the A and B channels of the enocder going into STM32F4. They are proper 90 degree out of phase with leading or lagging for each direction respectively.

2) In one direction, TIM2 counts up from 0 and the count values are as expected.

3) In the other direction, TIM2 count just oscillates between 0x00000000 and 0xFFFFFFFF (And sometimes gives the value of 0x00000001).

4) In the properly counting direction (2 above), I could see that DIR bit in TIM2->CR1 is stable at 0.

5) In the non-counting direction (3 above), DIR bit is unstable and oscillates between 1 and 0.

/*****************************************************************/

    // Port A Configuration

    // GPIOA Periph clock enable

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);    

    // Setup GPIO for ENC

    GPIO_InitStructure.GPIO_Pin =

        GPIO_Pin_0 |        // ENC1_CH1

        GPIO_Pin_1 |        // ENC1_CH2

        GPIO_Pin_6 |        // ENC2_CH1

        GPIO_Pin_7;         // ENC2_CH2

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // Setup TIM2

    TIM_DeInit(TIM2);

    // Enable TIM2 clock

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    // Configure GPIO pin alternate function

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2);

    // Configure GPIO pin alternate function

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);

    // Configure TIM2 to read quadrature encoder

    TIM_EncoderInterfaceConfig( TIM2, TIM_EncoderMode_TI12,

                                   TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);

    // Setup TimeBase for TIM2 (default values for now)

    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

    TIM_TimeBaseInitStruct.TIM_Period = 0xFFFFFFFF;

    TIM_TimeBaseInitStruct.TIM_Prescaler = 0x0000;

    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;

    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0x0000;

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);

    // Enable TIM2

    TIM_Cmd(TIM2, ENABLE);

    TIM_SetCounter( TIM2, 0x00000000 );

/********************************************************************/

Any inputs or pointers for understanding this behavior will be really appreciated. Expecting your co-operation.

Thanks

#stm32f4 #timer #encoder #confused!
2 REPLIES 2
jiss
Associate
Posted on September 10, 2014 at 09:00

JFYI:

1) I am using STM32F4xx Standard Peripheral Library Version V1.2.0RC2/20-February-2013

2) My encoder output is 5V which is given to PA0 and PA1 pins.
yucel
Associate
Posted on September 18, 2014 at 22:00

Hi, Joseph,

I have the same problem with STM32F427 TIM2 or TIM5.

Do you find any solution?

Edit:

Our problem is from hardware. if one of the channel is not connected or configured then it always counts up or down. Also as seen from the reference guide you must use channel 1&2.Channel 3&4 can not be used.  

Regards.