cancel
Showing results for 
Search instead for 
Did you mean: 

quadrature encoder

stm239955_stm1
Associate
Posted on December 18, 2011 at 01:56

Hi,

I am trying to get the quadrature encoder working.  I found and copied code from these forums for setting it up with a different micro controller but the one on the STM32F4 does not support RCC_APB2Periph_GPIOB or RCC_APB2Periph_AFIO so I cannot use the statement commented out in the code below (the 4th line in main(void)).  Can anybody help me figure out how to get the encoder working?  Here is my code setup from TIM_PWM_Output with the PWM code removed.

 ******************************************************************************

  * @file    TIM_PWM_Output/main.c

  * @author  MCD Application Team

  * @version V1.0.0

  * @date    19-September-2011

  * @brief   Main program body

  ******************************************************************************

  This code replaces TIM_PWM_Output/main.c

  1. Initialize TIM4 as the quadrature encoder

  2. run the program in debug mode

  3. spin the motor shaft that has the encoder

  4. put a breakpoint at the delay in the loop

  5. The value of currentPosition should no longer be zero

*/

/* Includes ------------------------------------------------------------------*/

&sharpinclude ''stm32f4_discovery.h''

uint16_t PrescalerValue = 0;

uint16_t currentPosition;

GPIO_InitTypeDef GPIO_InitStructure;

/* Private function prototypes -----------------------------------------------*/

static void delay(void);

/* Private functions ---------------------------------------------------------*/

int main(void)

{

  /* Compute the prescaler value */

  PrescalerValue = (uint16_t) ((SystemCoreClock /2) / 28000000) - 1;

  //WHAT DO I REPLACE THE FOLLOWING STATEMENT WITH?

     //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

    TIM_SetAutoreload(TIM4, 0xffff);

    TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);

    TIM_Cmd(TIM4, ENABLE);

    while(1) {

        currentPosition = TIM_GetCounter(TIM4);

        delay();

        delay();

        delay();

        delay();

    }

}

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

* Function Name  : delay

* Description    : Inserts a time delay.

* Input          : None

* Output         : None

* Return         : None

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

static void delay(void)

{

  vu32 i = 0;

  for(i = 0xFF; i != 0; i--)

  {

  }

}

&sharpifdef  USE_FULL_ASSERT

/**

  * @brief  Reports the name of the source file and the source line number

  *         where the assert_param error has occurred.

  * @param  file: pointer to the source file name

  * @param  line: assert_param error line source number

  * @retval None

  */

void assert_failed(uint8_t* file, uint32_t line)

{

  /* User can add his own implementation to report the file name and line number,

     ex: printf(''Wrong parameters value: file %s on line %d\r\n'', file, line) */

  while (1)

  {}

}

&sharpendif

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

Thank you,

Herb

#quadrature-encoder
2 REPLIES 2
Posted on December 18, 2011 at 02:59

From \STM32F4-Discovery_FW_V1.1.0\Project\Peripheral_Examples\TIM_PWM_Input

You don't need a clock for the AFIO, as it is now associated with the individual GPIOx units

  /* TIM4 clock enable */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

  /* GPIOB clock enable */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

  /* TIM4 chennel2 configuration : PB.07 */

  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;

  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* Connect TIM pin to AF2 */

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
stm239955_stm1
Associate
Posted on December 19, 2011 at 19:01

Hi,

Any thoughts on how this could be put into my code above so I can use quadrature encoding?  I am having trouble finding documentation on the peripheral library or even from the data sheets on how the timer and GPIOs are to be set up to act as a quadrature encoder.

Thank you,

Herb