WiPy Timer

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
iron2414
Posts: 6
Joined: Tue Oct 18, 2016 7:29 pm

WiPy Timer

Post by iron2414 » Tue Nov 29, 2016 10:40 pm

Hello, i found numerous questions here about the WiPy timer class, but most of them was before the 1.6 release. However i still couldn't make it work. I have a wipy with the latest framework(1.8.2). I tried all the examples from the docs, here are my tries:


[CODE]
from machine import Timer
from machine import Pin

tim = Timer(1, mode=Timer.PERIODIC, width=32)
tim_a = tim.channel(Timer.A | Timer.B, freq=1) # 1 Hz frequency requires a 32 bit timer

def tick(timer): # we will receive the timer object when being called
f = open('abc.txt', 'w')
f.write('it_works')
f.close() # toggle the LED
print('timerTicked')

tim_a.irq(handler=tick, trigger=Timer.TIMEOUT)
[/CODE]


[CODE]
from machine import Timer
from machine import Pin
led = Pin('GP16', mode=Pin.OUT) # enable GP16 as output to drive the LED
tim = Timer(3) # create a timer object using timer 3
tim.init(mode=Timer.PERIODIC) # initialize it in periodic mode
tim_ch = tim.channel(Timer.A, freq=5) # configure channel A at a frequency of 5Hz
tim_ch.irq(handler=lambda t:led.toggle(), trigger=Timer.TIMEOUT) # toggle a LED on every cycle of the timer
[/CODE]

I tried the other basic examples also, none of them seemed to work for me, but i dont get any errors either, could someone help me out?

vikebo
Posts: 15
Joined: Sun Mar 15, 2015 8:15 pm
Location: Norway

Re: WiPy Timer

Post by vikebo » Thu Dec 01, 2016 5:22 pm

Hello,

I had some problems with the Timers not so long ago, also trying the examples. Unfortunately, I do not see what is wrong in your code, but I can show what is working on my WiPy now.

Currently using the Pycom firmware (MicroPython v1.8.2-103-g834e021), but will move to the official MicroPython when I get the time...

I have added this code to a file called nr.py:

Code: Select all

from machine import Timer
from machine import ADC

counter_timer_callback = 0   # Count number of timer callbacks

# Create timer callback function
def timer_callback(timer):
    global counter_timer_callback
    counter_timer_callback += 1
        
# Configure timer with callback to enable temperature measurements
# at fixed intervals
def timer_init():
    temperature_init()
    
    tim = Timer(1, mode = Timer.PERIODIC, width = 32)
    tim_ch = tim.channel(Timer.A | Timer.B, freq = 2)
    tim_ch.irq(handler = timer_callback, trigger = Timer.TIMEOUT)
    print('timer initialised')

# Wait for counter_timer_callback to reach 120
def timer_wait_for_120():
    global counter_timer_callback
    
    while(1):
        if (counter_timer_callback > 120):
            temperature_read()
            counter_timer_callback = 0
            print('temperature read')
To test this:

Code: Select all

>>> import nr
>>> nr.timer_init()
timer initialised
>>> nr.timer_wait_for_120()
temperature read   <--- This repeats every minute
Hope it helps,

iron2414
Posts: 6
Joined: Tue Oct 18, 2016 7:29 pm

Re: WiPy Timer

Post by iron2414 » Thu Dec 01, 2016 8:14 pm

I have no idea what was wrong with mine, but this one works, thank you so much!

Post Reply