獨(dú)立按鍵驅(qū)動(dòng)程序
下面是獨(dú)立按鍵驅(qū)動(dòng)程序,特點(diǎn)是利用TIMER0中斷,CPU在不停的掃描程序,60ms到,執(zhí)行按鍵驅(qū)動(dòng),函數(shù)在48ms執(zhí)行一次,60ms再次執(zhí)行,避免了CPU資源的浪費(fèi)
bit ltkey = 0; //防止按鍵被長(zhǎng)按bit am_led = 0;uchar unitTemp = 0xff;bit amkey;bit remsg = 0;bit send = 0;bit delay100 = 0;bit mtoa = 0;void KeyScn(void) //按鍵掃描模塊{uchar NowKeyValue;bit NowAMKeyValue;//檢測(cè)按鍵if(pic == 10) //首次按鍵檢測(cè){amkey = AMKEY;unitTemp = P2;}if(pic == 13) //12ms后,再次執(zhí)行檢測(cè){NowKeyValue = P2;NowAMKeyValue = AMKEY;if(NowKeyValue != unitTemp) //如果兩次檢測(cè)到的結(jié)果不同,則認(rèn)為是抖動(dòng),不做處理{unitTemp = 0xff;}if((NowAMKeyValue == amkey) (amkey == 0)) //如果狀態(tài)鍵按鍵有效{if(ltkey == 0){ //AM狀態(tài)LED位,0為自動(dòng),1為手動(dòng)am_led = !am_led;if(am_led == 0) //如果是從手動(dòng)切回自動(dòng),切回到CH1{unitTemp = 0xf7;mtoa = 1; //由手動(dòng)切到自動(dòng)后的標(biāo)志位}AMLED = am_led; //狀態(tài)燈,變化時(shí)寫入ltkey = 1;}}else{ltkey = 0;}}}
評(píng)論