Use of timer.deinit()

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
PieMan
Posts: 6
Joined: Wed Feb 11, 2015 11:01 pm

Use of timer.deinit()

Post by PieMan » Wed Feb 11, 2015 11:40 pm

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)

PieMan
Posts: 6
Joined: Wed Feb 11, 2015 11:01 pm

Re: Use of timer.deinit()

Post by PieMan » Thu Feb 12, 2015 12:56 pm

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?
Last edited by PieMan on Thu Feb 12, 2015 10:39 pm, edited 1 time in total.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Use of timer.deinit()

Post by pythoncoder » Thu Feb 12, 2015 4:48 pm

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.
Peter Hinch
Index to my micropython libraries.

PieMan
Posts: 6
Joined: Wed Feb 11, 2015 11:01 pm

Re: Use of timer.deinit()

Post by PieMan » Thu Feb 12, 2015 6:51 pm

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.

PieMan
Posts: 6
Joined: Wed Feb 11, 2015 11:01 pm

Re: Use of timer.deinit()

Post by PieMan » Thu Feb 12, 2015 11:58 pm

I've raised as an issue on GitHub. See https://github.com/micropython/micropython/issues/1113

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Use of timer.deinit()

Post by Damien » Fri Feb 13, 2015 12:06 pm

Should now be fixed!

PieMan
Posts: 6
Joined: Wed Feb 11, 2015 11:01 pm

Re: Use of timer.deinit()

Post by PieMan » Fri Feb 13, 2015 9:18 pm

Thanks Damien
timer.deint() now stops the timer as expected and as described in the documentation.

Post Reply