#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar temp;
uchar key();
void main()
{
while(1)
{
key();
}
}
uchar key()
{
P1=0xfe;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xee:P0=0xfe;
break;
case 0xde:P0=0xfd;
break;
case 0xbe:P0=0xfb;
break;
case 0x7e:P0=0xf7;
break;
}
temp=temp&0xf0;
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xed:P0=0xef;
break;
case 0xdd:P0=0xdf;
break;
case 0xbd:P0=0xbf;
break;
case 0x7d:P0=0x7f;
break;
}
temp=temp&0xf0;
}
}
P1=0xfb;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xeb:P3=0xfe;
break;
case 0xdb:P3=0xfd;
break;
case 0xbb:P3=0xfb;
break;
case 0x7b:P3=0xf7;
break;
}
temp=temp&0xf0;
}
}
P1=0xf7;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xe7:P3=0xef;
break;
case 0xd7:P3=0xdf;
break;
case 0xb7:P3=0xbf;
break;
case 0x77:P3=0x7f;
break;
}
temp=temp&0xf0;
}
}
}
关于键盘的简单使用
本文介绍了一个基于51单片机的按键扫描程序实现方法。通过对外部按键的状态进行连续读取和判断,实现了对按键信号的有效识别,并针对不同的按键状态输出相应的响应。该程序使用了嵌套循环和条件判断来确保准确地检测到按键按下及释放的过程。

被折叠的 条评论
为什么被折叠?



