📄 key1.c
字号:
/**************************************************
;键控流水灯程序
**************************************************/
#include "reg51.h"
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
uchar LampCode1=0x01;
bit UpDown=0; //上下流动标志
bit StartEnd=0; //起动及停止标志
/*延时程序
由Delay参数确定延迟时间
*/
void mDelay(unsigned int Delay)
{ unsigned int i;
for(;Delay>0;Delay--)
{ for(i=0;i<124;i++)
{;}
}
}
void KProce(uchar KValue) //键值处理
{ if((KValue&0x04)==0)
StartEnd=1;
if((KValue&0x08)==0)
StartEnd=0;
if((KValue&0x10)==0)
UpDown=1;
if((KValue&0x20)==0)
UpDown=0;
}
uchar Key()
{ uchar KValue;
uchar tmp;
P3|=0x3c; //将P3口的接键盘的中间四位置1
KValue=P3;
KValue|=0xc3; //将未接键的4位置1
if(KValue==0xff) //中间4位均为1,无键按下
return(0); //返回
mDelay(10); //延时10ms,去键抖
KValue=P3;
KValue|=0xc3; //将未接键的4位置1
if(KValue==0xff) //中间4位均为1,无键按下
return(0); //返回
//如尚未返回,说明一定有1或更多位被按下
for(;;)
{ tmp=P3;
if((tmp|0xc3)==0xff)
break; //等待按键释放
}
return(KValue);
}
void main()
{ uchar KValue; //存放键值
uchar LampCode; //存放流动的数据代码
P1=0xff; //关闭所有灯
LampCode=0xfe;
for(;;)
{ KValue=Key(); //调用键盘程序并获得键值
if(KValue) //如果该值不等于0
{ KProce(KValue); //调用键盘处理程序
}
if(StartEnd) //要求流动显示
{
P1=LampCode;
if(UpDown) //要求由上向下
{ LampCode=_cror_(LampCode,1);
}
else //否则要求由下向上
{ LampCode=_crol_(LampCode,1);
}
mDelay(500); //延时
}
else //关闭所有显示
{ P1=0xff;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -