timer questions

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
Posts: 2
Joined: Fri Jan 19, 2018 4:57 am

timer questions

Post by stromgald » Fri Jan 19, 2018 5:12 am

Hi all,
I am new to micropython and the pyboard. I had a few questions about the timer functions.
I've been able to get the basic blink an LED at the timer freq using a callback working. However when i tried the same thing using a prescaler and period I didn't get the expected result.

tim = pyb.Timer(4)
tim.init(prescaler=83, period=1000000)
tim.callback(lambda t: pyb.LED(1).toggle())

as i understand it this should make a 1Mhz clock that counts up to 1000000 and then triggers the interrupt which should toggle the led once a second? But it just seems to stay on (or maybe blinking very rapidly).

Second deals with output compare
t2 = pyb.Timer(2, freq=2000)
oc = t2.channel(2, pyb.Timer.OC_TIMING)
oc.callback(lambda t: pyb.LED(2).toggle())
I was hoping to set a timer at a fixed freq and use OC_TIMING to make an interrupt when the counter reaches a value. at 2000hz that should make LED blink 2 per second? but it just appears on again. What happens after outputcompare reaches the value does it start counting again?

Thanks for any help!

User avatar
Posts: 2626
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada

Re: timer questions

Post by dhylands » Fri Jan 19, 2018 6:34 am

Timer 4 is a 16-bit timer, so the period has to be between 0 and 65535. Timers 2 & 5 are 32-bit timers.

According to the datasheet, OC_TIMING mode is a special mode used where the compare register is ignored, so no output is generated.

Use OC_TOGGLE if you want the output to toggle when the compare register is reached.

Note that if you use freq= then the code will pick a prescaler and period and using compare doesn't really make sense. If you're going to use compare, then you want to setup the prescaler and period yourself, so you're comparing against something expected.

If you removed your oc.compare(500) and us OC_TOGGLE, then the LED should toggle at 2 kHz (or turn on 1000 time per second).

If you used a prescaler of 8399 then the counter would go from 0-9999 in a second. Note that the compare interrupt will still occur only once per full period. With oc.compare you're really just changing the phase.

Posts: 2
Joined: Fri Jan 19, 2018 4:57 am

Re: timer questions

Post by stromgald » Fri Jan 19, 2018 8:02 pm

Thanks so much Dave. That makes sense for the period not working.

But for the OC compare i don't want to drive a pin i want to do an ISR. That function would do a couple tasks in addition to generating a 1us pulse to drive a stepper motor.

The arduino version would be
if (dir_M2 == 0) // If we are not moving we dont generate a pulse
// We generate 1us STEP pulse
if (dir_M2 > 0)

Still thinking of a good way to get the 1us delay because i saw it mentioned that it's not good practice to use delay in a callback.

Post Reply