求助51单片机流水灯程序

要实现下图中的功能,可以有偿帮助,求大神

第1个回答  2017-10-20
第4题
89S51 单片机的P1 口接有 8个 LED,当某一端口输出为“0”时,相应的 LED 点亮,P3.2、P3.3、P3.4、P3.5 分别接有四个按钮 K1~K4,按下按钮时,相应引脚被接地。现要求编写可键控的流水灯程序,当 K1 按下时,开始流动,K2 按下时停止流动,全部灯灭,K3 使灯由上往下流动,K4 使灯由下往上流动。
#include "reg51.h"
#include "intrins.h"
#define uchar unsigned char
void mDelay(unsigned int DelayTime)
{ unsigned int j=0;
for(;DelayTime>0;DelayTime--)
{ for(j=0;j<125;j++)
{;} }}
uchar Key()
{ uchar KeyV;
uchar tmp;
P1=P1|0x3c; //四个按键所接位置
KeyV=P1;
if((KeyV|0xc3)==0xff) //无键按下
return(0);
mDelay(10); //延时,去键抖
KeyV=P1;
if((KeyV|0xc3)==0xff)
return(0);
else
{ for(;;){ tmp=P1;
if((tmp|0xc3)==0xff)
break;}
return(KeyV);}}

void main()
{ unsigned char OutData=0xfe;
bit UpDown=0;
bit Start=0;
uchar KValue;
for(;;)
{ KValue=Key();
switch (KValue)
{ case 0xfb: //P3.2=0,Start
{ Start=1;
break; }
case 0xf7: //P3.3=0,Stop
{ Start=0;
break; }
case 0xef: //P3.4=0 Up
{ UpDown=1;
break; }
case 0xdf: //P3.5=0 Down
{ UpDown=0;
break; }
}
if(Start)
{ if(UpDown)
OutData=_crol_(OutData,1);
else
OutData=_cror_(OutData,1); P2=OutData;
}
else
P2=0xff; //否则灯全灭
mDelay(1000);
}
}本回答被网友采纳