3種方法實現(xiàn)流水燈
當(dāng)然,有些比較有心的同學(xué)會做些有花樣的,如這種“心”形的流水燈
下面試代碼:
(1)用總線的方法實現(xiàn)流水燈
#include
#define uchar unsigned char
#define uint unsigned int
uchartable[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delay(uint xms) //延時約1ms
{
uintx,y;
for(x=110;x>0;x--)
for(y=xms;y>0;y--);
}
void main()
{
uchar i;
while(1)
{
for(i=0;i<8;i++)
{
P0=table[i];
delay(500);
}
}
}
(2)移位實現(xiàn)流水燈
#include
#define uchar unsigned char
#define uint unsigned int
void delay(uint xms) //延時約1ms
{
uintx,y;
for(x=110;x>0;x--)
for(y=xms;y>0;y--);
}
void main()
{
uchar a,i;
while(1)
{
a=0xfe;
for(i=0;i<8;i++)
{
P0=a;
a=a<<1;
a=a|0x01;
delay(500);
}
}
}
(3)用庫函數(shù)實現(xiàn)流水燈
#include
#include
#define uchar unsigned char
#define uint unsigned int
void delay(uint xms) //延時約1ms
{
uint x,y;
for(x=110;x>0;x--)
for(y=xms;y>0;y--);
}
void main()
{
uint a;
a=0xfe;
while(1)
{
P2=a;
a=_crol_(a,1);
delay(500);
}
}
評論