Page 1 of 1

Use of timer.deinit()

Posted: Wed Feb 11, 2015 11:40 pm
by PieMan
I've tried using timer.deinit() and it doesn't stop the timer as I expect.
See the documentation here: http://docs.micropython.org/en/latest/l ... mer.deinit

I've written the following code which sets a PWM going on pin X2. I see the signal, as I expect, on my oscilloscope.
I expect the PWM to be stopped when I call the timer's deinit() but it doesn't. My workaround it to set the PWM to a 0/100 ratio but I expected the deinit() to "Stop the timer, and disable the timer peripheral" as described in the documentation.

Can someone explain where I'm wrong please? I've searched the forum and assume I'm mistaken somewhere.

Code: Select all

from pyb import Timer, Pin

# Set a PWM output going on pin X2
tim2 = Timer(2,prescaler=83,period=40500)
ch2 = tim2.channel(2,Timer.PWM,pin=Pin('X2'),pulse_width_percent=25)
# Pin X2 outputting the PWM

# Count to 10 (seconds) to give me time to see the PWM on oscilloscope
for x in range (1,10):
   pyb.delay(1000)
   print(x,end='')
print()
# Stop the timer ######### THIS DOESN'T DO WHAT I EXPECT:
tim2.deinit()

# Count to 10 (seconds) to give me time to see if the PWM has stopped.
for x in range (1,10):
   pyb.delay(1000)
   print(x,end='')
print()
# Flat-line the PWM by setting it to 0/100 ratio.
ch2.pulse_width_percent(0)

Re: Use of timer.deinit()

Posted: Thu Feb 12, 2015 12:56 pm
by PieMan
Hi
I forgot mention, I'm using a recent MicroPython version: v1.3.9-36-g1cd47c6 on 2015-02-04; PYBv1.0 with STM32F405RG

And I've realised overnight, I've only tried timer.deinit() on timer 2. I'll try some other timers tonight to see if deinit() works as I expect on those.

Can anyone confirm they know that timer.deinit() stops the timer? On timer 2 too?

Re: Use of timer.deinit()

Posted: Thu Feb 12, 2015 4:48 pm
by pythoncoder
The docs https://micropython.org/doc/module/pyb/Timer do indeed state that it "stops the timer" and this doesn't happen as far as I can see. I suggest you file an issue on github.

Re: Use of timer.deinit()

Posted: Thu Feb 12, 2015 6:51 pm
by PieMan
Thanks pythoncoder,

I've had a look at the history of the timer.deinit() method and can see some interesting discussion:

https://github.com/micropython/micropython/issues/733
and
https://github.com/micropython/micropython/issues/735

I'll do some more digging, experimenting and learning.

This could be a "bug" or "weakness" in timer.deinit() but for my use case it might be the wrong tool for the job, I just want to stop the timer.

Re: Use of timer.deinit()

Posted: Thu Feb 12, 2015 11:58 pm
by PieMan
I've raised as an issue on GitHub. See https://github.com/micropython/micropython/issues/1113

Re: Use of timer.deinit()

Posted: Fri Feb 13, 2015 12:06 pm
by Damien
Should now be fixed!

Re: Use of timer.deinit()

Posted: Fri Feb 13, 2015 9:18 pm
by PieMan
Thanks Damien
timer.deint() now stops the timer as expected and as described in the documentation.