AnsweredAssumed Answered

STM32F3 Discovery Quadrature Encoding

Question asked by fox.matthew on Jun 21, 2013
Latest reply on Jul 28, 2013 by MaRiSiPāLa
I'm working on getting quadrature encoding working through either TIM1 or TIM8.  I searched the past topics on this forum, but I can't tell how mine is different/ why it isn't working.  I'm using USART to debug the count, and I'm consistently getting a counter value of 0, though I can tell my input is valid.  

Can anyone see what i'm doing wrong in this code?  Thanks so much for taking the time to look at it.

 * main.c
 *  Created on: Jun 20, 2013

#include "main.h"
#include "common.h"
#include "usart.h"
#include "peripherals.h"

static void TIM_Config(void);

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
uint32_t a=0;
uint16_t i;

int main(void)

     /* Initialize USART2 */
     USART2_Init(115200, TRUE);
     printf("\fUART2 initialized\n\r");


     /* TIM1 clock enable */
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8 , ENABLE);

     /* Time Base configuration */
     TIM_TimeBaseStructure.TIM_Prescaler = 0;
     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
     TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
     TIM_TimeBaseStructure.TIM_ClockDivision = 0;
     TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

     //Configure the encoder interface
     TIM_EncoderInterfaceConfig(TIM8, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge, TIM_ICPolarity_BothEdge);

     TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
     TIM_ARRPreloadConfig(TIM8, ENABLE);

     /* TIM1 counter enable */
     TIM_Cmd(TIM8, ENABLE);

     //Counter loop

          printf("Counter: %d\n\r", (uint16_t)(TIM1->CNT & 0x0000ffff));
          for(i=0; i<30000; i++)
               a = a%5;


     return 0;

static void TIM_Config(void)
     GPIO_InitTypeDef GPIO_InitStructure;

     /* GPIOA, GPIOB and GPIOE Clocks enable */
     RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOC, ENABLE);
     //RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
     //RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

     //Configure pins
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

     GPIO_Init(GPIOC, &GPIO_InitStructure);
     GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_4);  //C6 -TIM1_CH1
     GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_4);  //C7  TIM1_CH2
//     GPIO_Init(GPIOC, &GPIO_InitStructure);
//     GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_2);  //C6 -TIM1_CH1
//     GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_2);  //C7  TIM1_CH2



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

     /* Infinite loop */
     while (1)