#include <reg52.h>
void delay(int t){ //时间延迟子函数
unsigned int i,j;
for(j=0;j<30;j++)
for(i=0;i<t;i++);
}
void putOne(int n,char a){ //定义变量函数
unsigned int m = n%8, k = n/8, b;
if(k==0) b=P0; if(k==1) b=P1; if(k==2) b=P2; if(k==3) b=P3;
if(a) b = b & (~(1<<m));
else b = b | (1<<m) ;
if(k==0) P0=b; if(k==1) P1=b; if(k==2) P2=b; if(k==3) P3=b;
}
void liu(int n1,int n2){ //流水灯子程序
int i;
for(i=n1; i<=n2; i++ ) {
putOne(i,1);
delay(2000);
putOne(i,0);
}
}
main() //流水灯主程序
{
unsigned int k;
while(1)
{
P0 = P1 = P2 =P3 = 0;
for(k=31;k>-1;k--)
{
liu(0, k); putOne(k,1);
}
P0 = P1 = P2 =P3 =0; delay(2000);
P0 = P1 = P2 =P3 =255; delay(2000);
P0 = P1 = P2 =P3 =0; delay(2000);
}
}
希望此帖能抛砖引玉.
楼主最近还看过