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;//返回空鍵值
}