📄 horsled.c
字号:
/**********************************************
;horsled.c
作者:胡进
跑马灯程序,采用ATmega8515芯片,在STK500调试
**********************************************/
#include<avr/io.h> //头文件,寄存器定义
#define uchar unsigned char //数据类型说明
#define uint unsigned int //数据类型说明
void DelayMs(uint i) //Ms级延时函数,参数i:延时时间
{uint j;
for(;i!=0;i--)
{for(j=800;j!=0;j--) {;}}
}
void Horse(uchar i) //跑马灯程序,通过步判断点亮相应的LED
{switch(i) //高电平点亮LED
{case 1:PORTB=0x01;break; //0000 0001B 点亮LED1
case 2:PORTB=0x03;break; //0000 0011B 点亮LED1~LED2
case 3:PORTB=0x07;break; //0000 0111B 点亮LED1~LED3
case 4:PORTB=0x0f;break; //0000 1111B 点亮LED1~LED4
case 5:PORTB=0x1f;break; //0001 1111B 点亮LED1~LED5
case 6:PORTB=0x3f;break; //0011 1111B 点亮LED1~LED6
case 7:PORTB=0x7f;break; //0111 1111B 点亮LED1~LED7
case 8:PORTB=0xff;break; //1111 1111B 点亮LED1~LED8
default:break;}
}
void main(void)
{uchar i;
DDRB=0xff; //端口设置:PB口设置为推挽1输出
PORTB=0xff;
//PORTB=0x00; //PORTB初始值为0,灭掉所有的LED
//DelayMs(100);
while(1) //程序无条件执行
{ for(i=0;i<9;i++) //依次点亮LED1~LED8
{Horse(i);
DelayMs(100);} //点亮时间约1秒
for(i=7;i!=0;i--) //依次熄灭LED8~LED1
{Horse(i);
DelayMs(100);} //熄灭时间约1秒
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -