I checked the API about the Timer class. It seems there is no direct method that could query Timer output state.
If this is the case, then I have to have one extra digital input hooked to the timer output in order to read back the Timer output current state, either 0 or 1 at the querying time.
My hunch is that I'm probably wrong.
Can someone point me to the correct conclusion?
Thanks a lot
Is it possible to query Timer output?
Re: Is it possible to query Timer output?
The counter() function: https://docs.micropython.org/en/latest/ ... er.counter should return the value of the timer counter (in particular the TIMx_CNT register).
I think that you can also read the corresponding GPIO pin (if you're trying to read say CH3 from Timer 4 (pin Y3) that you can read it as a GPIO pin.
Here's an example that sets up the timer to generate a PWM signal to flash the blue LED and a loop to read the value:
I think that you can also read the corresponding GPIO pin (if you're trying to read say CH3 from Timer 4 (pin Y3) that you can read it as a GPIO pin.
Here's an example that sets up the timer to generate a PWM signal to flash the blue LED and a loop to read the value:
Code: Select all
import pyb
# The Blue LED is on PB4 which is also TIM3_CH1
blue = pyb.Pin('LED_BLUE')
# Setup TIM3 channel 1 to generate a 1/2 sec on, 1/2 sec off signal on the Blue LED
tim3 = pyb.Timer(3, freq=1, mode=pyb.Timer.PWM)
ch1 = t3.channel(1, pyb.Timer.PWM, pin=blue, pulse_width_percent=50)
# Print out the value of the GPIO pin
while True:
print(blue.value())
pyb.delay(100)
Re: Is it possible to query Timer output?
I didn't know a pin assigned to a PWM channel could be queried value as a plain input. But it just works.dhylands wrote: ↑Sat Jun 25, 2022 4:33 pmThe counter() function: https://docs.micropython.org/en/latest/ ... er.counter should return the value of the timer counter (in particular the TIMx_CNT register).
I think that you can also read the corresponding GPIO pin (if you're trying to read say CH3 from Timer 4 (pin Y3) that you can read it as a GPIO pin.
Here's an example that sets up the timer to generate a PWM signal to flash the blue LED and a loop to read the value:Code: Select all
import pyb # The Blue LED is on PB4 which is also TIM3_CH1 blue = pyb.Pin('LED_BLUE') # Setup TIM3 channel 1 to generate a 1/2 sec on, 1/2 sec off signal on the Blue LED tim3 = pyb.Timer(3, freq=1, mode=pyb.Timer.PWM) ch1 = tim3.channel(1, pyb.Timer.PWM, pin=blue, pulse_width_percent=50) # Print out the value of the GPIO pin while True: print(blue.value()) pyb.delay(100)
Thank you dhylands for your kindness and warmhearted help.
(btw, t3 is a typo to tim3.) [EDIT: I fixed the typo)