這個就是把 先奇數亮再偶數亮,循環三次;一個燈上下循環三次;兩個分別從兩邊往中間流動三次;再從中間往兩邊流動三次;不過這個程序實現的 應該是這樣的 先奇數亮再偶數亮,循環三次;一個燈上下循環三次;兩個分別從兩邊往中間流動;再從中間往兩邊流動;
#include< reg52.h>
#include< intrins.h>
#define uint unsigned int
#define uchar unsigned char
void delay();
void main()
{ uchar temp,temp1,temp2,a,b;
P3=0xff;
while(1)
{ for(b=3;b>0;b--)
{ temp=0xaa;
P1=temp;
delay();
temp=0x55;
P1=temp;
delay();
}
for(a=3;a>0;a--)
{ temp=0xfe;
for(b=7;b>0;b--)
{ P1=temp;
temp=_crol_(temp,1);
delay();
P1=temp;
delay();
}
}
temp1=0xfe;
temp2=0x7f;
for(a=8;a>0;a--)
{
temp=temp1&temp2;
P1=temp;
delay();
temp1=_crol_(temp1,1);
temp2=_cror_(temp2,1);
}
}
}
void delay()
{ uint a,b;
for(a=100;a>0;a--)
for(b=600;b>0;b--);
}
程序實現的第2種方法:下面是http://www.zg4o1577.cn單片機12群里的朋友木信大俠提出的,實現單片機led流水燈從中間向兩邊,從兩邊向中間的效果,下面的程序就是實現思路,這個是直接調用,應該明白吧,數組那其實也可以改一下,如采用一維數組,在多次調用;也可以采用二維數組。主要看個人喜好.
-----------------------------------------------------
#include< reg52.h>
unsigned char code TABLE[]={
0xFF,0xE7,0xC3,0x81,0x00, //從中間向兩側點亮
0xFF,0xE7,0xC3,0x81,0x00,
0xFF,0xE7,0xC3,0x81,0x00,
0xFF,0x7E,0x3C,0x18,0x00, //從兩側向中間點亮
0xFF,0x7E,0x3C,0x18,0x00,
0xFF,0x7E,0x3C,0x18,0x00
};
unsigned char i;
DELAY()
{
unsigned int s;
for(s=0;s<30000;s++);
}
main()
{
while(1)
{
if(TABLE[i]!=0x01)
{
P1=TABLE[i];
i++;
DELAY();
}
else
{
i=0;
}
}
}