本帖最后由 猪百岁 于 2021-3-30 20:05 编辑
Timer(定时器) 的使用
关于 Timer 详细介绍请参考Timer-API 文档.
1. 使用方法
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_PERIODIC, period=1, unit=Timer.UNIT_S, callback=on_timer, arg=on_timer, start=False, priority=1, div=0)
tim.start()
tim.stop()
2. 示例
定时执行回调函数
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_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
|