2012-11-20 06:48 AM
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!2012-11-20 07:30 AM
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.2012-11-20 08:25 AM
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!2012-11-20 10:45 AM
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 MHz2012-11-22 12:40 AM
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, Chris2012-11-22 03:58 AM
What is the advantage of using the timers for interfacing an encoder?
Speed, and processor loading.2012-11-22 04:41 AM
Thanks, that makes sense. I will dig more and will try to implement.
BR2013-02-08 03:26 AM
@atay.stefan:
Have you finally found the reason why the encoder was working only with timers 1 and 8? Regards, Martin2013-02-12 12:37 PM
ara you slove your problem? I had successfully made my encoders work.
If you have some problem I can give help :)2018-11-23 02:55 PM
Could you please write your code initialisation?
Thank
Best regards
Fabio