|
#include
#define uchar unsigned char
sbit IO1=P1^6;
sbit IO2=P1^7;
uchar keybuf,keydat;
bit bkeyok;
void delay_us(unsigned int n)
{
while(--n);
}
void delay_ms(unsigned int n)
{
do
{
delay_us(1000);
}
while(--n);
}
bit scan_key(void)
{
uchar tempkey;
IO1=1;IO2=1;
if(!IO1)
{
if (!IO2)
tempkey=3;
else
tempkey=2;
}
else
{
if (!IO2)
tempkey=1;
else
{
IO1=0;
if (!IO2)
{
IO1=1;IO2=0;
if (!IO1)
tempkey=4;
else
tempkey=6;
}
else
{
IO1=1;IO2=0;
if (!IO1)
tempkey=5;
else
tempkey=0;
}
}
}
IO1=1;IO2=1;
if((keybuf !=0) && (keybuf == tempkey))
{
bkeyok=1;
keydat=tempkey;
}
keybuf=tempkey;
return(bkeyok);
}
uchar read_key(void)
{
bkeyok=0;
return(keydat);
}
void doanything(uchar key)
{
//----------按键处理只有有按键并正确消抖时才会进入,功能自己随便写
;
}
void main(void)
{
while(1)
{
if(scan_key())
doanything(read_key());
delay_ms(10);
}
}
|
|