cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F1 encoder problem

michaelrst
Associate II
Posted on January 09, 2014 at 09:56

I have a board with an STM32F100 and a quadrature encoder connected to TIM1 channels 1 and 2 PA8 and PA9, the index output is connected to GPIO pin. I have checked the input from the encoder and it is generating the correct pulses. I have set the gpio pins to GPIO_Mode_AF_OD. Unfortunately the timer is not shifting from 0. The index pulse interrupt is working perfectly.

The pins are intitalised thus:

/// @brief encoder channel A input

const GPIOsignal encoderChannelA_port(gpio_mode_GPIO, GPIO_Pin_9, GPIOA, RCC_APB2Periph_GPIOA, GPIO_Mode_AF_OD, GPIO_Speed_10MHz);

/// @brief encoder channel B input

const GPIOsignal encoderChannelB_port(gpio_mode_GPIO, GPIO_Pin_8, GPIOA, RCC_APB2Periph_GPIOA, GPIO_Mode_AF_OD, GPIO_Speed_10MHz);

The actual initialisation code is elsewhere, but it is thoroughly tested.

The timer is intialised thus:

void setupTimer1()

{

  TIM_DeInit(TIM1);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

  TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);

  TIM_TimeBaseInitStruct.TIM_Period = uint16_t(encoderStepsPerRevolution - 1);

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct);

  TIM_Cmd(TIM1, DISABLE);

  TIM_EncoderInterfaceConfig(TIM1, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);

}

 The encoder object is intialised thus:

void encoderClass::initialise(void)

{

  zeroPosition = 0;

  lastZeroCorrection = 0;

  active = false;

  setupTimer1();

  TIM_Cmd(TIM1, ENABLE);

  TIM_ClearFlag(TIM1, TIM_FLAG_Update);

// Check to see if moving changes the encoder position. If not, we assume the encoder is inactive

  if (motor != 0)

  {

    float encoderPos = getPosition();

    float motorPos = motor->currentPosition;

    motor->moveToPosition(motorPos + 3 * encoderStepSize);

    if (getPosition() != encoderPos)

    {

      active = true;

    }

  }

}

Any help would be much appreciated.

Michael Taylor

#stm32f1-encoder-quadrature-timer
3 REPLIES 3
Posted on January 09, 2014 at 12:23

For the F1 series are you sure GPIO_Mode_AF_OD is appropriate for an INPUT? Surely you'd want GPIO_Mode_IN_FLOATING?

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
michaelrst
Associate II
Posted on January 10, 2014 at 09:57

But then how do I set it to the AF? 

OK I tried it and it works. But why for the SPI do I have to set it to AF_PP even for the input pins? The manuals are very unclear on this.

thanks

michael

Posted on January 10, 2014 at 12:42

The AF muxing is a bit obtuse on the F1series parts.

Inputs just come from the same schmitt trigger, outputs you have to select if the data source is a GPIO ODR bit, or comes from a peripheral.

The USART would be a better example to look at.

The SPI is more complicated because the in/out nature of the pin is determined if the SPI is enabled as a Master or Slave, and consequently the control signals to the pin driver are fuller. ie at least 3 signals routed instead of one. You still get to determine OD or PP mode.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..