avr-gcc安装与使用
1.安装binutils
下载地址:http://ftp.gnu.org
#tar -jxvf binutils-2.21.tar.bz2
#cd binutils-2.21
#./configure --target=avr --prefix=/usr/local/atmel
#make
#make install
添加 /usr/local/atmel/lib 到 /etc/ld.so.conf文件中
添加 export PATH=${PATH}:/usr/local/atmel/bin到 /etc/bashrc
配置说明:
--target=avr
--prefix=/usr/local/atmel
2.安装avr-gcc编译器
下载地址:http://ftp.gnu.org
#tar -jxvf gcc-core-4.1.2.tar.bz2
#cd gcc-4.1.2
#./configure --target=avr --prefix=/usr/local/atmel --disable-nls --enable-language=c --disable-libssp
#make
#make install
配置说明:
--target=avr
--prefix=/usr/local/atmel
--enable-language=c //这里也可以添加c++
--disable-nls
--disable-libssp //新的gcc版本可能参需要此选项,否则编译时出错
3.安装avr c语言库
下载地址:http://savannah.nongnu.org/
设置一些环境变量(bash语法):
#export CC=avr-gcc
#export AS=avr-as
#export AR=avr-ar
#export RANLIB=avr-ranlib
编译安装
#tar -jxvf avr-libc-1.7.0.tar.bz2
#cd avr-libc-1.7.0
#./configure --target=avr --prefix=/usr/local/atmel --host=avr
#make
#make install
配置说明:
--target=avr
--prefix=/usr/local/atmel
--host=avr
注意事项:
本文档在RHEL5.4中安装成功;
各工具版本不一定要最新,能用即可,特别是gcc-core最好不要比当前系统运行的gcc版本高,否则有可能不能配置和编译。
如果编译gcc的时候出下面的错误:
libssp/ssp.c:168: error: too many arguments to function 'fail'
配置时候添加 --disable-libssp参数即可。
测试程序
/************* 测试程序 *********/
#include <avr/io.h>
//简单的延时程序
void delayms(unsigned char ntime)
{
unsigned char temp;
while(ntime){
ntime--;
temp = 0xff;
while(temp){temp--;}
temp = 0xff;
while(temp){temp--;}
temp = 0xff;
while(temp){temp--;}
temp = 0xc0;
while(temp){temp--;}
}
}
//主程序
int main(void)
{
delayms(200);
DDRA = 0xFF;
PORTA = 0x55;
delayms(200);
//程序主循环
while(1)
{
PORTA = ~PORTA;
delayms(200);
asm("NOP"); //嵌入汇编
}
return 0;
}
Makefile
################## Makefile ############################
PRG = main
OBJ = $(PRG).o
MCU_TARGET = atmega32
OPTIMIZE =
DEFS =
LIBS =
#################指定编译器类型#########################
CC = avr-gcc
#######################################################
# Override is only needed by avr-lib build system.
override CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS)
override LDFLAGS = -Wl,-Map,$(PRG).map
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
all: $(PRG).elf text $(PRG).lst
$(PRG).elf: $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
lst: $(PRG).lst
%.lst: %.elf
$(OBJDUMP) -h -S $< > $@
#######################################################
# Rules for building the .text rom images
text: hex bin
hex: $(PRG).hex
bin: $(PRG).bin
%.hex: %.elf
$(OBJCOPY) -j .text -j .data -O ihex $< $@
%.bin: %.elf
$(OBJCOPY) -j .text -j .data -O binary $< $@
#######################################################
clean:
rm -rf *.o *.elf *.hex *.bin
rm -rf *.lst *.map
#######################################################