#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);
}
}
希望此帖能拋磚引玉.
[ 此帖被我的工控在2020-03-01 01:29重新編輯 ]