uchar keyscan()//按键扫描
{
uchar keytemp=0;
keytemp=(P3&0x0f);
if(keytemp!=0x0f)
{
keypress++;
}
else
{
keypress=0;
}
if(keypress>3)
{
keypress=0;
keydown=1;
switch(keytemp)
{
case 0x0e: keyvalue=0x05; break;
case 0x0d: keyvalue=0x06; break;
case 0x0b: keyvalue=0x07; break;
case 0x07: keyvalue=0x08; break;
}
return 0xff; //按下后不松开的状态
}
if((keydown==1)&&(keytemp==0x0f))//按下后松开的状态
{
keydown=0;
return keyvalue;
}
return 0x00;//无按键按下的状态
}
void Timer0Init(void)
{
TMOD |= 0x01;
TL0 = 0x66;
TH0 = 0xFC; //1ms
TR0 = 1;
ET0=1;
EA=1;
}
void Timer0_ISR() interrupt 1
{
TL0 = 0x66;
TH0 = 0xFC;
count++;
keyc++;
disc++;
if(count>1000)
{
second++;
count=0;
}
if(keyc>10)
{
keyflag=1;
keyc=0;
}
if(disc>500)
{
disornot=~disornot;
disc=0;
}
if(beepflag==1)
{
beep=~beep;
}
}
void main()
{
uchar key_value;
Timer0Init();
timecalculate();
while(1)
{
if(keyflag==1)//按键定时器扫描 每10ms
{
key_value=keyscan();
keyflag=0;
}
switch(key_value)
{
case 0x05: //按键K5
if(k5)
{
disflag=~disflag;//时钟、闹钟切换
}
break;
case 0x06: //按键K6 加
if(k6)
{
if(disflag)
{
switch(cursor)
{
case 3: alarm_hour++; if(alarm_hour>=24) alarm_hour=0;break;
case 4: alarm_minute++; if(alarm_minute>=60){alarm_minute=0;alarm_hour++;} break;
}
}
else
{
switch(cursor)
{
case 1: hour++; if(hour>=24) hour=0;break;
case 2: minute++; if(minute>=60){minute=0;hour++;} break;
}
}
}
break;
case 0x07: //按键K7 减
if(k7)
{
if(disflag)
{
switch(cursor)
{
case 3: alarm_hour--; if(alarm_hour<0) alarm_hour=23;break;
case 4: alarm_minute--; if(alarm_minute<0){alarm_minute=59;alarm_hour--;} break;
}
}
else
{
switch(cursor)
{
case 1: hour--; if(hour<0) hour=23;break;
case 2: minute--; if(minute<0){minute=59;hour--;} break;
}
}
}
break;
case 0x08: //按键K8
if(disflag)//闹钟
{
k5=0;//切换不可用
k6=1;k7=1;//调节键可用
if(cursor==3) cursor=4; //闹钟 时 闪烁标志
else if(cursor==4)
{
cursor=0;k5=1;k6=0,k7=0;
}
else cursor=3; //闹钟 分 闪烁标志
}
else//时钟
{
k5=0;//切换不可用
k6=1;k7=1;//调节键可用
if(cursor==1) cursor=2; //时钟 时 闪烁标志
else if(cursor==2)
{
cursor=0;k5=1;k6=0;k7=0;
}
else cursor=1; //时钟 分 闪烁标志
}
break;
default: break;
}
if(disflag==0)//时间显示
{
if(cursor==1)
{
if(disornot) {time_disp();disbuff[0]=0;disbuff[1]=0;}//闪烁 下同
else time_disp();
}
else if(cursor==2)
{
if(disornot) {time_disp();disbuff[3]=0;disbuff[4]=0;}
else time_disp();
}
else
time_disp();
}
else//闹钟显示
{
if(cursor==3)
{
if(disornot) {alarm_disp();disbuff[3]=0;disbuff[4]=0;}
else alarm_disp();
}
else if(cursor==4)
{
if(disornot) {alarm_disp();disbuff[6]=0;disbuff[7]=0;}
else alarm_disp();
}
else
alarm_disp();
}
if(hour==alarm_hour&&minute==alarm_minute)//闹铃
{
beepflag=1;
}
else
beepflag=0;
key_value=0;//清除键值
timecalculate();
display();
}
} |