Is it possible to query Timer output?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
bittware
Posts: 45
Joined: Mon Aug 18, 2014 3:27 am

Is it possible to query Timer output?

Post by bittware » Sat Jun 25, 2022 9:26 am

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

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Is it possible to query Timer output?

Post by dhylands » Sat Jun 25, 2022 4:33 pm

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:

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)

bittware
Posts: 45
Joined: Mon Aug 18, 2014 3:27 am

Re: Is it possible to query Timer output?

Post by bittware » Sun Jun 26, 2022 12:27 am

dhylands wrote:
Sat Jun 25, 2022 4:33 pm
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:

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)
I didn't know a pin assigned to a PWM channel could be queried value as a plain input. But it just works.
Thank you dhylands for your kindness and warmhearted help.
(btw, t3 is a typo to tim3.) [EDIT: I fixed the typo)

Post Reply