芯片解密 反彙編 修改界面

51單片機的鍵盤掃描程序

特點:
按鍵在松手後有(yǒu)效,靈敏度高(gāo),消耗資源少(shǎo),運行(xíng)效率高(gāo)
 
獨立鍵盤為(wèi):K01=P2^4;K02=P2^5;K03=P2^6;K04=P2^7;
矩陣鍵盤為(wèi):行(xíng)(上(shàng)到下)_P2.3_P2.2_P2.1_P2.0
           (左到右)_P2.7_P2.6_P2.5_P2.4
 
提供的操作(zuò)函數(shù):
 
//獨立鍵盤.無按鍵動作(zuò)時(shí)其返回值num_key=0,否則返回按鍵号num_key
extern unsigned char keyboard_self();
 
//矩陣鍵盤.無按鍵動作(zuò)時(shí)其返回值num_key=0,否則返回按鍵号num_key****檢測高(gāo)四位
extern unsigned char keyboard_matrix();
 
****************************************/
 
先看獨立鍵盤(和(hé)矩陣鍵盤的算(suàn)法一樣)
-----------------------------------------------------------------------
#include
#include
 
//獨立鍵盤.無按鍵動作(zuò)時(shí)其返回值num_key=0,否則返回按鍵号num_key
extern unsigned char keyboard_self()
{
        unsigned char num_key=0;//按鍵号
        unsigned char temp=0;//用于讀取P2線上(shàng)按鍵值
        static unsigned char temp_code=0;//保存按鍵值
        static unsigned char num_check=0;//低(dī)電(diàn)平有(yǒu)效次數(shù)
        static unsigned char key_flag=0;//按鍵有(yǒu)效标識
 
        temp=P2&0xF0;//讀取P2線數(shù)據
 
        if(temp!=0xF0)//低(dī)電(diàn)平判斷
        {
                num_check++;
                if(num_check==10)//連續10(10ms)低(dī)電(diàn)平有(yǒu)效,則認為(wèi)按鍵有(yǒu)效
                {
                        key_flag=1;//使能按鍵有(yǒu)效标識
                        temp_code=temp;//保存按鍵值
                }
        }
        else//松手時(shí)判斷
        {
                num_check=0;
 
                if(key_flag==1)//按鍵有(yǒu)效
                {
                        key_flag=0;
 
                        switch(temp_code)//讀取按鍵号
                        {
                                case 0xE0: num_key=1;
                                           break;
                                case 0xD0: num_key=2;
                                           break;
                                case 0xB0: num_key=3;
                                           break;
                                case 0x70: num_key=4;
                                           break;
                        }
                }
        }
 
        return(num_key);
}
 
 
現在是矩陣鍵盤的
-----------------------------------------------------------------------
#include
#include  
 
//矩陣鍵盤.無按鍵動作(zuò)時(shí)其返回值num_key=0,否則返回按鍵号num_key****檢測高(gāo)四位
extern unsigned char keyboard_matrix()
{
        unsigned char num_key=0;//按鍵号
        unsigned char temp=0;//讀取P2口線數(shù)據
        static unsigned char temp_code=0;//用于保存按鍵值
        static unsigned char temp_circle=0xFE;//保存P2線上(shàng)的循環掃描值
        static unsigned char num_check=0;//低(dī)電(diàn)平計(jì)數(shù)
        static unsigned char key_flag=0;//按鍵有(yǒu)效标識
 
        P2=temp_circle;//0xFX
        temp=P2;//讀取P2口線數(shù)據
        if(temp!=temp_circle)//有(yǒu)按鍵動作(zuò)
        {
                num_check++;//低(dī)電(diàn)平計(jì)數(shù)|逢低(dī)電(diàn)平加1
                if(num_check==10)//連續10(10ms)低(dī)電(diàn)平有(yǒu)效
                {
                        key_flag=1;//按鍵有(yǒu)效标識置1
                        temp_code=temp;//保存按鍵值
                }
        }
        else//松手OR無按鍵動作(zuò),此時(shí)應該改變掃描線
        {
                num_check=0;
                if(key_flag==1)//按鍵有(yǒu)效判斷
                {
                        key_flag=0;
                        switch(temp_code)//讀取按鍵号
                        {
                                //P2^0
                                case 0xEE: num_key=1;
                                           break;
                                case 0xDE: num_key=2;
                                           break;
                                case 0xBE: num_key=3;
                                           break;
                                case 0x7E: num_key=4;
                                           break;
                                //P2^1
                                case 0xED: num_key=5;
                                           break;
                                case 0xDD: num_key=6;
                                           break;
                                case 0xBD: num_key=7;
                                           break;
                                case 0x7D: num_key=8;
                                           break;
                                //P2^2
                                case 0xEB: num_key=9;
                                           break;
                                case 0xDB: num_key=10;
                                           break;
                                case 0xBB: num_key=11;
                                           break;
                                case 0x7B: num_key=12;
                                           break;
                                //P2^3
                                case 0xE7: num_key=13;
                                           break;
                                case 0xD7: num_key=14;
                                           break;
                                case 0xB7: num_key=15;
                                           break;
                                case 0x77: num_key=16;
                                           break;
                        }
                }
                temp_circle=_crol_(temp_circle,1);//改變掃描線
                if(temp_circle==0xEF)
                {
                        temp_circle=0xFE;
                }
        }
        return(num_key);//返回按鍵号
}
 
/*************************************************************************
 
未按鍵時(shí),掃描線一直變化。
長按鍵時(shí),掃描線不變化,使得(de)該行(xíng)按鍵變成了獨立按鍵,這樣的掃描效率極高(gāo)。
如當按下P2.0線上(shàng)的某個(gè)鍵時(shí),程序将掃描到這個(gè)鍵,而後掃描線不變化,
當鍵盤程序連續10次進入時(shí)檢測到10次按鍵有(yǒu)效,直到松手後掃描線才變化

 




首頁

電(diàn)話(huà)

地址

到底了~