4×4矩阵式键盘(非中断)源程序
扫描二维码
随时随地手机看文章
#include
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};
uchar temp;
uchar key;
uchar k;
uchar code_h;
//延时函数
void mDelay(uchar Delay)
{ uchar i;
for(;Delay>0;Delay--)
{ for(i=0;i<124;i++)
{;}
}
}
void keyc(void);
//主函数
void main(void)
{
P2_0=0;
while(1)
{
code_h=0xef;
P3=code_h;
temp=P3;
while(temp==code_h)//检测键盘是否有按键按下
{
code_h=(code_h<<1)|0x01;
if(code_h==0xff)
{code_h=0xef;}
P3=code_h;
temp=P3;
}
mDelay(5); //延时去抖
if(P3==temp)
{
keyc();
while(temp==P3) //等待按键放开
{;}
}
}
}
//键盘操作
void keyc(void)
{
switch(temp)
{
case 0xee:
key=10;
break;
case 0xed:
key=0;
break;
case 0xeb:
key=11;
break;
case 0xe7:
key=12;
break;
case 0xde:
key=1;
break;
case 0xdd:
key=2;
break;
case 0xdb:
key=3;
break;
case 0xd7:
key=13;
break;
case 0xbe:
key=4;
break;
case 0xbd:
key=5;
break;
case 0xbb:
key=6;
break;
case 0xb7:
key=14;
break;
case 0x7e:
key=7;
break;
case 0x7d:
key=8;
break;
case 0x7b:
key=9;
break;
case 0x77:
key=15;
break;
}
P0=table[key];
}