|
【教程】Linux下烧写AVR单片机,以ATMega16为例 |
一派护法 十九级 |
首先安装: sudo yum install gcc sudo yum install avrdude sudo yum install avr-gcc sudo yum install binutils sudo yum install avr-libc
我安装的版本是: Package gcc-4.8.1-1.fc19.x86_64 already installed and latest version Package avrdude-5.11.1-4.fc19.x86_64 already installed and latest version Package avr-gcc-4.7.3-0.1.fc19.x86_64 already installed and latest version Package binutils-2.23.52.0.1-9.fc19.x86_64 already installed and latest version Package avr-libc-1.8.0-5.fc19.noarch already installed and latest version
[octopus@pc3 ~]$ sudo yum install gcc Loaded plugins: langpacks, refresh-packagekit Package gcc-4.8.1-1.fc19.x86_64 already installed and latest version Nothing to do [octopus@pc3 ~]$ sudo yum install avrdude Loaded plugins: langpacks, refresh-packagekit Package avrdude-5.11.1-4.fc19.x86_64 already installed and latest version Nothing to do [octopus@pc3 ~]$ sudo yum install avr-gcc Loaded plugins: langpacks, refresh-packagekit Package avr-gcc-4.7.3-0.1.fc19.x86_64 already installed and latest version Nothing to do [octopus@pc3 ~]$ sudo yum install binutils Loaded plugins: langpacks, refresh-packagekit Package binutils-2.23.52.0.1-9.fc19.x86_64 already installed and latest version Nothing to do [octopus@pc3 ~]$ sudo yum install avr-libc Loaded plugins: langpacks, refresh-packagekit Package avr-libc-1.8.0-5.fc19.noarch already installed and latest version Nothing to do [octopus@pc3 ~]$
下面的程序引用的文件是avr/io.h,如果文件不存在就检查一下avr-libc是否安装好,我用的系统是fedora19
|
一派护法 十九级 |
【示例程序】 【demo1.c】 #include <avr/io.h>
void main(void) { unsigned char i,j,k=0; DDRB=PORTB=0xff; while (1) { PORTB=~PORTB; //if (PINB&BIT(0)) // PORTB&=~BIT(0); //else // PORTB|=BIT(0); for (i=0;i<255;i++) for (j=0;j<255;j++) for (k=0;k<13;k++); } }
该程序可以使PB口上的LED闪烁起来,时间大约是一秒(很不精确)。
|
一派护法 十九级 |
【Makefile】 demo: demo1.c avr-gcc -mmcu=atmega16 -c demo1.c avr-gcc -mmcu=atmega16 -o demo1.elf demo1.o avr-objcopy -j .text -j .data -O ihex demo1.elf demo1.hex sudo avrdude -p m48 -c usbasp -e -U flash:w:demo1.hex -F
|
一派护法 十九级 |
然后插上USBAsp下载器,输入make就可以编译并下载了哦
|
一派护法 十九级 |
[octopus@pc3 t1]$ make avr-gcc -mmcu=atmega16 -c demo1.c avr-gcc -mmcu=atmega16 -o demo1.elf demo1.o avr-objcopy -j .text -j .data -O ihex demo1.elf demo1.hex sudo avrdude -p m48 -c usbasp -e -U flash:w:demo1.hex -F [sudo] password for octopus:
avrdude: warning: cannot set sck period. please check for usbasp firmware update. avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.01s
avrdude: Device signature = 0x1e9403 avrdude: Expected signature for ATMEGA48 is 1E 92 05 avrdude: erasing chip avrdude: warning: cannot set sck period. please check for usbasp firmware update. avrdude: reading input file "demo1.hex" avrdude: input file demo1.hex auto detected as Intel Hex avrdude: writing flash (212 bytes):
Writing | ################################################## | 100% 0.13s
avrdude: 212 bytes of flash written avrdude: verifying flash memory against demo1.hex: avrdude: load data flash data from input file demo1.hex: avrdude: input file demo1.hex auto detected as Intel Hex avrdude: input file demo1.hex contains 212 bytes avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.06s
avrdude: verifying ... avrdude: 212 bytes of flash verified
avrdude: safemode: Fuses OK
avrdude done. Thank you.
[octopus@pc3 t1]$
|