#include <REGX52.H>
#define KeyPort P1
unsigned char code Seg_Tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x66,0x8f,0xff};
void Delayms(unsigned int x)
{
unsigned int t;
while(x--)
{
for(t=0;t<120;t++);
}
}
//P1口低4位行线
//P1口高4位列线
//矩阵键盘线反转法
unsigned char KeyScan(void)//按键扫描函数
{
unsigned char row,col;
KeyPort=0x0f;
if((KeyPort&0x0f)!=0x0f)//低4位为行
{
Delayms(10);
if((KeyPort&0x0f)!=0x0f)
{
KeyPort=0x0f;//行线拉高,列线拉低,准备读行线值
row=KeyPort&0x0f;
KeyPort=0xf0;//列线拉高,行线拉低,准备读列线值
col=KeyPort&0xf0;//读列线值
while((KeyPort&0xf0)!=0xf0);//等待按键松开
return(row|col);
}
}
return 0xff;
}
unsigned char KeyPro(unsigned char Key_val)//按键处理函数,根据键值求键号
{
switch(Key_val)//高4位为列
{
case 0xee:return 0;break;//1110 1110
case 0xde:return 1;break;//1101
case 0xbe:return 2;break;//1011
case 0x7e:return 3;break;//0111
case 0xed:return 4;break;//1110 1101
case 0xdd:return 5;break;
case 0xbd:return 6;break;
case 0x7d:return 7;break;
case 0xeb:return 8;break;//1110 1011
case 0xdb:return 9;break;
case 0xbb:return 10;break;
case 0x7b:return 11;break;
case 0xe7:return 12;break;//1110 0111
case 0xd7:return 13;break;
case 0xb7:return 14;break;
case 0x77:return 15;break;
default:return 16;break;
}
}
void main()
{
unsigned char num,key_NO;
while(1)
{
key_NO=KeyScan();
num=KeyPro(key_NO);
if(num<16)
{
P0=Seg_Tab[num];
}
}
}
01-14
6623
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-27
1849
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-21
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交