|
在WinAVR中,有一个delay.h文件,里面包含了_delay_us和_delay_ms两个函数,分别可以延时微秒和毫秒时间。看下面一个例子:
#include <avr/io.h>
#include <util/delay.h>
unsigned char t = 10;
int main()
{
while(1)
{
_delay_ms(10);
}
return 0;
}
编译后,程序空间占用为:
AVR Memory Usage
----------------
Device: atmega168
Program: 180 bytes (1.1% Full)
(.text + .data + .bootloader)
Data: 2 bytes (0.2% Full)
(.data + .bss + .noinit)
Build succeeded with 0 Warnings...
如果将上面程序稍作修改:
#include <avr/io.h>
#include <util/delay.h>
unsigned char t = 10;
int main()
{
while(1)
{
_delay_ms(t);
}
return 0;
}
Device: atmega168
Program: 3844 bytes (23.5% Full)
(.text + .data + .bootloader)
Data: 266 bytes (26.0% Full)
(.data + .bss + .noinit)
Build succeeded with 0 Warnings...
发现RAM和ROM的占用突然就急剧增加了。这是为什么呢?从程序直接看,没有什么问题。打开delay.h文件,我们发现有一段说明:
In order for these functions to work as intended, compiler optimizations must be enabled, and the delay time must be an expression that is a known constant at compile-time. If these requirements are not met, the resulting delay will be much longer (and basically unpredictable), and applications that otherwise do not use floating-point calculations will experience severe code bloat by the floating-point library routines linked into the application.
上面这段话说明delay函数需要使用常数作为延时的参数,否则延时的结果可能会很长(不可预知),同时会因为链接了浮点库到程序,所以程序大小会急剧膨胀。
参考文献:
http://blog.ednchina.com/shaoziyang/139750/message.aspx |
|