Students who have learned SCM should be aware that when we write sensor drivers, we need to use high-precision timer, delay and other functions, WIRINGPI provides a set of functions to implement these functions, the functions are:
Micros () #返回当前的微秒数, this number is cleared after calling Wiringpisetup () 0 and re-timed
Millis () #返回当前的毫秒数, IBID., this number is cleared after calling Wiringpisetup () 0 and re-timed
Delaymicroseconds () #高精度微秒延时
Delay () #毫秒延时.
Python relative to C, a big problem is that the execution speed is slow, so the instruction execution speed is not negligible, we can use the Micos function to detect the command execution time, to avoid the actual use of the pit encountered, see the following code:
Import Wiringpi2 as Gpio for in range (5): T1=gpio.micros () t2=Gpio.micros () Print (T2-T1)
Call two consecutive micros, then print the travel value, running the result as follows:
[Email protected] ~/testcode]# python testus.py A 4 4 5 5
We see that the first results are significantly larger than the results of the future, more than 10 microseconds in general, which is irrelevant if the requirements are higher, and the code can be changed to look like this:
Import Wiringpi2 as Gpio for in range (5): T1=Gpio.micros () T1=gpio.micros () t2= Gpio.micros () Print (T2-T1)
The results of the operation are as follows:
[[email protected] ~/testcode] # python testus.py33332
Basically, look at the following code:
Import Wiringpi2 as Gpio for in range (5): T1=Gpio.micros () T1=Gpio.micros () Gpio.delaymicroseconds (ten) t2=Gpio.micros () print(T2-T1)
Delay 10US, the result is as follows:
[[email protected] ~/testcode] # python testus.py2121181818
Minus two calls Micros () between the delay of about 5US, the actual delay of 10US will have about 5us delay.
Import Wiringpi2 as Gpio for in range (5): T1=Gpio.micros () T1=Gpio.micros () for in range: pass t2=Gpio.micros () Print (T2-T1)
Results:
[[email protected] ~/testcode] # python testus.py5969666162
That is, a few instructions, each delay under 1us, you can basically ignore, call the function, there is 5-10us about the delay, in the writing process, should fully consider this point. If there is a complex code snippet in the timing, it's best to actually test it to see how the execution time affects our timing.
Raspberry Pi Advanced Gpio Library, wiringpi2 for Python use notes (ii) High precision timing, delay function