cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 Rotary Encoder config

modcan
Associate II
Posted on August 22, 2012 at 18:23

I am trying to use timer 8 in Encoder mode and my setup is as follows. Pins PC8 and PC9 (timer8) are connected to a cheap 3 terminal encoder that has C pin grounded so pullups are activated on these pins.

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOC, &GPIO_InitStructure); RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); TIM_TimeBaseStructure.TIM_Period = 0x0FFF; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_EncoderInterfaceConfig(TIM8,TIM_EncoderMode_TI1,TIM_ICPolarity_Falling, TIM_ICPolarity_Falling); TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); TIM_Cmd(TIM8, ENABLE); 

I then read the counter with temp=TIM_GetCounter(TIM8);

I have tried all the possible modes and falling/rising combos I can and am getting nothing. Does the Timer channel have any effect. It is using channels 3 and 4

I assume that there doesn't need to be any interrupts setup but maybe this is incorrect.

Any suggestions would be appreciated. Thanks

7 REPLIES 7
Posted on August 22, 2012 at 19:05

On what part exactly? ST makes dozens of STM32xxx parts. Some will require AF pin routing to be specified.

How does CH3/4 route to TI1FP1 or TI2FP2, refer to ''timer block diagram''

0690X0000060MnlQAE.gif

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
modcan
Associate II
Posted on August 22, 2012 at 19:24

modcan
Associate II
Posted on August 22, 2012 at 20:29

Ok maybe that's the issue. I am using STM32F407VGT6 part and it looks like PC8 and PC9s alternative function is TIMer channels 3 and 4 so it won't connect to channels 1 and 2 that are needed for the encoder interface. I wish I had looked at that diagram more closely when laying out my board. I am assuming I can't use the AF function to connect to another channel if it is not available in the AF list for that pin. (I don't have a complete grasp on the AF feature yet.) Is it true that one needs to define AF functions for any pin that is being used other than as a simple GPIO? For example if using SPI I need to set up port using AF assignments?

Posted on August 22, 2012 at 20:58

For non-GPIO function the pin drivers need to be connected to the appropriate peripheral using the AF pin multiplexor.

This isn't a cross-bar switch, so the specific pins, function and peripheral selection is limited. Refer to the ''Alternate Function Mapping'' table in the part datasheet. Pg 58

http://www.st.com/internet/mcu/product/252jsp

http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/DM00037pdf

/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); // USART2_TX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); // USART2_RX

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
modcan
Associate II
Posted on August 23, 2012 at 21:00

Thanks for the tip Clive. I had mistakenly assumed that peripherals could be remapped. Always something new to learn. BTW where did you find that diagram for timers you posted? I can't see it in the RM0090 reference.

Thanks

modcan
Associate II
Posted on August 23, 2012 at 21:03

Ok never mind I found it.

Thanks

Posted on August 23, 2012 at 21:48

I pulled it out of RM0033, for the STM32F2 as most STM32Fx parts share a similar construction, and I had it open already.

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