#include <reg51.h>
#include <absacc.h>
#define uchar unsigned char
#define uint unsigned int
void delays(void);
uchar kbscan(void);
void main(void)
{
uchar key;
while(1)
{
key=kbscan(); /*鍵掃描函數(shù)*/
delays(); /*鍵消抖的延時函數(shù)*/
}
}
// 鍵消抖的延時函數(shù)
void delays(void)
{
uchar i;
for(i=300;i>0;i--);
}
// kbscan(void) 鍵掃描函數(shù)
uchar kbscan(void)
{
uchar sccode,recode;
P1=0xf0; /*發(fā)全"0"行掃描碼,列線輸入*/
if((P1&0xf0)!=0xf0) /*若有鍵按下*/
{
delays(); /*延時去抖動*/
if((P1&0xf0)!=0xf0)
{
sccode=0xfe; /*逐行掃描初值*/
while((sccode&0x10)!=0)
{
P1=sccode; /*輸出行掃描碼*/
if((P1&0xf0)!=0xf0) /*本行有鍵按下*/
{
recode=(P1&0xf0)|0x0f;
return((~sccode)+(~recode)); /*返回特征字節(jié)碼*/
}
else sccode=(sccode<<1)|0x01; /*行掃描碼左移一位*/
}
}
}
return(0); /*無鍵按下,返回值為0*/
}