很简单嘛。看我的——
#include <reg51.h>
#define LED0 P0
#define LED1 P2
void Delay10ms(unsigned char c)
{
unsigned char a,b;
for(;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
void main ()
{
unsigned char i;
while (1)
{
//两组高四位交替亮灭3次
for (i=0;i<3;i++)
{
LED0=0x0f;
LED1=0xff;
Delay10ms(50);
LED0=0xff;
LED1=0x0f;
Delay10ms(50);
}
LED0=0xff;
LED1=0xff;
//从P00开始,逐步全亮
for (i=0;i<8;i++)
{
LED0=LED0<<1;
Delay10ms(50);
}
for (i=0;i<8;i++)
{
LED1=LED1<<1;
Delay10ms(50);
}
//全亮开始逐个熄灭
LED0=LED0+1;
Delay10ms(50);
LED0=LED0+2;
Delay10ms(50);
LED0=LED0+4;
Delay10ms(50);
LED0=LED0+8;
Delay10ms(50);
LED0=LED0+16;
Delay10ms(50);
LED0=LED0+32;
Delay10ms(50);
LED0=LED0+64;
Delay10ms(50);
LED0=LED0+128;
Delay10ms(50);
LED1=LED1+1;
Delay10ms(50);
LED1=LED1+2;
Delay10ms(50);
LED1=LED1+4;
Delay10ms(50);
LED1=LED1+8;
Delay10ms(50);
LED1=LED1+16;
Delay10ms(50);
LED1=LED1+32;
Delay10ms(50);
LED1=LED1+64;
Delay10ms(50);
LED1=LED1+128;
Delay10ms(50);
}
} |