NodeMcu(三)--tmr模块详解

趁着一股热情 来学习tmr模块, 找到官方的api文档, 网址:https://nodemcu.readthedocs.io/en/master/en/modules/tmr/ 全是鸟语,   怕啥子, 我们是攻城狮, 这点鸟语怕啥, 看不懂这不是还有谷歌翻译吗

tmr官方介绍:

The tmr module allows access to simple timers, the system counter and uptime.

It is aimed at setting up regularly occurring tasks, timing out operations, and provide low-resolution deltas.

What the tmr module is not however, is a time keeping module. While most timeouts are expressed in milliseconds or even microseconds, the accuracy is limited and compounding errors would lead to rather inaccurate time keeping. Consider using the rtctime module for "wall clock" time.

NodeMCU provides 7 static timers, numbered 0-6, and dynamic timer creation function 

意思是说: tmr模块允许访问简单的定时器,系统计数器和正常运行时间, 它旨在建立定期发生的任务,超时操作,并提供低分辨率的增量。然而,tmr模块不是一个计时模块。 虽然大多数超时以毫秒或甚至微秒表示,但准确性有限,并且复合错误将导致相当不准确的时间保持。 考虑将rtctime模块用于“挂钟”时间。NodeMCU提供7个静态定时器,编号为0-6,以及动态定时器创建功能

api的简介:

逐个介绍:

1.tmr.alarm()函数

介绍:这是一个将tmr.register()和tmr.start()组合成一个调用的函数。要在使用它时使用此计时器释放资源,请在其上调用tmr.unregister()。 对于一次性定时器,这不是必需的,除非它们在过期之前停止。

函数原型:tmr.alarm([id/ref], interval_ms, mode, func())

参数:id/ref: 定时器的id(0-6)或者是对象

interval_ms:定时器间隔,以毫秒为单位。 最大值为6870947(1:54:30.947)。

mode: 定时器模式, 有以下几种:

        tmr.ALARM_SINGLE:一次性警报(无需调用tmr.unregister())

        tmr.ALARM_SEMI:手动重复报警(调用tmr.start()重启)

        tmr.ALARM_AUTO:自动重复报警

func(): 定时器到时间时调用的函数

返回值:

       true:定时器开始工作,  false: 定时器错误

例程:

if not tmr.create():alarm(5000, tmr.ALARM_SINGLE, function()
  print("hey there")
end)
then
  print("whoopsie")
end

2.tmr.create()函数:创造动态计时器对象, 可以在控制功能中使用动态计时器代替数字ID。 也可以以面向对象的方式进行控制

timer对象支持的函数:

传入参数: 无

返回值: 一个timer对象

例程:

local mytimer = tmr.create()

-- oo calling
mytimer:register(5000, tmr.ALARM_SINGLE, function (t) print("expired"); t:unregister() end)
mytimer:start()

-- with self parameter
tmr.register(mytimer, 5000, tmr.ALARM_SINGLE, function (t) print("expired"); tmr.unregister(t) end)
tmr.start(mytimer)

这个例程涉及两个新的函数, register和start 我们提前先介绍这两个函数

3.tmr.register():配置定时器并注册回调函数以在到期时调用。要在使用它时使用此计时器释放资源,请在其上调用tmr.unregister()。 对于一次性定时器,这不是必需的,除非它们在过期之前停止。

函数原型:tmr.register([id/ref], interval_ms, mode, func())

参数:id/ref:定时器的id(0-6), 或者是一个对象

        interval_ms:需要定时的时间

        mode:定时器的模式, 和:tmr.alarm的mode参数一样, 在这里就不详述了

        func():回调函数,以timer对象作为参数调用

返回值:nil

例程:

mytimer = tmr.create()
mytimer:register(5000, tmr.ALARM_SINGLE, function() print("hey there") end)
mytimer:start()

4.tmr.start():启动或重新启动先前配置的计时器

函数原型:tmr.start([id/ref])

参数: id/ref: id定时的id(0-6), ref: tmr对象

返回值:true:如果定时器开始运行, false: 遇到错误

例程:

mytimer = tmr.create()
mytimer:register(5000, tmr.ALARM_SINGLE, function() print("hey there") end)
if not mytimer:start() then print("uh oh") end

5.tmr.delay():让处理器在us级延时内一直循环, 这通常是一个坏主意,因为没有其他东西可以运行,因此网络堆栈(和其他东西)可能会失败。 tmr.delay()可能适合使用的唯一时间是处理需要在命令之间(非常)短暂延迟的外围设备,或类似的。 谨慎使用!还要注意,延迟的实际时间量可能明显更大,这是由于时间不准确以及在此期间可能发生的中断。

函数原型:tmr.delay(us)

参数: us: 需要延时的us数

返回值:nil

例程:

for var=0, 5 do
    print("hi")
    tmr.delay(1000000)
end

6.tmr.interval():更改已注册计时器的到期时间间隔

函数原型:tmr.interval([id/ref], interval_ms)

参数: id/ref: 定时器id/定时器对象

         interal_ms:定时器新的定时时间, 最大为 6870947 (1:54:30.947).

返回值:nil

例程:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

零涂

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值