Question
Quadrature encoder wont work
Posted on February 14, 2013 at 06:16
Hi All,
I have copied a few different code examples from this forum for quadrature encoder but I just cant seem to get it to work. I have tried on TIM8, TIM3, TIM12 with no luck. The examples I have copied the code from seem to have been resolved and confirmed as working. I thought maybe my encoder is at fault, so tied CH1 to ground and CH2 to +3V6, then vice versa but the CNT doesnt seem to increment - should it? Unfortunately I do not have an oscilloscope to test the encoder. Someone had written that their encoder wasn't working because they had the wrong signal levels - but I guess 3.6V is fine? Any help/thoughts would be greatly appreciated. This is the code I'm using ATM void initEncoder(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStruct; GPIO_InitTypeDef GPIO_InitStructure; //Configure peripheral clocks 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_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; // GPIO_PuPd_DOWN; //GPIO_PuPd_NOPULL; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8); //C6 -TIM8_CH1 GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM8); //C7 TIM8_CH2 //Configure Timer TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_Period = 8191; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); TIM_ARRPreloadConfig(TIM8, ENABLE); //Debounce filter TIM_ICInitStruct.TIM_Channel=TIM_Channel_1; TIM_ICInitStruct.TIM_ICFilter=3; TIM_ICInit(TIM8, &TIM_ICInitStruct); TIM_ICInitStruct.TIM_Channel=TIM_Channel_2; TIM_ICInitStruct.TIM_ICFilter=3; TIM_ICInit(TIM8, &TIM_ICInitStruct); //Setup quadrature encoder and enable timer TIM_EncoderInterfaceConfig(TIM8,TIM_EncoderMode_TI12,TIM_ICPolarity_Falling,TIM_ICPolarity_Falling); TIM_Cmd(TIM8, ENABLE); } #quadrature-encoder #encoder