machine.Timer

Hardware timers deal with timing of periods and events. Timers are perhaps the most flexible and heterogeneous kind of hardware in MCUs and SoCs, differently greatly from a model to a model. MicroPython’s Timer class defines a baseline operation of executing a callback with a given period (or once after some delay), and allow specific boards to define more non-standard behavior (which thus won’t be portable to other boards).

1. Constructors

class machine.Timer(id, channel, mode=Timer.MODE_ONE_SHOT, period=1000, unit=Timer.UNIT_MS, callback=None, arg=None, start=True, priority=1, div=0)

Construct a new timer object of the given id.

1.1. Parameters

  • id: Timer ID, [0~2] (Timer.TIMER0~TIMER2)
  • channel: Timer channel, [Timer.CHANNEL0~Timer.CHANNEL3]
  • mode: Timer mode, MODE_ONE_SHOT or MODE_PERIODICor MODE_PWM
  • period: Timer period, after period the callback will be invoke, (0,~).
  • unit: unit of timer, default ms, Timer.UNIT_S or Timer.UNIT_MS or Timer.UNIT_US orTimer.UNIT_NS
  • callback: Timer callback, two parameters, first is Timer, second is user param, see param parameter below.

    callback execute in interrupt, so don't stay too long in callback

  • arg: Argument dilivered to callback
  • start: If start instantly timer after init, True:start, False:not start, need call start() function.
  • priority: interrupt priority, [1,7].
  • div: Timer clock divider,[0,255],default to 0. clk_timer = clk_pll0/2^(div+1)

    clk_timer*period(unit:s) should < 2^32 and >=1

2. Methods

2.1. init

Same to constructor

Timer.init(id, channel, mode=Timer.MODE_ONE_SHOT, period=1000, unit=Timer.UNIT_MS, callback=None, arg=None, start=True, priority=1, div=0)

2.2. callback_arg

callback arg of timer obj

2.3. callback

Get or set callback

e.g.

def on_timer(timer,param):
    print("time up:",timer)
    print("param:",timer.callback_arg())

tim.callback(on_timer)
print(on_timer, tim.callback())

2.4. period

Get or set period

e.g.

tim.period(2000)
print( tim.period() )

2.5. start

Start timer

e.g.

tim.start()

2.6. stop

Stop timer

2.7. restart

Restart timer

2.8. deinit/del

Deinitialises the timer. Stops the timer, and disables the timer peripheral.

e.g.

tim.deinit()

or

del tim

3. Constants

  • TIMER0: Timer0 id
  • TIMER1: Timer1 id
  • TIMER2: Timer2 id
  • CHANNEL0: Timer channel 0
  • CHANNEL1: Timer channel 1
  • CHANNEL2: Timer channel 2
  • CHANNEL3: Timer channel 3
  • MODE_ONE_SHOT: Timer only run once
  • MODE_PERIODIC: Timer always run
  • MODE_PWM: Timer used by PWM
  • UNIT_S: unit flag (s)
  • UNIT_MS: unit flag (ms)
  • UNIT_US: unit flag (us)
  • UNIT_NS: unit flag (ns)

4. Demo

4.1. Demo 1

Print data after 3s just once

from machine import Timer

def on_timer(timer):
    print("time up:",timer)
    print("param:",timer.callback_arg())

tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_ONE_SHOT, period=3000, callback=on_timer, arg=on_timer)
print("period:",tim.period())

4.2. Demo 2

Print every 1s, and stop 5s then restart 5s then shutdown

import time
from machine import Timer

def on_timer(timer,param):
    print("time up:",timer)
    print("param:",param)

tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PERIODIC, period=1, unit=Timer.UNIT_S, callback=on_timer, arg=on_timer, start=False, priority=1, div=0)
print("period:",tim.period())
tim.start()
time.sleep(5)
tim.stop()
time.sleep(5)
tim.restart()
time.sleep(5)
tim.stop()
del tim
powered by GitbookFile Modify: 2020-08-26 15:56:59

results matching ""

    No results matching ""