在單片機(jī)系統(tǒng)中應(yīng)用按鍵的時(shí)候,如果只需要按下一次按鍵加1或減1,那用第8章學(xué)到的知識就可以完成了,但如果想連續(xù)加很多數(shù)字的時(shí)候,要一次次按下這個(gè)按鍵確實(shí)有點(diǎn)不方便,這時(shí)我們會希望一直按住按鍵,數(shù)字就自動持續(xù)增加或減小,這就是所謂的長短按鍵應(yīng)用。
當(dāng)檢測到一個(gè)按鍵產(chǎn)生按下動作后,馬上執(zhí)行一次相應(yīng)的操作,同時(shí)在程序里記錄按鍵按下的持續(xù)時(shí)間,該時(shí)間超過1秒后(主要是為了區(qū)別短按和長按這兩個(gè)動作,因短按的時(shí)間通常都達(dá)到幾百 ms),每隔 200 ms(如果你需要更快那就用更短的時(shí)間,反之亦然)就自動再執(zhí)行一次該按鍵對應(yīng)的操作,這就是一個(gè)典型的長按鍵效果。
對此,我們做了一個(gè)模擬定時(shí)炸彈效果的實(shí)例,提供給大家作為參考。打開開關(guān)后,數(shù)碼管顯示數(shù)字0,按向上的按鍵數(shù)字加1,按向下的按鍵數(shù)字減1,長按向上按鍵1秒后,數(shù)字會持續(xù)增加,長按向下按鍵1秒后,數(shù)字會持續(xù)減小。設(shè)定好數(shù)字后,按下回車按鍵,時(shí)間就會進(jìn)行倒計(jì)時(shí),當(dāng)?shù)褂?jì)時(shí)到0的時(shí)候,用蜂鳴器和板子上的8個(gè) LED 小燈做炸彈效果,蜂鳴器持續(xù)響,LED 小燈全亮。
#include <reg52.h>
sbit BUZZ = P1^6;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit KEY_IN_1 = P2^4;
sbit KEY_IN_2 = P2^5;
sbit KEY_IN_3 = P2^6;
sbit KEY_IN_4 = P2^7;
sbit KEY_OUT_1 = P2^3;
sbit KEY_OUT_2 = P2^2;
sbit KEY_OUT_3 = P2^1;
sbit KEY_OUT_4 = P2^0;
unsigned char code LedChar[] = { //數(shù)碼管顯示字符轉(zhuǎn)換表
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[7] = { //數(shù)碼管+獨(dú)立 LED 顯示緩沖區(qū)
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
unsigned char code KeyCodeMap[4][4] = { //矩陣按鍵編號到標(biāo)準(zhǔn)鍵盤鍵碼的映射表
{ 0x31, 0x32, 0x33, 0x26 }, //數(shù)字鍵1、數(shù)字鍵2、數(shù)字鍵3、向上鍵
{ 0x34, 0x35, 0x36, 0x25 }, //數(shù)字鍵4、數(shù)字鍵5、數(shù)字鍵6、向左鍵
{ 0x37, 0x38, 0x39, 0x28 }, //數(shù)字鍵7、數(shù)字鍵8、數(shù)字鍵9、向下鍵
{ 0x30, 0x1B, 0x0D, 0x27 } //數(shù)字鍵0、ESC 鍵、 回車鍵、 向右鍵
}
unsigned char KeySta[4][4] = { //全部矩陣按鍵的當(dāng)前狀態(tài)
{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
};
unsigned long pdata KeyDownTime[4][4] = { //每個(gè)按鍵按下的持續(xù)時(shí)間,單位 ms
{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}
};
bit enBuzz = 0; //蜂鳴器使能標(biāo)志
bit flag1s = 0; //1秒定時(shí)標(biāo)志
bit flagStart = 0; //倒計(jì)時(shí)啟動標(biāo)志
unsigned char T0RH = 0; //T0 重載值的高字節(jié)
unsigned char T0RL = 0; //T0 重載值的低字節(jié)
unsigned int CountDown = 0; //倒計(jì)時(shí)計(jì)數(shù)器
void ConfigTimer0(unsigned int ms);
void ShowNumber(unsigned long num);
void KeyDriver();
void main(){
EA = 1; //使能總中斷
ENLED = 0; //選擇數(shù)碼管和獨(dú)立 LED
ADDR3 = 1;
ConfigTimer0(1); //配置 T0 定時(shí) 1ms
ShowNumber(0); //上電顯示0
while (1){
KeyDriver(); //調(diào)用按鍵驅(qū)動函數(shù)
if (flagStart && flag1s){ //倒計(jì)時(shí)啟動且1秒定時(shí)到達(dá)時(shí),處理倒計(jì)時(shí)
flag1s = 0;
if (CountDown > 0){ //倒計(jì)時(shí)未到0時(shí),計(jì)數(shù)器遞減
CountDown--;
ShowNumber(CountDown); //刷新倒計(jì)時(shí)數(shù)顯示
if (CountDown == 0){ //減到0時(shí),執(zhí)行聲光報(bào)警
enBuzz = 1;
//啟動蜂鳴器發(fā)聲
LedBuff[6] = 0x00; //點(diǎn)亮獨(dú)立 LED
}
}
}
}
}
/* 配置并啟動 T0,ms-T0 定時(shí)時(shí)間 */
void ConfigTimer0(unsigned int ms){
unsigned long tmp; //臨時(shí)變量
tmp = 11059200 / 12; //定時(shí)器計(jì)數(shù)頻率
tmp = (tmp * ms) / 1000; //計(jì)算所需的計(jì)數(shù)值
tmp = 65536 - tmp; //計(jì)算定時(shí)器重載值
tmp = tmp + 28; //補(bǔ)償中斷響應(yīng)延時(shí)造成的誤差
T0RH = (unsigned char)(tmp>>8); //定時(shí)器重載值拆分為高低字節(jié)
T0RL = (unsigned char)tmp;
TMOD &= 0xF0; //清零 T0 的控制位
TMOD |= 0x01; //配置 T0 為模式1
TH0 = T0RH; //加載 T0 重載值
TL0 = T0RL;
ET0 = 1; //使能 T0 中斷
TR0 = 1; //啟動 T0
}
/* 將一個(gè)無符號長整型的數(shù)字顯示到數(shù)碼管上,num-待顯示數(shù)字 */
void ShowNumber(unsigned long num){
signed char i;
unsigned char buf[6];
for (i=0; i<6; i++){ //把長整型數(shù)轉(zhuǎn)換為6位十進(jìn)制的數(shù)組
buf[i] = num % 10;
num = num / 10;
}
for (i=5; i>=1; i--){ //從最高位起,遇到0轉(zhuǎn)換為空格,遇到非0則退出循環(huán)
if (buf[i] == 0){
LedBuff[i] = 0xFF;
}else{
break;
}
}
for ( ; i>=0; i--){ //剩余低位都如實(shí)轉(zhuǎn)換為數(shù)碼管顯示字符
LedBuff[i] = LedChar[buf[i]];
}
}
/* 按鍵動作函數(shù),根據(jù)鍵碼執(zhí)行相應(yīng)的操作,keycode-按鍵鍵碼 */
void KeyAction(unsigned char keycode){ //按鍵動作函數(shù),根據(jù)鍵碼執(zhí)行相應(yīng)動作
if (keycode == 0x26){ //向上鍵,倒計(jì)時(shí)設(shè)定值遞增
if (CountDown < 9999){ //最大計(jì)時(shí)9999秒
CountDown++;
ShowNumber(CountDown);
}
}else if (keycode == 0x28){ //向下鍵,倒計(jì)時(shí)設(shè)定值遞減
if (CountDown > 1){ //最小計(jì)時(shí)1秒
CountDown--;
ShowNumber(CountDown);
}
}else if (keycode == 0x0D){ //回車鍵,啟動倒計(jì)時(shí)
flagStart = 1; //啟動倒計(jì)時(shí)
}else if (keycode == 0x1B){ //Esc 鍵,取消倒計(jì)時(shí)
enBuzz = 0; //關(guān)閉蜂鳴器
LedBuff[6] = 0xFF; //關(guān)閉獨(dú)立 LED
flagStart = 0; //停止倒計(jì)時(shí)
CountDown = 0; //倒計(jì)時(shí)數(shù)歸零
ShowNumber(CountDown);
}
}
/* 按鍵驅(qū)動函數(shù),檢測按鍵動作,調(diào)度相應(yīng)動作函數(shù),需在主循環(huán)中調(diào)用 */
void KeyDriver(){
unsigned char i, j;
static unsigned char pdata backup[4][4] = { //按鍵值備份,保存前一次的值
{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
};
static unsigned long pdata TimeThr[4][4] = { //快速輸入執(zhí)行的時(shí)間閾值
{1000, 1000, 1000, 1000}, {1000, 1000, 1000, 1000},
{1000, 1000, 1000, 1000}, {1000, 1000, 1000, 1000}
};
for (i=0; i<4; i++){ //循環(huán)掃描 4*4 的矩陣按鍵
for (j=0; j<4; j++){
if (backup[i][j] != KeySta[i][j]){ //檢測按鍵動作
if (backup[i][j] != 0){ //按鍵按下時(shí)執(zhí)行動作
KeyAction(KeyCodeMap[i][j]); //調(diào)用按鍵動作函數(shù)
}
}
backup[i][j] = KeySta[i][j];
if (KeyDownTime[i][j] > 0){
if (KeyDownTime[i][j] >= TimeThr[i][j]){ //達(dá)到閾值時(shí)執(zhí)行一次動作
KeyAction(KeyCodeMap[i][j]); //調(diào)用按鍵動作函數(shù)
TimeThr[i][j] += 200; //時(shí)間閾值增加 200 ms,以準(zhǔn)備下次執(zhí)行
}
}else{ //按鍵彈起時(shí)復(fù)位閾值時(shí)間
TimeThr[i][j] = 1000; //恢復(fù) 1 s 的初始閾值時(shí)間
}
}
}
}
/* 按鍵掃描函數(shù),需在定時(shí)中斷中調(diào)用 */
void KeyScan(){
unsigned char i;
static unsigned char keyout = 0; //矩陣按鍵掃描輸出索引
static unsigned char keybuf[4][4] = { //矩陣按鍵掃描緩沖區(qū)
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}
};
//將一行的4個(gè)按鍵值移入緩沖區(qū)
keybuf[keyout][0] = (keybuf[keyout][0] << 1) | KEY_IN_1;
keybuf[keyout][1] = (keybuf[keyout][1] << 1) | KEY_IN_2;
keybuf[keyout][2] = (keybuf[keyout][2] << 1) | KEY_IN_3;
keybuf[keyout][3] = (keybuf[keyout][3] << 1) | KEY_IN_4;
//消抖后更新按鍵狀態(tài)
for (i=0; i<4; i++){ //每行4個(gè)按鍵,所以循環(huán)4次
//連續(xù) 4 次掃描值為 0,即 4*4 ms 內(nèi)都是按下狀態(tài)時(shí),可認(rèn)為按鍵已穩(wěn)定的按下
if ((keybuf[keyout][i] & 0x0F) == 0x00){
KeySta[keyout][i] = 0;
KeyDownTime[keyout][i] += 4; //按下的持續(xù)時(shí)間累加
//連續(xù)4次掃描值為1,即 4*4 ms 內(nèi)都是彈起狀態(tài)時(shí),可認(rèn)為按鍵已穩(wěn)定的彈起
}else if ((keybuf[keyout][i] & 0x0F) == 0x0F){
KeySta[keyout][i] = 1;
}
}
KeyDownTime[keyout][i] = 0; //按下的持續(xù)時(shí)間清零
//執(zhí)行下一次的掃描輸出
keyout++; //輸出索引遞增
keyout &= 0x03; //索引值加到4即歸零
switch (keyout){ //根據(jù)索引,釋放當(dāng)前輸出引腳,拉低下次的輸出引腳
case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break;
case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break;
case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break;
case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break;
default: break;
}
}
/* LED 動態(tài)掃描刷新函數(shù),需在定時(shí)中斷中調(diào)用 */
void LedScan(){
static unsigned char i = 0; //動態(tài)掃描索引
P0 = 0xFF; //關(guān)閉所有段選位,顯示消隱
P1 = (P1 & 0xF8) | i; //位選索引值賦值到 P1 口低3位
P0 = LedBuff[i]; //緩沖區(qū)中索引位置的數(shù)據(jù)送到 P0 口
if (i < 6){ //索引遞增循環(huán),遍歷整個(gè)緩沖區(qū)
i++;
}else{
i = 0;
}
}
/* T0 中斷服務(wù)函數(shù),完成數(shù)碼管、按鍵掃描與秒定時(shí) */
void InterruptTimer0() interrupt 1{
static unsigned int tmr1s = 0; //1秒定時(shí)器
TH0 = T0RH; //重新加載重載值
TL0 = T0RL;
if (enBuzz){ //蜂鳴器發(fā)聲處理
BUZZ = ~BUZZ; //驅(qū)動蜂鳴器發(fā)聲
}else{
BUZZ = 1; //關(guān)閉蜂鳴器
}
LedScan(); //LED 掃描顯示
KeyScan(); //按鍵掃描
if (flagStart){ //倒計(jì)時(shí)啟動時(shí)處理1秒定時(shí)
tmr1s++;
if (tmr1s >= 1000){
tmr1s = 0;
flag1s = 1;
}
}else{ //倒計(jì)時(shí)未啟動時(shí)1秒定時(shí)器始終歸零
tmr1s = 0;
}
}
長按鍵功能實(shí)現(xiàn)的重點(diǎn)有兩個(gè):第一,是在原來的矩陣按鍵掃描函數(shù) KeyScan 內(nèi),當(dāng)檢測到按鍵按下后,持續(xù)的對一個(gè)時(shí)間變量進(jìn)行累加,其目的是用這個(gè)時(shí)間變量來記錄按鍵按下的時(shí)間;第二,是在按鍵驅(qū)動函數(shù) KeyDriver 里,除了原來的檢測到按鍵按下這個(gè)動作時(shí)執(zhí)行按鍵動作函數(shù) KeyAction 外,還監(jiān)測表示按鍵按下時(shí)間的變量,根據(jù)它的值來完成長按時(shí)的連續(xù)快速按鍵動作功能。