2013-06-21 01:44 PM
I'm working on getting quadrature encoding working through either TIM1 or TIM8. I searched the past topics on this forum, but I can't tell how mine is different/ why it isn't working. I'm using USART to debug the count, and I'm consistently getting a counter value of 0, though I can tell my input is valid.
Can anyone see what i'm doing wrong in this code? Thanks so much for taking the time to look at it./* * main.c * * Created on: Jun 20, 2013 */&sharpinclude ''main.h''&sharpinclude ''common.h''&sharpinclude ''usart.h''&sharpinclude ''peripherals.h''static void TIM_Config(void);TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;uint32_t a=0;uint16_t i;int main(void){ /* Initialize USART2 */ USART2_Init(115200, TRUE); printf(''\fUART2 initialized\n\r''); TIM_Config(); /* TIM1 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8 , ENABLE); /* Time Base configuration */ TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1; TIM_TimeBaseStructure.TIM_Period = 0xFFFF; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //Configure the encoder interface TIM_EncoderInterfaceConfig(TIM8, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge, TIM_ICPolarity_BothEdge); TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); TIM_ARRPreloadConfig(TIM8, ENABLE); /* TIM1 counter enable */ TIM_Cmd(TIM8, ENABLE); //Counter loop while(1) { printf(''Counter: %d\n\r'', (uint16_t)(TIM1->CNT & 0x0000ffff)); for(i=0; i<30000; i++) { a++; a = a%5; } } return 0;}static void TIM_Config(void){ GPIO_InitTypeDef GPIO_InitStructure; /* GPIOA, GPIOB and GPIOE Clocks enable */ RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOC, ENABLE); //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_2MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_4); //C6 -TIM1_CH1 GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_4); //C7 TIM1_CH2// GPIO_Init(GPIOC, &GPIO_InitStructure);// GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_2); //C6 -TIM1_CH1// GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_2); //C7 TIM1_CH2}&sharpifdef USE_FULL_ASSERT/** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */void assert_failed(uint8_t* file, uint32_t line){ /* User can add his own implementation to report the file name and line number, ex: printf(''Wrong parameters value: file %s on line %d\r\n'', file, line) */ /* Infinite loop */ while (1) { }}&sharpendif #input-capture #quadrature-encoder2013-06-24 09:09 AM
I figured out that I had a bug in my arduino code which is why I only counted up 4. Now I'm having an error where the counter will not count upwards correctly, but will could downwards accurately. I keep getting a variable count up (but around 2x the number of rotations using only one rising edge) but exactly 2x the number of rotations down. Any ideas?
2013-06-24 01:00 PM
Another update: I got the functionality above using TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising
If I use falling, or switch to TIM_EncoderMode_TI2 rising or falling, I get incorrect numbers for both counting upwards and downwards. I have yet to figure out why this could be. I tried switching to TIM 1 and switching the input pins to PC8 and 9 but I didn't get a reading out. I also don't get a reading out for TIM_EncoderMode_T12. Any ideas would be much appreciated!2013-06-25 11:24 AM
Final update: I finally got it working. Really stupid error on my part. My arduino outputs 5 volts, so I set up a voltage divider before attaching it to the discovery board. I assumed that 3 volts would be close enough to 3.3 volts that the board runs on to be sufficient for anything that I needed, but it turns out that 3 V is right on the limit of the board's capabilities, so i would not always catch the rising edge of the encoder. I redid the voltage divider to set it around 3.3 volts and everything works.
Thanks so much for helping!! I definitely needed it.2013-07-27 12:17 PM
Hi,
I'm new to the STM and I just want to configure the quadrature encoder on Timer2 of my stm32f3 discovery board. the programming manual says that two encoder inputs are captured from TI1 and TI2 pins. but i cant find such an alternate function on any pin of the microcontroller. so can anyone tell me which pins i should use to connect the encoder and in which mode of the Timer2?Thank You!2013-07-27 01:06 PM
You'd want to use some uncommitted pins designated for TIM2_CH1 and TIM2_CH2, see Data Sheet/Manual for the specific part involved.
This thread has a TIM2 example at the end.[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/STM32F3%20Discovery%20Counter&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/AllItems.aspx¤tviews=94]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fSTM32Discovery%2fSTM32F3%20Discovery%20Counter&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&TopicsView=https%3A%2F%2Fmy.st.com%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FAllItems.aspx¤tviews=942013-07-27 10:10 PM
Thank you very much 🙂 it helped a lot ^^