#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
//uchar temp,key_num,i;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0};
uchar code scan[4] = {0xfe, 0xfd, 0xfb, 0xf7};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void disp(uchar key_num) //显示子程序
{
P0=table[key_num];
delay(20);
P2=0xf8;
}
uchar keyscan();
void mian()
{
P2=0xf8;
while(1)
{
key=keyscan();
delayms(20);
keydisp(key);
}
}
uchar keyscan()
{
uchar i;
for(i=0;i<4;i++)
{
P1=scan[i];//按键检测
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(10);
P1=scan[i];
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
switch(temp&0xf0)
{
case 0xe0: key_num=i*4;
break;
case 0xd0: key_num=i*4+1;
break;
case 0xb0: key_num=i*4+2;
break;
case 0x70: key_num=i*4+3;
break;
}
}
}
}
return key_num;
}
这是我自己写的一个键盘扫描的程序,编译没错,就是显示不出来,不知道是什么问题,请高手帮忙解答。
P1口接4*4矩阵键盘,P0口接数码管,P2.0-P2.3控制数码管的位选 |