老师们,us级的延时函数咋写,能够自动适应时钟频率的
老师们,微秒级的延时函数咋写,能够自动适应时钟频率的,找了半天没找到这个函数库。us级的感觉还是几条nop指令和晶振频率匹配就好了。 延时函数受中断影响,一般不可用于精确延时
us级延时函数少则几个机器周期,受指令执行时间影响很大,直接用nop即可 DebugLab 发表于 2024-11-25 11:39
延时函数受中断影响,一般不可用于精确延时
us级延时函数少则几个机器周期,受指令执行时间影响很大,直接 ...
我自己写了个自动适应时钟的,,误差几十us,将就用了。 耀子 发表于 2024-11-25 11:56
我自己写了个自动适应时钟的,,误差几十us,将就用了。
非要写只能这样了,误差很大的 耀子 发表于 2024-11-25 11:56
我自己写了个自动适应时钟的,,误差几十us,将就用了。
想要准确可以看示波器调nop个数 DebugLab 发表于 2024-11-25 12:00
想要准确可以看示波器调nop个数
也可以精确计算出来,具体要看生成的汇编,对照着指令时钟数计算 us级的延迟,建议直接用NOP做成宏定义吧,或者调用官方头文件里面的NOP(x)好了,这样比较准确一点,封装成函数就误差很大了,因为调用跳转和返回都需要额外的时钟周期。 Do you mean compile-time adapt to the clock, or run-time adapt to the clock.
Run-time adapt means you need to pass a clock parameter to the inner loop, and it will have some granularity.
If you really want 1.0 us LSB, the DJNZ opcode is 3 cycles, so a 24~40MHz SYSCLK can only match that within ~ 10%
A 24~40MHz clock and inner DJNZ can more naturally give 10-25us LSB and for that range, you can adjust to within ~1%
It is a pity the 8051 did not implement a WAIT Rn opcode, that would be 1 SYSCLK granular.
There is opcode space to support a WAIT opcode, should STC decide to add one.
ms级自适应函数也有一定的误差。us级时间误差更大,无法做到自适应吧
页:
[1]
2