8条口线24按键的C51源程序
2005-12-1 15:36:50
//键盘扫描处理,无按键返回0,有按键返回键值,键值对应于keycode[]下标值。8条(以P2为例)口线24按键
//键盘码也可定义为局部数组变量
unsigned char code keycode[]=
{0x00,0xee,0xde,0xbe,0xed,0xdd,0xbd,0xeb,0xdb,0xbb,0xd7,0xe7,0xb7,0x7b,0x7d,0x7e,0x77,
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0x7f,0xbf};
unsigned char keyread(void)
{ unsigned char i,j,x,k1,k2;
P2=0xff;
if(P2!=0xff)
{k1=P2;
delay();
P2=0xff;
if(P2!=0xff)
{for(P2=0xff;P2!=0xff;P2=0xff)
x=k1;}
}
else{x=0;
}
//以上判断接地按键
//以下判断交叉按键(反转法)
if(x==0)
{P2=0x0f;
if(P2!=0x0f)
{delay();
P2=0x0f;
if(P2!=0x0f)
{k1=P2;
P2=0xf0;
if(P2!=0xf0)
{k2=P2;
for(P2=0xf0;P2!=0xf0;P2=0xf0)
x=k1^k2;}
}
}
else{x=0;} }
if(x==0){i=0;}
else{i=j=0xff;
do{i ;j ;
if(keycode[i]==x){j=24;}
else if(j==24){i=0;}else{;} }
while(j!=24);}
return(i);
}
void delay()//延时
{ unsigned char b;
for (b=0;b<=0xff;b )
{
for (b=0;b<=0xff;b )
{;}
}
}