AT89S52鍵盤程序
/********************************************************************/
本文引用地址:http://m.butianyuan.cn/article/201611/316304.htm//鍵盤掃描子程序 獲取鍵值函數(shù)
/****************************************************************************/
uchar keyscan(void)
{ uchar temp;
P0=0xfe; //第一行置低
temp=P0;
temp&=0xf0;
if(temp!=0xf0)
{
delay_nms(10);
if(temp!=0xf0)
{
switch(temp) //根據(jù)列線獲取第一行的鍵值
{
case 0xe0: key=3; break;
case 0xd0: key=2; break;
case 0xb0: key=1; break;
case 0x70: key=0; break;
}
}
}
P0=0xfd; //掃描第二行,置低
temp=P0;
temp&=0xf0;
if(temp!=0xf0)
{
delay_nms(10);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: key=7; break;
case 0xd0: key=6; break;
case 0xb0: key=5; break;
case 0x70: key=4; break;
}
}
}
P0=0xfb; //掃描第三行,置低
temp=P0;
temp&=0xf0;
if(temp!=0xf0)
{
delay_nms(10);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: key=11; break;
case 0xd0: key=10; break;
case 0xb0: key=9; break;
case 0x70: key=8; break;
}
}
}
P0=0xf7; //掃描第四行,置低
temp=P0;
temp&=0xf0;
if(temp!=0xf0)
{
delay_nms(10);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: key=15; break;
case 0xd0: key=14; break;
case 0xb0: key=13; break;
case 0x70: key=12; break;
}
}
}
else
{
;
}
return(key);
}
評論