AnsweredAssumed Answered

Encoder Interface on TIM2-TIM5

Question asked by atay.stefan on Nov 20, 2012
Latest reply on Feb 12, 2013 by wolf_man
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!

Outcomes