[英]Raspberry Pi Pico - Nanosecond Timer
I'm new to Micropython and microcontrollers in general.一般来说,我是 Micropython 和微控制器的新手。 I'm trying to create a script to run on a Raspberry Pi Pico that takes two time variables
time1 = utime.time_ns()
and time2 = utime.time_ns()
and then subtracts time2 from time1 to give me the difference between the two times with nanosecond precision.我正在尝试创建一个脚本以在 Raspberry Pi Pico 上运行,该脚本需要两个时间变量
time1 = utime.time_ns()
和time2 = utime.time_ns()
,然后从 time1 中减去 time2 以获得两个时间之间的差异具有纳秒精度。 When attempting to do this it prints out the value in nanoseconds rounded up to the second... for example, If there is 5 seconds between the two times the value returned is 5000000000... Is there a way that I can get a more accurate time?尝试执行此操作时,它会打印出以纳秒为单位的值,四舍五入到秒...例如,如果两次之间有 5 秒,则返回的值为 5000000000 ... 有没有办法让我获得更多准确的时间? Am I going about this the wrong way?
我会以错误的方式解决这个问题吗? Thank you!!!
谢谢!!!
The MicroPython utime page explains how there is a difference between absolute time from time_ns()
and relative time from ticks_us()
. MicroPython utime页面解释了 time_ns() 的绝对时间和
ticks_us()
time_ns()
的相对时间之间的差异。 They are best used for different purposes, and probably use different resources.它们最好用于不同的目的,并且可能使用不同的资源。 From the examples there, you could try something like
从那里的示例中,您可以尝试类似
start = time.ticks_us()
...
end = time.ticks_us()
usecs = time.ticks_diff(end, start)
As the page explains, this cannot be used to measure long times, such as more than 1 or 2 seconds, depending on implementation, and the resolution will not be nanoseconds, but at best microseconds.正如页面所解释的,这不能用于测量长时间,例如超过 1 或 2 秒,具体取决于实现,并且分辨率不会是纳秒,但最好是微秒。
The processor crystal is not going to be accurate enough to get nanosecond precision.处理器晶体的精度不足以获得纳秒级精度。 You would have to replace it with a TCXO/OCXO crystal to get microsecond precision.
您必须用 TCXO/OCXO 晶体替换它才能获得微秒级精度。 Another problem is crystal drift with temperatures.
另一个问题是晶体随温度漂移。 The OCXO is a heated crystal.
OCXO 是一种加热晶体。 A TCXO is a temperature compensated crystal.
TCXO 是一种温度补偿晶体。 As long as the temperature change a small a TCXO will likely get you in the microsecond ballpark.
只要温度变化很小,TCXO 就可能让您进入微秒级的球场。 Then comes the firmware issues.
然后是固件问题。 Python is too slow for precise timing.
Python 对于精确计时来说太慢了。 you would have to pick a compiled language to minimize the jitter issues.
您必须选择一种编译语言来最大限度地减少抖动问题。 I hope this helped.
我希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.