我现在正在教我的朋友如何对 AVR 微控制器进行编程。我们编写了这个小程序,它发送简单的类似莫尔斯电码。
问题是,在用 AVR-GCC 和 WinAVR 编译之后,a.out 文件几乎是 30KB,而 hex 文件是 11KB,所以它不适合 attiny2313 闪存。
WinAVR 命令:avr-gcc -mmcu=attiny2313 -Os -g main.c
avr-objcopy:avr-objcopy -O ihex a.out a.hex
这是代码:
#define F_CPU 8000000L
#include <avr/io.h>
#include <util/delay.h>
void light_led(int ms)
{
PORTD |= (1 << 4);
_delay_ms(ms);
PORTD &= ~(1 << 4);
_delay_ms(1000);
}
void send_char(int c)
{
int i;
for(i = 1; i < 8+1; i++)
{
if(c & i) light_led(1000);
else light_led(500);
}
}
int main(void)
{
DDRD |= (1 << 4);
//char text[] = {'t', 'e', 's', 't'};
int i;
for(i = 0; i < 1; i++) send_char(100);//text[i]);
return 0;
}