使用 MsTimer2 库
文章目录
- 1. 使用 MsTimer2 库定时做多件事(教程)(定时器timer2的使用)
- 1.1 相关资料
- 1.2 MsTimer2 库简单又好用,
- 1.2.1 库地址:
- 1.2.2 MsTimer2 库函数介绍
- 1.2.3 程序范例
- 程序说明
- 1.3 MsTimer2库注意事项
- 1.4 疑问解答
- 问答1
- 问答2
- 问答3
- 问答4
- 问答5
- 问答6
- 问答7
- 2. 自己控制 timer2 定时器定时做多件事(教程, 设定timer2 定时器)
ARDUINO TIMER AND INTERRUPT TUTORIAL:https://oscarliang.com/arduino-timer-and-interrupt-tutorial/
1. 使用 MsTimer2 库定时做多件事(教程)(定时器timer2的使用)
转载:使用 MsTimer2 库定时做多件事(教程)(定时器timer2的使用)
https://www.arduino.cn/thread-12435-1-1.html
(出处: Arduino中文社区)
1.1 相关资料
可以自己用 millis( ) 或 micros( ) 检查时间以决定是否该做事了:
http://arduino.cc/en/Reference/Millis
http://arduino.cc/en/Reference/Micros可以看看我写的这篇"不使用 Timer 库要定时做某事或做两三件事(教程)定时器相关":
http://www.arduino.cn/thread-12408-1-2.html也可以使用 Timer 库或 SimpleTimer 库或类似的库做设定:
http://playground.arduino.cc/Code/Timer
http://playground.arduino.cc/Code/SimpleTimer
不过 Timer 库和 SimpleTimer 库也都是使用 millis( ), 很容易被 loop( ) 内其他事搞成"很不准"!你当然可以自己控制内部定时器 timer0, timer1, timer2 写ISR(), 可参考:
http://www.hobbytronics.co.uk/arduino-timer-interrupts
http://www.engblaze.com/we-interrupt-this-program-to-bring-you-a-tutorial-on-arduino-interrupts/
http://www.instructables.com/id/Arduino-Timer-Interrupts/step1/Prescalers-and-the-Compare-Match-Register/
http://www.instructables.com/id/Arduino-Timer-Interrupts/step2/Structuring-Timer-Interrupts/自己控制内部定时器 ,那相对比较难且很容易出错! 所以,想要比较精准定时做某件事, 最简单的就是使用硬件中断的 MsTimer2 库
http://playground.arduino.cc/Main/MsTimer2
(注意这库精准度只有以 milli second 为单位)
1.2 MsTimer2 库简单又好用,
1.2.1 库地址:
https://www.pjrc.com/teensy/td_libs_MsTimer2.html
MsTimer2, by Javier Valencia, lets you periodically run a function, by a configurable number of milliseconds.
FlexiTimer2 is version of MsTimer2 by Wim Leers, which makes the interval resolution configurable, rather than being fixed at 1 millisecond steps
Javier Valencia的MsTimer2允许您定期运行一个函数,可配置为毫秒数。 FlexiTimer2是在MsTimer2版本基础上修改的,它可配置间隔分辨率,而不是固定在1毫秒级
Download: Included with the Teensyduino Installer
Latest MsTimer2 on Github
Latest FlexiTimer2 on Github
1.2.2 MsTimer2 库函数介绍
- 设定时间与要执行的 function
MsTimer2::set( some_ms, your_function); - 启动中断
MsTimer2::start(); - 必要时可停止中断(当然随时可以再重新启动)
MsTimer2::stop();
先来看一个简单范例: (改自原本范例)#
1.2.3 程序范例
#include <MsTimer2.h>
const int INTERVAL = 500; // 0.5 秒 = 500ms
void ggyy( ) {static int gy = 0;gy = 1- gy; // toggle 0, 1digitalWrite(13, gy); // pin 13 LED
}
void setup( ) {pinMode(13, OUTPUT);MsTimer2::set(INTERVAL, ggyy); // INTERVAL msMsTimer2::start( );
}
void loop( ) {delay(6123); // 故意MsTimer2: : stop( );delay(3388);MsTimer2: : start( );
}
程序说明
- 这范例让 pin 13 的 LED 灯闪烁大约6秒, 然后停大约 3.4秒, 之后又闪烁大约6秒, 然后停大约 3.4秒, … 亮灭间隔是 0.5 秒(500 ms) !
- 你会发现: 用 MsTimer2 只能设定一件要定时做的事 !
查看 MsTimer2 库的 source code 你会发现,重复使用 MsTimer2::set( ) 只有最后一次有效,因为每次使用 MsTimer2::set( ) 会盖掉前一次的设定 ! ! - 那如果我有两件事想要定时做呢? 其实也很简单:
就是把原先定时做的事改为负责计时,并判定是否要做其他事即可!
以下是要定时做两件事的范例:
(A) 每 250 ms 做一次 myJobOne : 闪烁 LED on pin 13
(B) 每 250 ms 做一次 myJobTwo : 闪烁 LED on pin 8
#include <MsTimer2.h>
const int intA = 250; //每 250 ms 做一次 myJobOne
const int intB = 250; // 每 250 ms 做一次 myJobTwo
int led2 = 8; // pin 8
const int INTERVAL = 1; // 0.001 秒 = 1ms
void ggyy( ) {static unsigned int gy = 0;++gy;if( gy % intA == 0) myJobOne( ); // gy 除以 intA 的余数是 0if( gy % intB == 0) myJobTwo( );
}
void setup( ) {pinMode(13, OUTPUT);pinMode(led2, OUTPUT);MsTimer2::set(INTERVAL, ggyy); // INTERVAL msMsTimer2::start( );
}
void loop( ) {delay(6123); // 故意MsTimer2::stop( ); delay(3388); MsTimer2::start( );
}
void myJobOne( ) {static int gy = 0;gy = 1- gy; // toggle 0, 1digitalWrite(13, gy); // pin 13 LED
}
void myJobTwo( ) {static int gy = 1; // 故意与 myJobOne 内gy不同 !gy = 1- gy; // toggle 0, 1digitalWrite(led2, gy); // pin 8 LED
}
1.3 MsTimer2库注意事项
- 请注意, 如果你使用了 MsTimer2 库, 则 pin11 和 pin3 就不能再用做 PWM 输出了! 因为该 pin3 和 pin11 的 PWM 是靠 timer2 帮忙的! (tone( ) 也是)
- 注意 Servo.h 库与 TimerOne 都是使用内部定时器 timer1 会影响pin 9, pin 10 的 PWM
- **tone() function ** 使用 timer2 定时器; 若使用 Tone 库的 Tone 对象(Tone 变量)也是优先使用 timer2 定时器,若用两个 Tone 变量则 timer1 也会被用掉, 用三个 Tone 则连控制 millis( )的 timer0 也会被用掉 ! ! !
别忘了, timer0 负责帮忙控制 pin 5 和 pin 6 的 PWM 输出 ! ! !
只要不去改变 timer 的 Prescaler就不会影响其控制的 PWM pin, 但MsTimer2 库与 tone( )都会改变 Prescaler ! !
1.4 疑问解答
问答1
Q: 这范例显然每0.25秒都 “先” 做 myJobOne, 然后再做 myJobTwo, 并没有 “同时” 做啊?
A: 不然还能怎样 ?
Arduino 的 CPU 只有一个, 又不是多核心(multi core), 怎可能真的"同时"做呢 ? 不过 Arduino 在 16MHz 频率之下每个C语言的指令大约0.7到 3 micro seconds,
如果做了二十句 C语言指令也才大约 0.05 ms (milli second),
进入 ISR( )与离开 ISR( )总计大约要 3 micro seconds,
进入 function 与离开 function 也大约3 micro seconds,
所以, 两个工作前后差不到 0.1 个千分之一秒 ( 0.1 ms), 感觉还是 “同时” 做啦 !
如果你认为应该优先处理 myJobTwo, 那就把该两句检查 gy 的 if 前后对调即可 !
问答2
Q: 例中 intA 和 intB 可不可以设不一样呢?
A: 当然可以啊 !
你可以把 intB 改为 500 或 1000 自己测试看看 !
问答3
Q: 那如果要设定为定时做三件事呢?
ㄟ … 阿这个看完上面例子你应该就会了啊 !
只要多用个类似 intA 与 intB 的 intC 就可以仿照写出了!
好啦, 为了让初学新手更清楚如何"仿照"写出多一件事要定时做,
以下再改一下上述范例给新手参考,
这次在第三个定时的变量我故意命名 int38 以免有人误以为一定要叫做 intC !
/// 利用 MsTimer2 定时做三件事
#include <MsTimer2.h>
const int intA = 250; //每 250 ms 做一次 myJobOne
const int intB = 250; // 每 250 ms 做一次 myJobTwo
int int38 = 1000; // 每 1 秒做一次 myJob666; 没规定说必须用 const : -)
int led2 = 8; // pin 8
int led3 = 7; // pin 7
const int INTERVAL = 1; // 0.001 秒 = 1ms
void ggyy( ) {static unsigned int gy = 0;++gy;if( gy % intA == 0) myJobOne( );if( gy % intB == 0) myJobTwo( );if( gy % int38 == 0) myJob666( );
}
void setup( ) {pinMode(13, OUTPUT);pinMode(led2, OUTPUT); pinMode(led3, OUTPUT);MsTimer2::set(INTERVAL, ggyy); // INTERVAL msMsTimer2::start( );
}
void loop( ) {// 这次 loop( ) 内故意甚么都不写
}
void myJobOne( ) {static int gy = 0;gy = 1- gy; // toggle 0, 1digitalWrite(13, gy); // pin 13 LED
}
void myJobTwo( ) {static int gy = 1; // 故意与 myJobOne 内gy不同 !gy = 1- gy; // toggle 0, 1digitalWrite(led2, gy); // pin 8 LED
}
void myJob666( ) {static int gy = 0;gy = 1- gy; // toggle 0, 1digitalWrite(led3, gy); // pin 7 LED
}
问答4
Q: 可不可以定时做四件事或更多呢?
A: 用脚头乌(膝盖)想也知道当然可以, 不过这时你可能想用 Array 来记住
所有的设定时间与对应的 function, 以免 code 太长太丑看了不爽
使用 MsTimer2 库相关推荐
- millis()和MsTimer2库的使用方法
0 前言 使用这两个函数主要是为了多任务处理,可是在ESP8266使用MsTimer2库时会出错. 1 MsTimer2使用方法 使用MsTimer2库函数做多件任务 /// 利用 MsTimer2 ...
- Arduino笔记-定时器中断(MsTimer2库使用)
定时器中断,在某一特定的时候, 不管在干嘛,都要去执行那个定时器中断,指向的代码段. 这里以Arduino UNO为例: 运行截图如下: 每隔1s,亮LED_BUILTIN的灯: 这里要下载MsTim ...
- 使用TimerOne库
文章目录 1. 使用TimerOne库定时做多件事 特别强调: 相关资料 如何使用硬件中断的 TimerOne (Timer1) 库, 疑难解答 1:Q: 这范例显然每0.25秒都 "先&q ...
- arduino中断函数的学习——定时中断(MsTimer2)
arduino定时器(MsTimer2)学习 学习之前,先认识一下定时器的中断函数吧 void set(unsigned long ms, void (*f)()) 这个函数设置定时中断的时间间隔和调 ...
- 关于中断(Interrupt)的详细介绍以及IIC、软串口、PinChangeInt库
文章目录 参考文档 1. 关于中断处理的一些常见问题 . . . 1.1 常常看到有人问到: 1.2 关于中断的重进入(reentrant), 请参考维基百科: 1.3 关于中断的概念可以看看奈何大神 ...
- MS-DOS安装过程
MS-DOS系统文件是一个exe程序,将其解压之后,我们只使用其中的一个DISKS文件夹中的内容. 只使用其中以144开头的三个文件 创建虚拟机 以下内容为转载: VMWare 下安装 MSDN版 M ...
- 步进电机正反转驱动、AS5600编码器信息读取及速度检测
步进电机基本运动 1.思路: 用定时器替代mstimer2库,mstimer2底层实现是延时函数控制,delay函数占用CPU资源,使得CPU在delay函数工作时无法处理其他任务,二是delay函数 ...
- Windows API参考大全
第一章 Win32 API概论 1.1为什么使用 Wu32 API 在Windows程序设计领域处于发展初期时,Windows程序员可使用的编程工具唯有API 函数.这些函数在程序员手中犹如" ...
- Go 编译的可执行文件是否有动态库链接?
Go 引用了其他包的话,是将引用的包都编译进去.用 ldd 看几个 Go 编译出来的二进制程序有的没有动态链接库的使用.但是有的又有引用动态链接库,这个是为什么? 回答:Go 默认是开启 CGO_EN ...
最新文章
- IT工程师实战英语之一
- 量子计算机到底神在哪里说明文,“九章”量子计算机到底有多神!
- 基本数据类型与引用数据类型的不同
- jquery_pagination分页插件的使用
- wxWidgets:wxFileCtrl类用法
- python 批量更换图片格式脚本
- 【Vue】 element ui 引入第三方图标
- android--在命令行中生成Android的数字证书keystore文件
- 使用Adreno Profiler分析android游戏
- srand函数_SUDA-CS考研复试常用函数
- 切换无线网卡失败服务器提示,Win7系统切换usb无线网卡为AP模式时提示ICS启动失败如何解决...
- 华为网络技术比赛-云平台课件笔记
- APP安全测试点概述
- 微信小程序选项卡demo
- 未来互联网+大数据时代,DT革命互联网大数据应用简析
- ubuntu 18.04.1安装RTL8821CE无线网卡驱动
- 发布jar包至maven本地库及私服
- 计算机制作不同数据数据图表,数据图表与分析.doc
- 五一南京-无丝竹无案牍
- 2021-12-18 迈向程序猿的第四十七步
热门文章
- 移动叔叔MT6573一键ROOT工具!适应所有android2.2/2.3以上系统
- Python爬虫新手入门教学:爬取前程无忧招聘信息
- 天气预报查询 API + AI 等于王炸(一大波你未曾设想的天气预报查询 API 应用场景更新了)
- 《A Graduate Course in Applied Cryptography》Chapter 13 Digital Signatures(4)
- tf.distribute 分布式训练
- 【Nmap】常用命令「二」——端口扫描
- 使用matlab实现ISD悬架离散仿真分析
- 全搜索法FS、三步搜索法TSS、菱形搜索法DS
- xmpp协议抓包_在MAC下基于Openfire和Spark的XMPP协议实现
- 活动管理系统 php,单位活动现场管理系统