写了矩阵键盘的程序(扫描法) 可是按0~3按键就是不显示,4~F显示正常,想了一天了 没找出原因 有空的帮忙看看 代码如下:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
void delay1ms(uchar i);
void delay1ms(uchar i)
{ uchar x,y;
for(;i<0;i--)
{
for(x=0;x<10;x++)
{ for(y=0;y<50;y++);}
}
}
uchar quzhi();
void main()
{uchar a;
a=quzhi(); //调用取键码函数
switch(a)
{
case 0xee: P1=0xc0; break;
case 0xde: P1=0xf9; break;
case 0xbe: P1=0xa4; break;
case 0x7e: P1=0xb0; break;
case 0xed: P1=0x99; break;
case 0xdd: P1=0x92; break;
case 0xbd: P1=0x82; break;
case 0x7d: P1=0xf8; break;
case 0xeb: P1=0x80; break;
case 0xdb: P1=0x90; break;
case 0xbb: P1=0x88; break;
case 0x7b: P1=0x83; break;
case 0xe7: P1=0xc6; break;
case 0xd7: P1=0xa1; break;
case 0xb7: P1=0x86; break;
case 0x77: P1=0x8e; break;
}
}
uchar quzhi()
{
uchar a,b=0xfe,c;
P2=0x0f;
while((P2&0x0f)!=0x0f)
{delay1ms(10); //延时去抖动
if((P2&0x0f)!=0x0f)
{a=P2&0xf0; //屏蔽低四位
P2=b; //将扫描初值送给P2
while((P2&0xf0)!=0xf0)
{ c=P2&0x0f;
a=a+c;
return (a);
}
P2=b;
P2<<=0x01;
P2=P2|0x01;
b=P2;
}
}
return(0);
} |