cancel
Showing results for 
Search instead for 
Did you mean: 

Quadrature encoder wont work

stefanskos9
Associate II
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
12 REPLIES 12
hrobinson
Associate II
Posted on February 26, 2013 at 11:48

Yes, I can see how you might get confused, let me help... it's just the words... A quadrature *encoder* is a device that generates waveforms that behave like sin and cos; usually digital waveforms, in the present context, but not always.  The encoder may for example be a thing an inch or two across that fits on the end of a shaft, so as to measure its rotation; it might give you eg 3600 pulses per rotation, which when you divide it up into the individual phases (four phases per pulse cycle) would give you measurement of shaft rotation to 1/40 of a degree.

The corresponding *decoder* is what you feed the pulses into.  In this case, it's the pair of pins and ''timer'' configured for encoder signals.

The problem is that people quite often call all of this, both the encoder and decoder, ''encoder''.  Right or wrong, that's what people call it.  So when you look in the MCU reference and it's referred to as an ''encoder interface'', it just means ''an interface which will accept the signals from an encoder''.

ismimusyafani
Associate II
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);

}
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
Up vote any posts that you find helpful, it shows what's working..