[SOLVED] STM32G431 Timer 2 Encoder Mode Only Counts One Direction - Page 1
Forum section: electronics / general

author: Ground_Loop
author_rank: Frequent Contributor
timestamp: November 20, 2023, 10:04:20 pm
content: I'm trying to move into the STM world and have been able to get familiar with Cube, PRG, and MX. Also been able to get examples of most peripherals operating. I'm stuck on setting up a timer as a quadrature encoder counter. The micro is a STM32G431CBU6. I have an optical quadrature encoder attached with phase A to IN1 and phase B to IN2 with timer 2 set up as follows:Code:htim2.Instance = TIM2;htim2.Init.Prescaler = 1;htim2.Init.CounterMode = TIM_COUNTERMODE_UP;htim2.Init.Period = 60;htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;sConfig.EncoderMode = TIM_ENCODERMODE_TI12;sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;sConfig.IC1Prescaler = TIM_ICPSC_DIV1;sConfig.IC1Filter = 10;sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;sConfig.IC2Prescaler = TIM_ICPSC_DIV1;sConfig.IC2Filter = 10;I have it set up to roll over at 60 counts which works. I've tried just about every iteration I can and no matter what I do it will only count in one direction. On line searches for examples have not helped. What's wrong?

author: langwadt
author_rank: Super Contributor
timestamp: November 20, 2023, 10:20:28 pm
content: try sConfig.IC2Polarity = TIM_ICPOLARITY_FALLING;

author: Ground_Loop
author_rank: Frequent Contributor
timestamp: November 20, 2023, 11:54:49 pm
content: Quote from: langwadt on November 20, 2023, 10:20:28 pmNo joy. Now it only counts down.Edit...If I start the encoder in one direction after power up it only counts up. If I start the encoder in the other direction after power up it only counts down. For some reason it's latching in a count direction based on initial encoder direction.

author: langwadt
author_rank: Super Contributor
timestamp: November 21, 2023, 12:35:51 am
content: Quote from: Ground_Loop on November 20, 2023, 11:54:49 pmone of the phases not connected?

author: Ground_Loop
author_rank: Frequent Contributor
timestamp: November 21, 2023, 01:12:02 am
content: Solid connection both phases. Monitoring both at the pin with oscope. Further, it will not count at all with one disconnected.

author: langwadt
author_rank: Super Contributor
timestamp: November 21, 2023, 01:27:46 am
content: I don't know if the G431 timers are the same as the F411 timers, but I know this works for four quadrature encoders on an F411https://github.com/langwadt/nucleo_quad4

author: Ground_Loop
author_rank: Frequent Contributor
timestamp: November 21, 2023, 02:56:22 am
content: Quote from: langwadt on November 21, 2023, 01:27:46 amThanks for the link but seems a lot of fuss for something that should work entirely in the peripheral.

author: Chalcogenide
author_rank: Regular Contributor
timestamp: November 21, 2023, 07:10:32 am
content: Dumb question: how do you start the timer? The config looks fine to me, so maybe something is wrong there.

author: ace1903
author_rank: Regular Contributor
timestamp: November 21, 2023, 07:49:07 am
content: htim2.Init.CounterMode = TIM_COUNTERMODE_UP;I don't have cubeide on phone but this doesn't seem right.If I remember well it needs to be something like TIM_COUNTERMODE_UPDOWN

author: wek
author_rank: Frequent Contributor
timestamp: November 21, 2023, 08:25:50 am
content: Always start debugging with reading out and checking relevant registers content, and in such questions, post them.> htim2.Init.Prescaler = 1;That'scertainly wrong, but I'm not sure if it can cause the symptoms described. But then, 'G4's timer is much more complicated than those in the "classic" STM32, and it also has 2 more distinct encoder modes (variants of clock+direction). I don't understand the Cube gobbledygook so that's why it's important to look at the registers (here, mainly TIMx_SMCR). That could also reveal problems caused byincompletely initialized initialization structs.JW

author: Ground_Loop
author_rank: Frequent Contributor
timestamp: November 21, 2023, 11:28:53 am
content: Quote from: wek on November 21, 2023, 08:25:50 amAny value there other than 1 causes no count at all.

author: Ground_Loop
author_rank: Frequent Contributor
timestamp: November 21, 2023, 11:31:21 am
content: Quote from: ace1903 on November 21, 2023, 07:49:07 amThese are the options:Code:@defgroup TIM_Counter_Mode TIM Counter Mode* @{*/#define TIM_COUNTERMODE_UP 0x00000000U /*!< Counter used as up-counter */#define TIM_COUNTERMODE_DOWN TIM_CR1_DIR /*!< Counter used as down-counter */#define TIM_COUNTERMODE_CENTERALIGNED1 TIM_CR1_CMS_0 /*!< Center-aligned mode 1 */#define TIM_COUNTERMODE_CENTERALIGNED2 TIM_CR1_CMS_1 /*!< Center-aligned mode 2 */#define TIM_COUNTERMODE_CENTERALIGNED3 TIM_CR1_CMS /*!< Center-aligned mode 3 */I've also tried the center aligned modes.

author: Ground_Loop
author_rank: Frequent Contributor
timestamp: November 21, 2023, 11:34:42 am
content: Quote from: Chalcogenide on November 21, 2023, 07:10:32 amLike this:HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL);I'll try just starting channels 1 and 2 and see what happens.

author: Ground_Loop
author_rank: Frequent Contributor
timestamp: November 21, 2023, 12:34:01 pm
content: Strangest thing. I moved it all to timer 1 and it works perfectly. It may be because of a reassignment of input 1 while using timer 2 due to a pin conflict. Thanks for all the replies.