<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>定时器 :: 标签 :: yafeng 的博客</title><link>http://yafengabc.github.io/tags/%E5%AE%9A%E6%97%B6%E5%99%A8/index.html</link><description/><generator>Hugo</generator><language>zh-cn</language><lastBuildDate>Sat, 19 Sep 2026 14:36:30 +0800</lastBuildDate><atom:link href="http://yafengabc.github.io/tags/%E5%AE%9A%E6%97%B6%E5%99%A8/index.xml" rel="self" type="application/rss+xml"/><item><title>树莓派高级GPIO库，wiringpi2 for python使用笔记（二）高精度计时、延时函数</title><link>http://yafengabc.github.io/cnblogs/p5096445/index.html</link><pubDate>Sun, 03 Jan 2016 15:11:00 +0800</pubDate><guid>http://yafengabc.github.io/cnblogs/p5096445/index.html</guid><description>学过单片机的同学应该清楚，我们在编写传感器驱动时，需要用到高精度的定时器、延时等功能，wiringpi提供了一组函数来实现这些功能，这些函数分别是：&#10;micros() #返回当前的微秒数，这个数在调用wiringPiSetup()后被清零并重新计时&#10;millis() #返回当前的毫秒数，同上，这个数在调用wiringPiSetup()后被清零并重新计时&#10;delayMicroseconds() #高精度微秒延时&#10;delay() #毫秒延时。&#10;python相对于C，一个很大的问题就是执行速度慢，所以指令执行速度不可忽视，我们可以用micos函数来检测指令执行时间，用来避免实际使用中遇到的坑，请看以下代码：&#10;import wiringpi2 as gpio for i in range(5): t1=gpio.micros() t2=gpio.micros() print(t2-t1) 连续调用两次micros，然后打印出差值，运行结果如下：&#10;[root@RasPi ~/testcode]# python testus.py 12 4 4 5 5 我们看到第一次的结果明显比以后的结果要大，多了接近10微秒，一般的程序来说，这无关紧要，要是要求更高，可以把代码改成这个样子:&#10;import wiringpi2 as gpio for i in range(5): t1=gpio.micros() t1=gpio.micros() t2=gpio.micros() print(t2-t1) 运行结果如下：&#10;[root@RasPi ~/testcode]# python testus.py 3 3 3 3 2 基本一致了再看以下代码：&#10;import wiringpi2 as gpio for i in range(5): t1=gpio.micros() t1=gpio.micros() gpio.delayMicroseconds(10) t2=gpio.micros() print(t2-t1) 延时10us，结果如下：&#10;[root@RasPi ~/testcode]# python testus.py 21 21 18 18 18 减去两次调用micros()之间的5us左右的延时，实际延时10us会有5us左右的延时。</description></item></channel></rss>