CODE:
//ICC-AVR application builder
// Target : M16
// Crystal: 1.0000Mhz
// AVR与虚拟仪器 古欣 [url]http://www.avrvi.com[/url]
// 内部1M晶振 ,如果使用7.3728M外部晶振,请短接跳线JP2的1和2,这样闪烁会很快。
// 开发板连接:LED0~7——PA0~7,电源跳线连接3.3V或者5V
#include <iom16v.h>
#include <macros.h>
#define led_port PORTA
//定义输出端口
#define led_ddr DDRA
//定义输出控制寄存器
//可以自行修改,必须保持 led_port和led_ddr一致,PORTA对应DDRA
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
led_ddr=0xff; //设置LED的端口为输出
}
//call this routine to initialize all peripherals
//此处为ICC系统生成,未做更改
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//延时,没有详细计算
void Delay(void) {
unsigned char i,j;
for(i=20;i>0;i--)
{
for(j=200;j>0;j--)
;
}
}
void main(void)
{
unsigned char i,j,k; //定义变量
init_devices(); //初始化
while(1)
{
i=1;
for (j=0;j<8;j++) //循环8次,即PA0~~PA7轮流闪亮
{
led_port=~i; //反相输出,低电平有效 位操作指令详解:[url]http://bbs.avrvi.com/read.php?fid=30&tid=392&toread=1[/url]
for (k=0;k<10;k++) Delay(); //延时 可自行调节
i=i<<1; //左移一位 移位算法详解:[url]http://bbs.avrvi.com/read.php?fid=5&tid=897&toread=1[/url]
// 0b00000001 PA0
// 0b00000010 PA1
// 0b00000100 PA2
// 0b00001000 PA3
// 0b00010000 PA4
// 0b00100000 PA5
// 0b01000000 PA6
// 0b10000000 PA7
}
}
}