打印
[AVR单片机]

关于延时的问题

[复制链接]
612|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
chuandaoxy|  楼主 | 2017-7-25 10:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
AVR的延时是如何计算的,下面的代码是外部晶振3.6864M的,我现在变成12M,cpuMega8
/*************************************************************************
delay loop for small accurate delays: 16-bit counter, 4 cycles/loop
*************************************************************************/
static inline void _delayFourCycles(unsigned int __count)
{
    if ( __count == 0 )
        __asm__ __volatile__( "rjmp 1f\n 1:" );    // 2 cycles
    else
        __asm__ __volatile__ (
            "1: sbiw %0,1" "\n\t"
            "brne 1b"                              // 4 cycles/loop
            : "=w" (__count)
            : "0" (__count)
           );
}


/*************************************************************************
delay for a minimum of <us> microseconds
the number of loops is calculated at compile-time from MCU clock frequency
*************************************************************************/

#define delay(us)  _delayFourCycles( ( ( 1*(F_CPU/4000) )*us)/1000 )

需要修改吗?比如原来delay(1500),现在要改成多少?

相关帖子

沙发
694844985| | 2017-7-25 14:26 | 只看该作者
计算一条指令需要多少个时钟周期

使用特权

评论回复
板凳
feelhyq| | 2017-7-25 17:04 | 只看该作者
694844985 发表于 2017-7-25 14:26
计算一条指令需要多少个时钟周期

即便算出来了,可能效果不会很大,因为该跟编译器的优化代码的优化程度有关,硬件定时器,简单实用。

使用特权

评论回复
地板
linqing171| | 2017-7-25 21:47 | 只看该作者
1500*12/3.6864 =4883

这个延时是内嵌汇编的,和编译器优化无关。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

843

主题

1029

帖子

4

粉丝