键盘子程序(AVR)
扫描二维码
随时随地手机看文章
键盘扫描有中断方式和查询方式
#i nclude "jn8515def.h"
/******以下是键盘排列图******
| | |
PC0 ___1|__2|__3|__
| | |
PC1 ___4|__5|__6|__
| | |
PC2 ___7|__8|__9|__
| | |
PC3 ___C|__0|__S|__
| | |
| | |
PC4 PC5 PC6
*****************************/
//下沿为有效键值 0 1 2 3 4 5 6 7 8 9 s(10) c0 c1 c4 c7 cs c
flash uchar cKeyTab[]={0x57,0x6e,0x5e,0x3e,0x6d,0x5d,0x3d,0x6b,0x5b,0x3b,0x37,0x47,0x66,0x65,0x63,0x27,0x67};
flash uchar cKeyOut[]={0xef,0xdf,0xbf}; //其中一列为0
flash uchar cKeyDir[]={0x10,0x20,0x40};
uchar cMenu=0;
uchar cKeyVal()
{ static uchar cLastKv,cSameKvNum=0;
static bit bKeyPress=0;
uchar i,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; //延时去抖
goto Next;
}
}
}
// do{ //等待按键释放
// }while(!bKeyPress);
cSameKvNum=0;
bKeyPress=0;
return 0xff;
Next:
// delay_ms(2);
if(bKeyPress) return 0xff;
if(cKeytmp==cLastKv)
{ if(++cSameKvNum>10)
{ cSameKvNum=0;
bKeyPress=1;
return j;
}
}
else
{ cSameKvNum=0;
cLastKv=cKeytmp;
}
return 0xff;//返回空键值
}