51单片机矩阵键盘扫描程序
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
sbit key1=P3^4;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0};
uchar code KEY_TABLE[] =
{
0xEE, 0xDE, 0xBE, 0x7E,
0xED, 0xDD, 0xBD, 0x7D,
0xEB, 0xDB, 0xBB, 0x7B,
0xE7, 0xD7, 0xB7, 0x77
};
uchar temp,i,key;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
uchar keyscan();
void display(uchar aa);
void main()
{
i=16;
dula=1;
P0=0;
dula=0;
wela=1;
P0=0xc0;
wela=0;
while(1)
{
display(keyscan());
}
}
void display(uchar aa)
{
dula=1;
P0=table[aa];
dula=0;
}
uchar keyscan()
{
P3 = 0xF0;
if (P3 != 0xF0)
{
delay(5);
if (P3 != 0xF0)
{
temp = P3;
P3 = 0x0F;
key = temp | P3;
for (i = 0; i < 16; ++i)
if (key == KEY_TABLE[i])
break;
}
}
return (i);
}
|