首先是第一种:
#include<reg52.h>//头文件
unsigned int a; //定义无字符整型变量
void main() //主函数
{
a=50000;//给他一个数,延时用
P2=0xfe;//P2口输出11111110,第一个led灯亮
while (a--);//延时
a=50000;//以下同理
P2=0xfd;
while (a--);
a=50000;
P2=0xfb;
while (a--);
a=50000;
P2=0xf7;
while (a--);
a=50000;
P2=0xef;
while (a--);
a=50000;
P2=0xdf;
while (a--);
a=50000;
P2=0xbf;
while (a--);
a=50000;
P2=0x7f;
while (a--);
}
第二种
#include <reg52.h>//头文件
unsigned int b;//定义无字符型整型变量
unsigned char a,c,d;//定义无字符字节型变量
void main()//主函数
{
a=0x01;//0000 0001 给变量赋初值
while(1)//精准循环
{
if(d==0x00)//0000 0000相当于复位
{
a=0x01;
}
c=~a;//1111 1110把a取反
P2=c;//点亮第一个灯
b=5000;//延时500ms
while (b--);
d=~c;//0000 0001再次取反
d=d<<1;//0000 0010左移1位
a=d;//为点亮第二个led做铺垫
}
}
前者使用多个while耗时间,后者只用了一个while!