專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

一種非傳統(tǒng)的矩陣鍵盤掃描函數(shù)

作者:佚名   來(lái)源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2013年10月13日   【字體:


//子函數(shù)delay_1ms(1)大家自己寫吧
#define scan_key_port P3

uchar key_scan()
{
    uchar sccode,recode;
    uchar key_value;
    scan_key_port=0xf0; /* 發(fā)全 "0" 的行掃描碼,列線輸入 */
    if((scan_key_port&0xf0)!=0xf0) /* 若有鍵按下 */
    {
        delay_1ms(1); /*延時(shí)去抖動(dòng)*/
        if((scan_key_port&0xf0)!=0xf0) /* 若有鍵按下 */
        {
            sccode=0xfe; /*逐行掃描初值*/
            while((sccode&0x10)!=0)
            {
                scan_key_port=sccode; /* 輸出行掃描碼 */
                if((scan_key_port&0xf0)!=0xf0) /*  本行有鍵按下 */
                {
                    recode=(scan_key_port&0xf0)|0x0f;
                    key_value=(~sccode)+(~recode);/* 返回特征碼 */
                 }
                else sccode=(sccode<<1)|0x01; /* 行掃描左移一位 */
            }
        }
     }
    else {         //若無(wú)鍵按下
     scan_key_port = 0xff;
  return (0);
}
    scan_key_port = 0xff; /*釋放按鍵掃描端口*/
    switch(key_value){ /*顯示按鍵對(duì)應(yīng)的絲印*/
    case 0x11:
     return('0');
   
    case 0x12:
     return('1');
   
    case 0x14:
     return('2');
   
    case 0x21:
     return('3');
   
    case 0x22:
     return('4');
   
    case 0x24:
     return('5');
   
    case 0x41:
     return('6');
   
    case 0x42:
     return('7');
   
    case 0x44:
     return('8');
   
    case 0x81:
     return('9');
   
    case 0x82:
     return('A');
   
    case 0x84:
     return('B');
   
    case 0x18:
     return('C');
   
    case 0x28:
     return('D');
   
    case 0x48:
     return('E');
   
    case 0x88:
     return('F');
    }
}

關(guān)閉窗口

相關(guān)文章