(P0一般接10K的上拉电阻,此图复位电路没给出)
当然,有些比较有心的同学会做些有把戏的,如这种“心”形的流水灯
下面试代码:
(1)用总线的办法完成流水灯
#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)移位完成流水灯
#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)用库函数完成流水灯
#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);
}
}