PWM input/Timer.IC question
Posted: Tue Oct 17, 2017 7:39 am
The STM32F4 timers are a lot more capable than the typical ATMEGA timers, so I was sort of surprised when all the examples I could find of doing PWM input or input capture - either in MicroPython or Arduino - fell back on the typical ATMEGA thing of taking interrupts on value change, and then figuring out the pulse width based on the values and a timer. Especially when the reference manual provides pretty explicit instructions on how to get the hardware to do it for you.
I translated that to Python, and got this:
Seems to work fine - calling ch.capture() returns the width of the last pulse in microseconds, which is exactly what I want. All the work is done by the timer hardware - I don't need any code in my main or an interrupt handler, I just need to configure the timer and then read the values.
That I couldn't find examples of this worries me - is there something I overlooked that makes this expensive/painful/bad compared to using the ATMEGA interrupt handler style?
I'd also like to know if there are values somewhere for the TIM_SMCR & TIM_CCER field positions I'm using, so I can replace the constants with names?
Thanks,
Mike
I translated that to Python, and got this:
Code: Select all
from pyb import Timer
from stm import mem16, TIM2, TIM_SMCR, TIM_CCER
# Configure timer2 as a microsecond counter.
tim = Timer(2, prescaler=84, period=0x3fffffff)
# Configure channel1 on PA0 for timer IC.
ch = tim.channel(1, Timer.IC, pin=pyb.Pin('PA0'), polarity=Timer.FALLING)
# Reset the timer counter on rising edge.
mem16[TIM2 + TIM_SMCR] = (5 << 4) + 4
# Capture counter on falling edge.
mem16[TIM2 + TIM_CCER] = 11
That I couldn't find examples of this worries me - is there something I overlooked that makes this expensive/painful/bad compared to using the ATMEGA interrupt handler style?
I'd also like to know if there are values somewhere for the TIM_SMCR & TIM_CCER field positions I'm using, so I can replace the constants with names?
Thanks,
Mike