Skip to main content
stefanskos9
Associate III
February 14, 2013
Question

Quadrature encoder wont work

  • February 14, 2013
  • 12 replies
  • 2873 views
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
This topic has been closed for replies.

12 replies

ismimusyafani
Associate II
September 9, 2015
Posted on September 09, 2015 at 18:09

Hello my name is musyafani,

why my TIM9 in STM32F407 dixcovery not work.

are the config is wrong ?

plesae help me. This is my config

void Rotary9Config(void){

    //enable clock rotary

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE);    

    

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    //connect TIM  ke GPIO

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_TIM9);   //TIM2_CH1

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_TIM9);    //TIM2_CH2

    

    //Debounce filter

    TIM_ICInitStruct.TIM_Channel=TIM_Channel_1;

    TIM_ICInitStruct.TIM_ICFilter=0x0F;

    TIM_ICInitStruct.TIM_ICPrescaler=TIM_ICPSC_DIV1;

    TIM_ICInit(TIM9, &TIM_ICInitStruct);

    TIM_ICInitStruct.TIM_Channel=TIM_Channel_2;

    TIM_ICInitStruct.TIM_ICFilter=0x0F;

    TIM_ICInitStruct.TIM_ICPrescaler=TIM_ICPSC_DIV1;

    TIM_ICInit(TIM9, &TIM_ICInitStruct);

  

   TIM_SetAutoreload (TIM9, 0xffff);

 

    //mode counter

    TIM_EncoderInterfaceConfig(TIM9, TIM_EncoderMode_TI12, TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);

      

    

    //enable counter

    TIM_Cmd(TIM9, ENABLE);

}
Tesla DeLorean
Guru
September 9, 2015
Posted on September 09, 2015 at 18:33

Pretty sure TIM9 doesn't support encoder mode, you should carefully review the reference manual.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..