2020-03-10 02:45 PM
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.
Solved! Go to Solution.
2020-03-11 01:44 AM
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
2020-03-10 02:49 PM
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
2020-03-10 03:22 PM
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...
2020-03-10 03:29 PM
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.
2020-03-11 01:44 AM
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
2020-03-12 09:42 AM
Thank you so much JW and berendi, this sounds clear. I will attempt this, and report back if I have any further questions. :)