cancel
Showing results for 
Search instead for 
Did you mean: 

Can TIM_15, 16, or 17 be used to create an upcounting (only) encoder on an STM32G4?

TB
Associate III

I'm running out of timers on the STM32G431RB that I am designing for, and am inclined to try to use TIM_15, 16, and/or 17 for upcounting (only! not up/down quadrature counting) encoders, for rotary encoders on shafts that only spin one direction. Is this possible? I'm finding quite a lack of information on this type of application for these timers in both the STM32G4 Datasheet as well as the 2000+ supplemental Reference Manual.

1 ACCEPTED SOLUTION

Accepted Solutions

As berendi pointed out, TIM16 and TIM17 don't have the slave-mode controller, I missed that, sorry.

So you can use TIM15_CH1 (for both edges can't use CH2), set it to "direct" input by setting TIMx_CCMR1.CC1S to 0b01, set TIMx_SMCR.TS to 0b00100 to select the CH1 edge detector as input (TRGI) to the slave-mode controller, set TIMx_SMCR.SMS to 0b0111 for External clock mode 1, and then you just set up the timer as usually - set prescaler and ARR (presumably you want to leave them at their maximum) and enable counting by setting TIMx_CR1.CEN.

Optionally, if your input signal is prone to bouncing at the edges, you may want to employ an input filter, see TIMx_CCMR1.IC1F.

Note, that the edges in your input signal must be apart more than the time delay given by the filter, or if no filter, than the period of timer's internal clock.

JW

View solution in original post

5 REPLIES 5

Every timer which has at least one external pin (TIMx_CH1 or TIMx_CH2), supports external clock mode - see the TIM chapter in RM.

If it's not what you wanted, draw a diagram, what is the input signal and what is the expected behaviour of the timer.

JW

TB
Associate III

input signal is a (externally supplied) square wave of unknown (irregular) period (from an encoder on a shaft that only turns one direction). I just want to increment a counter at every rising edge (or, on both the rising and falling edges) of this input signal when the feature is enabled. And to reset this counter from time to time on demand. Sure sounds like an "external clock" mode, but I can't figure out which one from the RM...

berendi
Principal

Counting on an external signal (i.e. incrementing the counter whenever there is an edge detected on the pin) depends on the slave mode control register (SMCR). If the timer doesn't have SMCR, it can't do it.

For timers that do have SMCR, read the clock selection chapter under the timer functional description in the reference manual. There are step-by-step procedures telling you what to write in which timer register.

Enable the necessary timer and GPIO clocks in RCC, and set the GPIO pin alternate modes beforehand.

As berendi pointed out, TIM16 and TIM17 don't have the slave-mode controller, I missed that, sorry.

So you can use TIM15_CH1 (for both edges can't use CH2), set it to "direct" input by setting TIMx_CCMR1.CC1S to 0b01, set TIMx_SMCR.TS to 0b00100 to select the CH1 edge detector as input (TRGI) to the slave-mode controller, set TIMx_SMCR.SMS to 0b0111 for External clock mode 1, and then you just set up the timer as usually - set prescaler and ARR (presumably you want to leave them at their maximum) and enable counting by setting TIMx_CR1.CEN.

Optionally, if your input signal is prone to bouncing at the edges, you may want to employ an input filter, see TIMx_CCMR1.IC1F.

Note, that the edges in your input signal must be apart more than the time delay given by the filter, or if no filter, than the period of timer's internal clock.

JW

Thank you so much JW and berendi, this sounds clear. I will attempt this, and report back if I have any further questions. 🙂