矩阵键盘问题
//键盘扫描,线反转法
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit weila=P2^7;
sbit duanla=P2^6;
void delay(uint);
uchar num;
unsigned char code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00
};
uchar keyValue;
void keyscan();
void keyHandle();
void main()
{
weila=1;
P0=0x0f;
weila=0;
num=0;
while(1)
{
keyscan();
keyHandle();
P0=table[num];
duanla=1;
duanla=0;
}
}
void delay(uint z)
{
int x,y;
for(x=z; x>0; x--)
for(y=110; y>0; y--);
}
void keyscan()
{
uchar x,y;
P3=0xf0;
if((P3 & 0xf0)!=0xf0)
{
delay(10);
if((P3 & 0xf0)!=0xf0)
{
x=P3 & 0xf0;
P3=0x0f;
y=P3 & 0x0f;
while((P3&0x0f)!=0x0f);
//delay(5);
//if((P3&0x0f)!=0x0f);//松手检测
keyValue=x | y;
}
}
}
void keyHandle()
{
switch(keyValue)
{
case 0xee: num=(num+1)%16; break;//目的让数码管值加一,但总显示8,为什么?
case 0xde: num=1; break;
case 0xbe: num=2; break;
case 0x7e: num=3; break;
case 0xed: num=4; break;
case 0xdd: num=5; break;
case 0xbd: num=6; break;
case 0x7d: num=7; break;
case 0xeb: num=8; break;
case 0xdb: num=9; break;
case 0xbb: num=10;break;
case 0x7b: num=11;break;
case 0xe7: num=12;break;
case 0xd7: num=13;break;
case 0xb7: num=14;break;
case 0x77: num=15;break;
}
}