#include < reg52.h >
#include <intrins.h>//包括crol移位子函数的头文件
#define uchar unsigned char
#defind uintunsigned int
uchar key_value=0x19;
uchar temp,a;
uchar keyboard()
{
uchari,t=4;
P2=0xFF;//P2口写1
a=0x7F;//p_value赋初始值
i=1;
while(t–)
{
a=_crol_(a,1);//循环左移一位,检测下一行
key_scan();
i++;
if(i==5){ i=1; a=0x7f;}
}
returnkey_value;
}
void key_scan()
{
P2=a;//P2.0置零
temp=P2; //读P2口的值
temp=temp&0xF0;//取高四位,都得用temp,即temp=p2,temp=temp&0xf0;
while(temp!=0xF0)//假如高四位不全为0,则有键下
{
delay(5);//延时消抖
while(temp!=0xF0)//再读一次,假如高四位仍是不全为0,真的有键按下了
{
temp=P2;//读P2口的值给temp
switch(temp)
{
case0xEE:key_value=0; break;
case0xDE:key_value=1;break;
case0xBE:key_value=2;break;
case0x7E:key_value=3;break;
case0xED:key_value=4;break;
case0xDD:key_value=5;break;
case0xBD:key_value=6;break;
case0x7D:key_value=7;break;
case0xEB:key_value=8;break;
case0xDB:key_value=9;break;
case0xBB:key_value=10; break;
case0x7B:key_value=11; break;
case0xE7:key_value=12; break;
case0xD7:key_value=13; break;
case0xB7:key_value=14; break;
case0x77:key_value=15; break;
}
while(temp!=0xf0)//松手检测
{
temp=P2;
temp=temp&0xf0;
}
}
}
}
void delay(uint z)
{
uintm,n;
for(m=z;m>0;m–)
for(n=78;n>0;n–);
}