AnsweredAssumed Answered

quadrature encoder

Question asked by Herb on Dec 18, 2011
Latest reply on Dec 19, 2011 by Herb
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 ------------------------------------------------------------------*/
#include "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;
     //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);


* 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--)


  * @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)
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

Thank you,