Author Topic: [SOLVED] STM32G431 Timer 2 Encoder Mode Only Counts One Direction  (Read 2013 times)

0 Members and 1 Guest are viewing this topic.

Offline Ground_LoopTopic starter

  • Frequent Contributor
  • **
  • Posts: 666
  • Country: us
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: [Select]
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?
« Last Edit: November 22, 2023, 02:32:31 pm by Ground_Loop »
There's no point getting old if you don't have stories.
 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 4778
  • Country: dk
Re: STM32G431 Encoder mode ony counts 1 direction
« Reply #1 on: November 20, 2023, 10:20:28 pm »
try   sConfig.IC2Polarity = TIM_ICPOLARITY_FALLING;
 

Offline Ground_LoopTopic starter

  • Frequent Contributor
  • **
  • Posts: 666
  • Country: us
Re: STM32G431 Encoder mode ony counts 1 direction
« Reply #2 on: November 20, 2023, 11:54:49 pm »
try   sConfig.IC2Polarity = TIM_ICPOLARITY_FALLING;

No 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.
« Last Edit: November 21, 2023, 12:14:07 am by Ground_Loop »
There's no point getting old if you don't have stories.
 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 4778
  • Country: dk
Re: STM32G431 Encoder mode ony counts 1 direction
« Reply #3 on: November 21, 2023, 12:35:51 am »
try   sConfig.IC2Polarity = TIM_ICPOLARITY_FALLING;

No 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.

one of the phases not connected?

 

Offline Ground_LoopTopic starter

  • Frequent Contributor
  • **
  • Posts: 666
  • Country: us
Re: STM32G431 Timer 1 Encoder Mode Only Counts One Direction
« Reply #4 on: November 21, 2023, 01:12:02 am »
Solid connection both phases.  Monitoring both at the pin with oscope.  Further, it will not count at all with one disconnected.
There's no point getting old if you don't have stories.
 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 4778
  • Country: dk
Re: STM32G431 Timer 1 Encoder Mode Only Counts One Direction
« Reply #5 on: November 21, 2023, 01:27:46 am »
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 F411

https://github.com/langwadt/nucleo_quad4
 

Offline Ground_LoopTopic starter

  • Frequent Contributor
  • **
  • Posts: 666
  • Country: us
Re: STM32G431 Timer 1 Encoder Mode Only Counts One Direction
« Reply #6 on: November 21, 2023, 02:56:22 am »
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 F411

https://github.com/langwadt/nucleo_quad4

Thanks for the link but seems a lot of fuss for something that should work entirely in the peripheral.
There's no point getting old if you don't have stories.
 

Offline Chalcogenide

  • Regular Contributor
  • *
  • Posts: 170
  • Country: it
Re: STM32G431 Timer 1 Encoder Mode Only Counts One Direction
« Reply #7 on: November 21, 2023, 07:10:32 am »
Dumb question: how do you start the timer? The config looks fine to me, so maybe something is wrong there.
 

Offline ace1903

  • Regular Contributor
  • *
  • Posts: 240
  • Country: mk
Re: STM32G431 Timer 1 Encoder Mode Only Counts One Direction
« Reply #8 on: November 21, 2023, 07:49:07 am »
 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
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 536
  • Country: sk
Re: STM32G431 Timer 1 Encoder Mode Only Counts One Direction
« Reply #9 on: November 21, 2023, 08:25:50 am »
Always start debugging with reading out and checking relevant registers content, and in such questions, post them.

> htim2.Init.Prescaler = 1;

That's certainly 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 by incompletely initialized initialization structs.

JW
« Last Edit: November 21, 2023, 08:31:43 am by wek »
 

Offline Ground_LoopTopic starter

  • Frequent Contributor
  • **
  • Posts: 666
  • Country: us
Re: STM32G431 Timer 1 Encoder Mode Only Counts One Direction
« Reply #10 on: November 21, 2023, 11:28:53 am »
Always start debugging with reading out and checking relevant registers content, and in such questions, post them.

> htim2.Init.Prescaler = 1;

JW

Any value there other than 1 causes no count at all.
There's no point getting old if you don't have stories.
 

Offline Ground_LoopTopic starter

  • Frequent Contributor
  • **
  • Posts: 666
  • Country: us
Re: STM32G431 Timer 1 Encoder Mode Only Counts One Direction
« Reply #11 on: November 21, 2023, 11:31:21 am »
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

These are the options:
Code: [Select]
@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.
There's no point getting old if you don't have stories.
 

Offline Ground_LoopTopic starter

  • Frequent Contributor
  • **
  • Posts: 666
  • Country: us
Re: STM32G431 Timer 1 Encoder Mode Only Counts One Direction
« Reply #12 on: November 21, 2023, 11:34:42 am »
Dumb question: how do you start the timer? The config looks fine to me, so maybe something is wrong there.

Like this:
 HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL);

I'll try just starting channels 1 and 2 and see what happens.
« Last Edit: November 21, 2023, 06:19:25 pm by Ground_Loop »
There's no point getting old if you don't have stories.
 

Offline Ground_LoopTopic starter

  • Frequent Contributor
  • **
  • Posts: 666
  • Country: us
Re: STM32G431 Timer 1 Encoder Mode Only Counts One Direction
« Reply #13 on: November 21, 2023, 12:34:01 pm »
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.
« Last Edit: November 21, 2023, 12:35:51 pm by Ground_Loop »
There's no point getting old if you don't have stories.
 
The following users thanked this post: 2N3055


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf