// 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) { // 秒标志被置位?
fSec = 0 ; // 清除秒标志
if(!fSTOP && modify==0) {
if(cn!=0) cn-- ; else { fTimer = 1 ; fSTOP = 1 ; }
}
display() ;
}
if(fKey) { // 有按键按下?
fKey = 0 ; // 清除按件标志,并处理该按键
if(KeyVal==1) { // 是调整按钮?
fSTOP = 0 ; modify++ ; // 进入相应状态,并重置超时定时器
if(modify==7) { modify = 0 ; OverTime = 0 ; } else OverTime = 10 ;
}
if(KeyVal==2 && modify!=0) { // +1按钮,相应位+1
Cal_val() ;
if(val[modify-1]<9) val[modify-1]++ ;
Cal_cn() ;
display() ; OverTime = 10 ; // 刷新显示,并重置超时定时器
}
if(KeyVal==3 && modify!=0) { // -1按钮,相应位-1
Cal_val() ;
if(val[modify-1]>0) val[modify-1]-- ;
Cal_cn() ;
display() ; OverTime = 10 ; // 刷新显示,并重置超时定时器
}
}
}
}
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) { // 扫描调整按钮
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 ; // 倒计时结束,启动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闪烁定时器
count2++ ;
if(count2==400) {
count2 = 0 ; fSec = 1 ; fTimer = 0 ; // 秒脉冲标志置位,并关闭P10输出
if(OverTime) { OverTime-- ; if(OverTime==0) modify = 0 ; } // 10s超时定时器
}
}
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 ;
}
}