#include "stdio.h" #include "reg51.h"
// 0 - 9 的編碼 unsigned char code table[] = { 0x11,0xb7,0x29,0x23,0x87,0x43,0x41,0x37,0x01,0x03,0x00 } ; unsigned int count2 ;
//第1-6 位的片選線 unsigned char code dig[] = { 0x7f,0xfd,0xdf,0xf7,0xef,0xfb } ; unsigned char buffer[8],val[6] ; unsigned char count1,scan,i,ch,KeyVal,modify,OverTime ; bit fKey,fSec,Key1,Key2,Key3,OldKey1,OldKey2,OldKey3,blink,fTimer,fSTOP ; unsigned long int cn,tmp ;
void init(void) ; void display(void) ; void Cal_cn(void) ; void Cal_val(void) ;
main() { init() ;
while(1) {
if(fSec) { // 秒標(biāo)志被置位? fSec = 0 ; // 清除秒標(biāo)志 if(!fSTOP && modify==0) { if(cn!=0) cn-- ; else { fTimer = 1 ; fSTOP = 1 ; } } display() ; }
if(fKey) { // 有按鍵按下? fKey = 0 ; // 清除按件標(biāo)志,并處理該按鍵
if(KeyVal==1) { // 是調(diào)整按鈕? fSTOP = 0 ; modify++ ; // 進(jìn)入相應(yīng)狀態(tài),并重置超時(shí)定時(shí)器 if(modify==7) { modify = 0 ; OverTime = 0 ; } else OverTime = 10 ; }
if(KeyVal==2 && modify!=0) { // +1按鈕,相應(yīng)位+1 Cal_val() ; if(val[modify-1]<9) val[modify-1]++ ; Cal_cn() ; display() ; OverTime = 10 ; // 刷新顯示,并重置超時(shí)定時(shí)器 }
if(KeyVal==3 && modify!=0) { // -1按鈕,相應(yīng)位-1 Cal_val() ; if(val[modify-1]>0) val[modify-1]-- ; Cal_cn() ; display() ; OverTime = 10 ; // 刷新顯示,并重置超時(shí)定時(shí)器 }
}
} }
void init(void) {
count1 = count2 = 0 ; scan = 0 ; KeyVal = 0 ; cn = 0 ; for(i=0 ;i<6 ;i++) val = 0 ; fKey = 0 ; modify = 0 ; blink = 0 ; OverTime = 0 ; fTimer = 0 ; fSTOP = 0 ; Key1 = Key2 = Key3 = 0 ; OldKey1 = OldKey2 = OldKey3 = 1 ;
for(i=0 ;i<6 ;i++) buffer = 0 ;
TCON &= 0xCF ; // 初始化Timer0 TMOD &= 0xF0 ; TMOD |= 0x01 ; TH0 = TL0 = 0 ; EA = 1 ; ET0 = 1 ; TR0 = 1 ;
}
timerint () interrupt 1 {
TR0 = 0 ; TL0 = 0x12 ; TH0 = 0xf7 ; // 0xf712 for 2.5ms TR0 = 1 ;
if(scan==1 && !fKey) { // 掃描調(diào)整按鈕 Key1 = P30 ; if(!Key1 && OldKey1) { fKey = 1 ; KeyVal = 1 ; } }
if(scan==3 && !fKey) { // 掃描+1按鈕 Key2 = P30 ; if(!Key2 && OldKey2) { fKey = 1 ; KeyVal = 2 ; } }
if(scan==4 && !fKey) { // 掃描-1按鈕 Key3 = P30 ; if(!Key3 && OldKey3) { fKey = 1 ; KeyVal = 3 ; } }
OldKey1 = Key1 ; OldKey2 = Key2 ; OldKey3 = Key3 ;
P1 = 0xff ; P3 = dig[scan] ; ch = buffer[scan] ; // 依次顯示6位
if(modify==0) { if(fTimer) P1 = table[ch] & 0xfe ; // 倒計(jì)時(shí)結(jié)束,啟動(dòng)P10(P10低電平) else P1 = table[ch] ; } else if(modify==1 && blink && scan==0) P1 = 0xff ; else if(modify==2 && blink && scan==1) P1 = 0xff ; else if(modify==3 && blink && scan==2) P1 = 0xff ; else if(modify==4 && blink && scan==3) P1 = 0xff ; else if(modify==5 && blink && scan==4) P1 = 0xff ; else if(modify==6 && blink && scan==5) P1 = 0xff ; else P1 = table[ch] ;
scan++ ; if(scan==6) scan = 0 ;
count1++ ; if(count1==200) { count1 = 0 ; blink = ~blink ; } // 0.5s閃爍定時(shí)器
count2++ ; if(count2==400) { count2 = 0 ; fSec = 1 ; fTimer = 0 ; // 秒脈沖標(biāo)志置位,并關(guān)閉P10輸出
if(OverTime) { OverTime-- ; if(OverTime==0) modify = 0 ; } // 10s超時(shí)定時(shí)器
}
}
void Cal_cn(void) {
cn = 0 ; for(i=0 ;i<6 ;i++) { cn *= 10 ; cn += val ; }
}
void Cal_val(void) {
tmp = cn ; for(i=0 ;i<6 ;i++) { val[5-i] =(unsigned char) ( tmp % 10 ) ; tmp = tmp / 10 ; }
}
void display(void) {
tmp = cn ; for(i=0 ;i<6 ;i++) { buffer[5-i] =(unsigned char) ( tmp % 10 ) ; tmp = tmp / 10 ; }
} |