AVR单片机键盘子程序(中断方式和查询方式 )
扫描二维码
随时随地手机看文章
AVR单片机键盘子程序
键盘扫描有中断方式和查询方式
#include"jn8515def.h"
/******以下是键盘排列图******
|||
PC0___1|__2|__3|__
|||
PC1___4|__5|__6|__
|||
PC2___7|__8|__9|__
|||
PC3___C|__0|__S|__
|||
|||
PC4PC5PC6
*****************************/
//下沿为有效键值0123456789s(10)c0c1c4c7csc
flashucharcKeyTab[]={0x57,0x6e,0x5e,0x3e,0x6d,0x5d,0x3d,0x6b,0x5b,0x3b,0x37,0x47,0x66,0x65,0x63,0x27,0x67};
flashucharcKeyOut[]={0xef,0xdf,0xbf};//其中一列为0
flashucharcKeyDir[]={0x10,0x20,0x40};
ucharcMenu=0;
ucharcKeyVal()
{statICucharcLastKv,cSameKvNum=0;
staticbitbKeyPress=0;
uchari,j,cKeytmp=0xff;
for(i=0;i<3;i++)
{DDRC&=0x80;
DDRC|=cKeyDir[i];
PORTC=cKeyOut[i];
#asm("nop")
#asm("nop")
cKeytmp=PINC&0x7f;
for(j=0;j<16;j++)
{if(cKeyTab[j]==cKeytmp)
{cWaitTime=10;//延时去抖
gotoNext;
}
}
}
//do{//等待按键释放
//}while(!bKeyPress);
cSameKvNum=0;
bKeyPress=0;
return0xff;
Next:
//delay_ms(2);
if(bKeyPress)return0xff;
if(cKeytmp==cLastKv)
{if(++cSameKvNum>10)
{cSameKvNum=0;
bKeyPress=1;
returnj;
}
}
else
{cSameKvNum=0;
cLastKv=cKeytmp;
}
return0xff;//返回空键值
}