cancel
Showing results for 
Search instead for 
Did you mean: 

Can I use a single timer for encoder counting on channels 1 & 2 and PWM generation on channels 3 & 4?

Ricardo1
Associate II

Hello,

I have received conflicting answers to this question so I want to try asking it in a different way. Hopefully you understand my situation but please let me know if I should clarify anything.

I want to use an STM32G431RB. I need to be able to read 4 quadrature encoders and drive 9 PWM outputs.

In order to do this, I believe I need to use at least one timer running Encoder Mode on channels 1 & 2 and PWM Output on channels 3 & 4. Is that possible?

I currently have an STM32F3Discovery board for testing purposes and I have set up PWM generation on TIM2 channels 3 & 4 using STM32CubeIDE. When I measure the pins with an oscilloscope, I read the expected PWM. Then, I set TIM2 Combined Channels to Encoder Mode and, without adding anything else to my code, the oscilloscope shows that the pin is set to high (3V). Could something be off in my code? Is this expected? Is there a way to generate PWM with TIM2 CH3 and CH4 in this setup?

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
KnarfB
Principal III

> In order to do this, I believe I need to use at least one timer running Encoder Mode on channels 1 & 2 and PWM Output on channels 3 & 4. Is that possible?

No, the hardware is not designed that way. One Timer == one counter register (plus 0 or more timer channels attached to it).

How to clock that counter register? For PWM you need a stable periodic clock to gererate a stable PWM, the internal or some external clock.

But, for Encoder Mode, the counter register is clocked by the encoder pulses. If there are no encoder pulses (e.g. wheel motors turned off), the counter stands still, no PWM.Quadrature Encoder Mode is a type of encoder mode where the counter register counts up or down depending on the input pattern (using CH1 and CH2 channel inputs)

If you cannot allocate all the resources in the proposed chip, you can

  • try a larger package like LQFP100 having more timers
  • move the PWM generation to an external chip (many choices)
  • check if you can do some task in software by interrupt handlers. Both, Quad Encoder counting and PWM can be done in principle in SW depending on the performance, precision and safety requirements of your design

View solution in original post

2 REPLIES 2
KnarfB
Principal III

> In order to do this, I believe I need to use at least one timer running Encoder Mode on channels 1 & 2 and PWM Output on channels 3 & 4. Is that possible?

No, the hardware is not designed that way. One Timer == one counter register (plus 0 or more timer channels attached to it).

How to clock that counter register? For PWM you need a stable periodic clock to gererate a stable PWM, the internal or some external clock.

But, for Encoder Mode, the counter register is clocked by the encoder pulses. If there are no encoder pulses (e.g. wheel motors turned off), the counter stands still, no PWM.Quadrature Encoder Mode is a type of encoder mode where the counter register counts up or down depending on the input pattern (using CH1 and CH2 channel inputs)

If you cannot allocate all the resources in the proposed chip, you can

  • try a larger package like LQFP100 having more timers
  • move the PWM generation to an external chip (many choices)
  • check if you can do some task in software by interrupt handlers. Both, Quad Encoder counting and PWM can be done in principle in SW depending on the performance, precision and safety requirements of your design

berendi
Principal

Don't forget about the LPTIM which can work in encoder mode too, freeing up a 4 channel regular timer for PWM generation.