avr 中 delay 函数的调用注意事项!delay_ns delay_ms

上传人:第*** 文档编号:31321085 上传时间:2018-02-06 格式:DOCX 页数:10 大小:19.26KB
返回 下载 相关 举报
avr 中 delay 函数的调用注意事项!delay_ns delay_ms_第1页
第1页 / 共10页
avr 中 delay 函数的调用注意事项!delay_ns delay_ms_第2页
第2页 / 共10页
avr 中 delay 函数的调用注意事项!delay_ns delay_ms_第3页
第3页 / 共10页
avr 中 delay 函数的调用注意事项!delay_ns delay_ms_第4页
第4页 / 共10页
avr 中 delay 函数的调用注意事项!delay_ns delay_ms_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《avr 中 delay 函数的调用注意事项!delay_ns delay_ms》由会员分享,可在线阅读,更多相关《avr 中 delay 函数的调用注意事项!delay_ns delay_ms(10页珍藏版)》请在金锄头文库上搜索。

1、早就知道 AVR 的编译器有自带的延时子函数(或者说是头文件),但一直没时间一探究竟,今天终于揭开了其内幕。AVR 编译器众多,可谓是百家齐鸣,本人独尊 WinAVR.说明:编译器版本 WinAVR-20080610先说 winAVR 的_Delay.h_肯定是在 Include 文件夹下了,进去一看果然有,可打开一看,其曰:“This file has been moved to .在 util 文件夹中找到 delay 头文件如下:-void_delay_us(double _us)uint8_t _ticks;double _tmp = (F_CPU) / 3e6) * _us; /3e

2、6=3000000if (_tmp 255)_delay_ms(_us / 1000.0);return;else_ticks = (uint8_t)_tmp;_delay_loop_1(_ticks);-_delay_ms(double _ms)uint16_t _ticks;double _tmp = (F_CPU) / 4e3) * _ms;if (_tmp 65535)/ _ticks = requested delay in 1/10 ms_ticks = (uint16_t) (_ms * 10.0);while(_ticks)/ wait 1/10 ms _delay_loop_

3、2(F_CPU) / 4e3) / 10);_ticks -;return;else_ticks = (uint16_t)_tmp;_delay_loop_2(_ticks);1、分析程序发现上面两个子函数,分别 using _delay_loop_1() and using_delay_loop2()2、还有一点,用此头文件时,必须设置主频和优化项,否则会出现如下提示:#ifndef F_CPU/* prevent compiler error by supplying a default */# warning F_CPU not defined for # define F_CPU 10

4、00000UL#endif#ifndef _OPTIMIZE_# warning Compiler optimizations disabled; functions from wont work as designed #endif3、通过查找发现_Delay_loop1()和_Delay_loop2()在文件delay_basic.h 中,如下:/* ingroup util_delay_basicDelay loop using an 8-bit counter c _count, so up to 256 iterations are possible. (The value 256

5、would have to be passedas 0.) The loop executes three CPU cycles per iteration, not including the overhead the compiler needs to setup the counter register.Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds can be achieved.*/上面翻译如下:循环变量为 8 位,所以可达 256(其值 256 和 0 等同),每次循环好执行 3 个 CPU 时钟,不包

6、括程序调用和退出该函数所花费的时间。如此,当 CPU 为 1MHZ 时,最大延时为 768us。( 3us*256)void _delay_loop_1(uint8_t _count)_asm_ volatile ( 1: dec %0 ntbrne 1b a a: =r (_count): 0 (_count);/* ingroup util_delay_basicDelay loop using a 16-bit counter c _count, so up to 65536 iterations are possible. (The value 65536 would have to

7、be passed as 0.) The loop executes four CPU cycles per iteration, not including the overhead the compiler requires to setup the counter register pair.Thus, at a CPU speed of 1 MHz, delays of up to about 262.1 milliseconds can be achieved.*/上面翻译如下:循环变量为 16 位,所以可达 65536(其值 65536 和 0 等同),每次循环好执行 4 个 CP

8、U 时钟,不包括程序调用和退出该函数所花费的时间。如此,当 CPU 为 1MHZ 时,最大延时大约为 262.1us。( 4us*65536) void_delay_loop_2(uint16_t _count)_asm_ volatile (1: sbiw %0,1 ntbrne 1b: =w (_count): 0 (_count);4、有了上面的基础就不难得出#include / 头文件/ _delay_loop_1(XX); / 8-bit count, 3 cycles/loop / _delay_loop_2(XXXX); / 16-bit count, 4 cycles/loop

9、#include / 头文件_delay_loop_2(uint16_t _count) 1MHz 时: MAX_DELAY_TIME = (1/1000000)*3*256 = 0.000768 S = 768 uS 8MHz 时: MAX_DELAY_TIME = (1/8000000)*3*256 = 0.000096 S = 96 uS . F_CPU MAX_DELAY_TIME = (1/F_CPU)*3*256 依此类推。 _delay_loop_2(uint16_t _count) 1MHz 时: MAX_DELAY_TIME = (1/1000000)*4*65535 = 0

10、.26214 S = 262.1 mS 8MHz 时: MAX_DELAY_TIME = (1/8000000)*4*65535 = 0.03277 S = 32.8 mS . F_CPU MAX_DELAY_TIME = (1/F_CPU)*4*65535 依此类推。重要提示:_delay_loop_1(0)、_delay_loop_1(256)延时是一样的! 同理, _delay_loop_2(0)、_delay_loop_2(65536)延时也是一样的!这些函数的延时都是最长的延时。重量级函数出场_delay_us() and _delay_ms() !/ _delay_loop_2(X

11、XXX); / 16-bit count, 4 cycles/loop / _delay_loop_1(XX); / 8-bit count, 3 cycles/loop/*/void delay_1ms(void) /1ms 延时函数 主频为 8MHz _delay_loop_2(2000); / 16-bit count,4 cycles/loop / 2000*4/FREQ/使用不同的晶振,可以自己来计算出()里的值/*/void delay_nms(unsigned int n) /N ms 延时函数 unsigned int i=0; for (i=0;in;i+) delay_1ms(); /* */来自: http:/

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号