cancel
Showing results for 
Search instead for 
Did you mean: 

Encoder Interface on TIM2-TIM5

Atay.Stefan
Associate II
Posted on November 20, 2012 at 15:48

Hello,

I am having difficulty configuring the quadrature encoder interface on timers 2 through 5.  I have successfully configured the encoder interface on timers 1 and 8, using the code below:

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_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); 

However, when I try to use the same code to configure the quadrature encoder interface using timer 3 (or 4), I am unable to do so.  Specifically, I used the exact code shown below.  It is identical to the code above, with the following exceptions:

1) Timer 3 is used instead of timer 8

2) Timer 3 requires use of the APB1 clock instead of the APB2 clock

Also, note that TIM8_CH1 and TIM8_CH2 are mapped to pins C6 and C7, respectively.  Likewise, TIM3_CH1 and TIM3_CH2 are also mapped to C6 and C7, respectively.

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

TIM_ICInitTypeDef TIM_ICInitStruct;

GPIO_InitTypeDef  GPIO_InitStructure;

//Configure peripheral clocks

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, 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_NOPULL ;

GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);  //C6 - TIM3_CH1

GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);  //C7 - TIM3_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(TIM3, &TIM_TimeBaseStructure);

TIM_ARRPreloadConfig(TIM3, ENABLE);

//Debounce filter

TIM_ICInitStruct.TIM_Channel=TIM_Channel_1;

TIM_ICInitStruct.TIM_ICFilter=3;

TIM_ICInit(TIM3, &TIM_ICInitStruct);

TIM_ICInitStruct.TIM_Channel=TIM_Channel_2;

TIM_ICInitStruct.TIM_ICFilter=3;

TIM_ICInit(TIM3, &TIM_ICInitStruct);

//Setup quadrature encoder and enable timer

TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_Falling,TIM_ICPolarity_Falling);

TIM_Cmd(TIM3, ENABLE);

It is not clear to me why the code above does not successfully configure the encoder interface module for timer 3.  I would appreciate any help in determining what my errors are.  Thank you!

15 REPLIES 15
Posted on November 20, 2012 at 16:30

Presumably not TIM8 and TIM3 on the same pins at the same time?

I might start by looking at the extended fields of TIM_ICInitStruct that aren't being initialized.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Atay.Stefan
Associate II
Posted on November 20, 2012 at 17:25

Clive,

Thanks for the reply. 

Correct, TIM8 and TIM3 are not on the same pins at the same time.  I went ahead and initialized the remaining fields of TIM_ICInitStruct as follows:

TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Falling;

TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;

TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;

I did this for both timer 8 and timer 3.  Once again, the encoder was operational when using timer 8 but not when using timer 3.  I also went ahead and removed the input capture initialization entirely and got the same result.  That is, it worked with timer 8 but not with timer 3. 

I appreciate your response, any other thoughts?  Thanks!

Posted on November 20, 2012 at 19:45

Also puzzled. Code looks reasonable, and no documentation, or source, suggesting encoding doesn't work outside extended TIM1/8.

Grasping for other things, perhaps look at APB1 clock rate being less than 42 MHz

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
M0NKA
Senior
Posted on November 22, 2012 at 09:40

Hi,

What is the advantage of using the timers for interfacing an encoder ? I currently have

2 encoders and 4 GPIO interrupts to read them.

Regards, Chris
Posted on November 22, 2012 at 12:58

What is the advantage of using the timers for interfacing an encoder?

Speed, and processor loading.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
M0NKA
Senior
Posted on November 22, 2012 at 13:41

Thanks, that makes sense. I will dig more and will try to implement.

BR

martinstingl
Associate II
Posted on February 08, 2013 at 12:26

@atay.stefan:

Have you finally found the reason why the encoder was working only with timers 1 and 8?

Regards,

Martin

ahmed2399
Associate II
Posted on February 12, 2013 at 21:37

ara you slove your problem? I had successfully made my encoders work.

If you have some problem I can give help :)

Could you please write your code initialisation?

Thank

Best regards

Fabio