矩陣鍵盤掃描程序
按鍵掃描(線反轉(zhuǎn))
//-------------------------------- ------------------------------------------------------------------
// 函數(shù)名稱: program_SCANkey
// 函數(shù)功能: 程序掃描鍵盤,
//????????????????? 有鍵按下完成按鍵處理,無鍵按下直接返回
//--------------------------------------------------------------------------------------------------
void program_SCANkey()
{
unsigned char key_code;
????????? if(judge_hitkey())?????????????????? //判斷是否有鍵按下
{
????????????? delay(1000);???????????????????????? //延時(shí)20ms左右,消除抖動(dòng)干擾
????????????? if(judge_hitkey())?????????????????? //判斷是否有效按鍵
?? {
????????????????? key_code=scan_key();?????????? //獲取鍵值
?????????????while(judge_hitkey());????????? //等待按鍵釋放
??????? {
???????? }
????????????????? key_manage(key_code);?????????? //鍵盤掃描、鍵盤散轉(zhuǎn)、按鍵處理
?????? }
}
}
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱: judge_hitkey
// 函數(shù)功能: //判斷是否有鍵按下,有返回1,沒有返回0
// 列判斷,還可以用行判斷。
//--------------------------------------------------------------------------------------------------
bit judge_hitkey()??????????????????????????? //判斷是否有鍵按下,有返回1,沒有返回0
{
unsigned char scancode,keycode;
scancode=0x0F;?????????????? //開始設(shè)定P1.0~P1.3輸出全1(初值)即表明無鍵閉合
KEY=scancode;??????????
keycode=KEY;???????????????? //讀取P1.0~P1.3的真實(shí)狀態(tài),從而確定有沒有鍵被按下
if(keycode==0x0F)
??????return(0);????????????????????????????? //全1則無鍵閉合
else
?????????? return(1);????????????????????????????? //否則有鍵閉合
}
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱: scan_key
// 函數(shù)功能: //掃描鍵盤,返回鍵值(高四位代表行,低四位代表列)
// 說明:scancode 掃描碼,keycode 鍵值,keycode_line 行,keycode_row 列
// 過程:先掃描行,確定那行的按鍵被按下。再掃描列,確定那列的按鍵被按下,從而確定那個(gè)按鍵被按下。
//--------------------------------------------------------------------------------------------------
unsigned char scan_key()??????????????????? //掃描鍵盤,返回鍵值(高四位代表行,低四位代表列)
{
???????? unsigned char scancode,keycode,keycode_line,keycode_row;
????scancode=0xF0;?????????????? //列置低,行置高
????KEY = scancode;????????????? //輸入掃描碼,掃描行
????keycode_line=KEY;??????????? //KEY的值是與鍵盤相連的P的狀態(tài)值。若沒有按鍵按下KEY的值為0xF0,若有按鍵按下則KEY的值就不是0xF0
???
????scancode=0x0F;??????????????????????????? //列置高,行置低
????KEY=scancode;???????????????????????????? //輸入掃描碼,掃描列
????keycode_row=KEY;???????? //KEY的值是與鍵盤相連的P的狀態(tài)值。若沒有按鍵按下KEY的值為0x0F,若有按鍵按下則KEY的值就不是0x0F
???????? keycode = ((keycode_line&0xF0)|(keycode_row&0x0F));
???????? return(keycode);
}
2、按鍵掃描(逐行掃描)
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱: kbscan???????? 鍵盤掃描子程序
// 函數(shù)功能: 判斷是否有鍵按下,有返回鍵值,沒有返回0
// p1的高四位為列,低四位為行????????? P1.7???????? P1.6???????? P1.5???????? P1.4???????? P1.3???????? P1.2???????? P1.1???????? P1.0
//???????????????????????????????????? 列4????????? 列3????????? 列2????????? 列1????????? 行4????????? 行3????????? 行2????????? 行1
// 過程:先根據(jù)列判斷是否有鍵按下,沒有返回0,有,則逐行掃描以確定按鍵所在的行,再確定按鍵所在列
//???????????? 從而最終確定該按鍵。
//--------------------------------------------------------------------------------------------------
uchar kbscan(void)
{
uchar sccode,recode;
P1=0xf0;?????????????????????? //置所有行為低電平,行掃描,列線輸入(此時(shí))
if((P1&0xf0)!=0xf0)??????????? //判斷是否有有鍵按下(讀取列的真實(shí)狀態(tài),若第4列有鍵按下則P1的值會(huì)變成0111 0000),有往下執(zhí)行
{
??????? delays();?????????????????? //延時(shí)去抖動(dòng)(10ms)
???if((P1&0xf0)!=0xf0)???????? //再次判斷列中是否是干擾信號(hào),不是則向下執(zhí)行
??????? {
???????? sccode=0xFE;???????????????? //逐行掃描初值(即先掃描第1行)
????while((sccode&0x10)!=0)??????? //行掃描完成時(shí)(即4行已經(jīng)全部掃描完成)sccode為1110 1111 停止while程序?????
????{
???? P1=sccode;???????????????????? //輸出行掃描碼
????????? if ((P1&0xf0)!=0xf0)?????????? //本行有鍵按下(即P1(真實(shí)的狀態(tài))的高四位不全為1)
?????{
?????????? recode=(P1&0xf0)|0x0f; //列
??????return(sccode&recode); //返回行和列
???? }
????????? else???????????? //所掃描的行沒有鍵按下,則掃描下一行,直到4行都掃描,此時(shí)sccode值為1110 1111 退出while程序
???????? {
?????????? sccode=(sccode<<1)|0x01;//行掃描碼左移一位
????????? }
???????? }
??????? }
}
????????? else
{
?????????? return 0;??????? //無鍵按下,返回0
}
}
--------------------------------------------------------------------------------------------------------------------------
/*Main.c*/
#include "global.c"
void SystemInit();
void Timer1Init();
void KickDog();
void delay();
unsigned int judge_key();
unsigned int scan_key();
unsigned char numkey=0;
unsigned char DATX,DATY;???
main()
{??
??? SystemInit();?????????? //系統(tǒng)初始化
???
??? MCRA=MCRA & 0x80FF;?????? //IOPB0-6設(shè)為IO口模式
??? PBDATDIR=0xBFC2;???????? //所有LED=0,并置IOPB6為輸入口
???
??? Timer1Init();??????????? //定時(shí)器初始化
??? asm(" CLRC INTM ");
??? while(1)
??? {
???? // KeyLed();
if(judge_key()==1)
numkey++;
??? }
???
}
void SystemInit()
{??
asm(" SETC INTM "); /* 關(guān)閉總中斷 */
asm(" CLRC?? SXM?? "); /* 禁止符號(hào)位擴(kuò)展 */
asm(" CLRC?? CNF?? ");???? /* B0塊映射為 on-chip DARAM*/
asm(" CLRC?? OVM?? ");???? /* 累加器結(jié)果正常溢出*/
SCSR1=0x83FE;?????????? /* 系統(tǒng)時(shí)鐘CLKOUT=20*2=40M */
WDCR=0x006F;??? /* 禁止看門狗,看門狗時(shí)鐘64分頻 */
KickDog();??????? /* 初始化看門狗 */??
???? IFR=0xFFFF;???? /* 清除中斷標(biāo)志 */
???? IMR=0x0002;???? /* 打開中斷2*/??????
????
}??
void Timer1Init()???????????
{
???? EVAIMRA=0x0080;??? //?? 定時(shí)器1周期中斷使能
???? EVAIFRA=0xFFFF;??? //?? 清除中斷標(biāo)志
???? GPTCONA=0x0000;
T1PR=2500;???? //?? 定時(shí)器1初值,定時(shí)0.4us*2500=1ms????
T1CNT=0;
T1CON=0x144E;??????????? //增模式, TPS系數(shù)40M/16=2.5M,T1使能
????
}??
unsigned int judge_key()
{
MCRC=MCRC&0x81FF;??? //
PFDATDIR=PFDATDIR|0x0070;
PFDATDIR=PFDATDIR&0x8FFF;?? //設(shè)置456輸入高
PFDATDIR=PFDATDIR&0xFFF1;
PFDATDIR=PFDATDIR|0x0E00;???? //設(shè)置123輸出低
?
if((PFDATDIR&0x0070)==0x0070)
????? return(0);
else
????? return(1);
}
unsigned int scan_key()
{
if(judge_key()==1)
?? delay();
if(judge_key()==1)
?? {
????? MCRC=MCRC&0x81FF;??? //
??? PFDATDIR=PFDATDIR|0x0070;
??? PFDATDIR=PFDATDIR&0x8FFF;?? //設(shè)置456輸入高
???
??? PFDATDIR=PFDATDIR&0xFFF1;
??? PFDATDIR=PFDATDIR|0x0E00;???? //設(shè)置123輸出低??
?????? delay();
???????
?????? numkey=((PFDATDIR&0x0070)|(PFDATDIR&0x000E));
?? // delay();
??
??
??? //MCRC=MCRC&0x81FF;??? //
????????? PFDATDIR=PFDATDIR&0xFF8F;??? //設(shè)置456輸出低
??? PFDATDIR=PFDATDIR|0xE000;
????????? PFDATDIR=PFDATDIR|0x000E;??? //設(shè)置123輸入高
??? PFDATDIR=PFDATDIR&0xF1FF;
??? delay();
???
?? // numkey=((PFDATDIR&0x0070)|(PFDATDIR&0x000E));
??? numkey=numkey|(PFDATDIR&0x000E);
??? return(numkey);
??
??
?? }
}
?
?
void c_int2()??????????????? /*定時(shí)器1中斷服務(wù)程序*/
{
???? if(PIVR!=0x27)
?? {??? asm(" CLRC INTM ");
??? return;
?? }
scan_key()?? ;??
????
EVAIFRA=EVAIFRA&0x80;
asm(" CLRC INTM ");???
}??
void delay()
{
int i;
for(i=0;i<10000;i++);
}
void KickDog()???? /*踢除看門狗 */
{
WDKEY=0x5555;
WDKEY=0xAAAA;
}
結(jié)束
更多矩陣鍵盤知識(shí)請(qǐng)?jiān)L問http://ttokpm.com/zhuanti/20111025226587.html
評(píng)論
查看更多