 |
首先,祝贺我成功地点亮了LED灯! 
|
 |
 电路: 第1脚接正常的AVR RESET电路,和ATMega16一样,电容10μF。 第3脚通过一个200欧姆的电阻接发光二极管的正极。 然后按照ATMega16的电路图接上ISP下载口。 
|
 |
 如图,插好USBASP。图中的两个HC595和一个8x8LED不用管,我以前就焊好了。
|
 |
然后编写一个C文件,文件名tiny.c: #include <avr/io.h> #include <avr/eeprom.h>
int main() { DDRB |= _BV(4); //把PB4设置为输出 PORTB |= _BV(4); //把PB4设置为高电平 eeprom_busy_wait(); eeprom_write_byte(0x00, 0xa5); eeprom_busy_wait(); eeprom_write_byte(0x01, 0xb6); eeprom_busy_wait(); eeprom_write_byte(0x02, 0xc7); eeprom_busy_wait(); eeprom_write_byte(0x03, PORTB); eeprom_busy_wait(); eeprom_write_byte(0x04, DDRB); eeprom_busy_wait(); eeprom_write_byte(0x05, PINB); eeprom_busy_wait(); while (1); } 为了方便起见,笔者是在Linux下用的avrdude+gccavr进行编译的程序。在Windows平台上和WinAVR等价。用ICC AVR的话就得把头文件改了,还要把_BV改成BIT。
|
 |
Makefile: # make tiny.hex: tiny.c avr-gcc -mmcu=attiny13 -Wall -Os tiny.c -o tiny.o avr-objcopy -j .text -j .data -O ihex tiny.o tiny.hex
# make run run: tiny.hex sudo avrdude -p t13 -c usbasp -e -U flash:w:tiny.hex
|
 |
在终端中执行make run(也可以先make后make run),把程序下载到芯片中。程序的运行结果是:LED亮了! 
|
 |
同时,程序运行后获得的EEPROM内容如下:  这说明, PORTB = 0x10 (00010000) DDRB = 0X10 (00010000) PINB = 0x18 (00011000)
|
 |
PORTB = 0x10 (00010000) 左边第一位是PB7(当然ATtiny13根本没这个端口) 左边第4位就是PB4,PORTB中只有这一位为1 右边最后一位是PB0
|
 |
现在删掉EEPROM部分的代码。 #include <avr/io.h>
int main() { DDRB |= _BV(4); //把PB4设置为输出 PORTB |= _BV(4); //把PB4设置为高电平 while (1); } 程序运行结果:LED亮
#include <avr/io.h>
int main() { DDRB |= _BV(4); //把PB4设置为输出 PORTB &= ~_BV(4); //把PB4设置为低电平 while (1); } 程序运行结果:LED灭
|
 |
10樓
巨大八爪鱼
2015-7-18 18:09
令我奇怪的是,之前不能运行的以下程序: #include <avr/io.h>
int main() { DDRB = 0xff; PORTB = 0xff; while (1) { PORTB = 0xff; } } 现在居然又能正常运行了。。。我靠!所以这个原因暂时还没找到。。。
|