if(tmp==1||tmp==3||tmp==5||tmp==7||tmp==8||tmp==10)
tmp_days=tmp_days+31;
elseif(tmp==4||tmp==6||tmp==9||tmp==11)
tmp_days=tmp_days+30;
elseif(tmp==2)
{if(year%4==0)tmp_days=tmp_days+29;
elsetmp_days=tmp_days+28;
}
tmp_days=tmp_days+day-1;weekday=(weekday+tmp_days%7)%7+1;
return;
}
/***查询是否有闹钟时间项符合触发条件***/
if(count_down==5000)
{if((alarm_stop||sound)&&alarm[cur_alarm_active].m!
=min)//触发后1分钟
{alarm_stop=0;sound=0;}//自动关蜂鸣器
if(sound==0&&alarm_stop==0)//没有已触发的闹钟项
for(tmp=0;tmp<8;tmp++)//则查询8个闹钟项内是否有符合条件的
{if(((alarm_en>>tmp)&1)==0)continue;//该闹钟项不启用
if(((alarm_wk>>tmp)&1)==1)//该闹钟项周末有效
{if(weekday!
=6&&weekday!
=7)continue;}//当前不是星期六或星期天
else
{if(weekday==6||weekday==7)continue;}
if(alarm[tmp].h==hour&&alarm[tmp].m==min)//比较当前时间与该
{sound=1;cur_alarm_active=tmp;break;}//闹钟项的时间
}
return;
}
if(count_down==0)//过了一秒钟
{count_down=8000;
sec++;
if(sec==60)
{sec=0;
min++;
if(min==60)
{min=0;
hour++;
if(hour==24)
{hour=0;day++;
switch(day)
{case29:
if(month==2&&year%4){day=1;month=3;}
break;
case30:
if(month==2&&year%4==0){day=1;month=3;}
break;
case31:
if(month==4||month==6||month==9||month==11)
{day=1;month++;}
break;
case32:
day=1;month++;
if(month==13){month=1;year++;}
}
}
}
}
}
}
voidTimer1ISR(void)interrupt3using2//定时器2中断,用于按键扫描
{unsignedcharkeytmp;
chartmp;
TH1=0x15;TL1=0xa0;//每30ms中断一次
/***当前显示的内容***/
if(show_status==0)//当前正在设置闹钟项
{display[0]=cur_alarm_set;display[1]=0xf;
display[2]=alarm[cur_alarm_set].h/10;display[3]=alarm[cur_alarm_set].h%10;
display[4]=alarm[cur_alarm_set].m/10;display[5]=alarm[cur_alarm_set].m%10;
display[6]=(alarm_wk>>cur_alarm_set)&1;display[7]=(alarm_en>>cur_alarm_set)&1;
}
if(show_status==1||show_status==3)//当前显示或设置日期
{display[0]=year/10;display[1]=year%10;display[2]=month/10;
display[3]=month%10;display[4]=day/10;display[5]=day%10;
display[6]=0xf;display[7]=weekday;
}
if(show_status==2||show_status==4)//当前显示或设置时间
{display[0]=hour/10;display[1]=hour%10;display[2]=min/10;
display[3]=min%10;display[4]=sec/10;display[5]=sec%10;
display[6]=0xf;display[7]=0xf;//最后两后无显示
}
/***按键扫描及处理***/
keytmp=~(P1)&0x0f;
if(keytmp==0){km=0;kp=0;}
else
{if(km==0)km=1;
else
{if(kp==0)
{kp=1;
if(keytmp==1)//第一个按钮
{if(sound){alarm_stop=1;sound=0;}//如果闹钟正响,按此键停止
elseif((show_status==1||show_status==2)&&led_on)//正显示日期或时间
{show_status=0;cur_alarm_set=0;attr=0x3f;}//进入闹钟设置
elseif(show_status==0)//如正在设置闹钟时间项
{show_status=2;new_alarm_info=1;attr=0xff;}//返回当前时间显示
return;
}
if(keytmp==2&&led_on)//第二个按钮,仅当数码管打开时有效
{switch(attr)
{case0xff:
if(show_status==1)show_status=2;//在显示时间与日期间切换
elseif(show_status==2)show_status=1;
break;
case0x3f:
if(show_status==0)cur_alarm_set=(cur_alarm_set+1)%8;
elseif(show_status==3)
year=(year+1)%50;//当前日期的“年”加1
elseif(show_status==4)
hour=(hour+1)%24;//当前时间的“时”加1
break;
case0xcf:
if(show_status==0)//闹钟设置的“时”加1
alarm[cur_alarm_set].h=(alarm[cur_alarm_set].h+1)%24;
elseif(show_status==3)
{month++;//当前日期的“月”加1
if(month==13)month=1;}
elseif(show_status==4)
min=(min+1)%60;//当前时间的“分”加1
break;
case0xf3:
if(show_status==0)
alarm[cur_alarm_set].m=(alarm[cur_alarm_set].m+1)%60;
elseif(show_status==3)
{day++;//当前日期的“日”加1
if(day==32)day=1;}
elseif(show_status==4)
{count_down=8000;
sec=(sec+1)%60;}//当前时间的“秒”加1
break;
case0xfd:
if(show_status==0)
alarm_wk^=0x1<break;
case0xfe:
if(show_status==0)
alarm_en^=0x1<}//endofswitch(attr)
return;
}//endofif(keytmp==1)
if(keytmp==4)//第三个按钮
{switch(attr)
{case0xff:
if(show_status==1||show_status==2)
led_on=~led_on;//打开或关闭数码管显示
break;
case0x3f:
if(show_status==0)//如果正在设置闹钟
{if(cur_alarm_set==0)cur_alarm_set=7;
elsecur_alarm_set--;}
elseif(show_status==3)//当前日期的“年”减1
{if(year==0)year=49;elseyear--;}
elseif(show_status==4)//当前时间的“时”减1
{tmp=hour-1;if(tmp<0)hour=23;elsehour=tmp;}
break;
case0xcf:
if(show_status==0)//闹钟设置的“时”减1
{tmp=alarm[cur_alarm_set].h-1;
if(tmp<0)alarm[cur_alarm_set].h=23;
elsealarm[cur_alarm_set].h=tmp;
}
elseif(show_status==3)
{month--;//当前日期的“月”减1
if(month==0)month=12;}
elseif(show_status==4)
{tmp=min-1;//当前时间的“分”减1
if(tmp<0)min=59;elsemin=tmp;}
break;
case0xf3:
if(show_status==0)//闹钟设置的“分钟”减1
{tmp=alarm[cur_alarm_set].m-1;
if(tmp<0)alarm[cur_alarm_set].m=59;
elsealarm[cur_alarm_set].m=tmp;
}
elseif(show_status==3)
{day--;//当前日期的“日”减1
if(day==0)day=31;}
elseif(show_status==4)
{tmp=sec-1;//当前时间的“秒”减1
count_down=8000;
if(tmp<0)sec=59;elsesec=tmp;}
break;
case0xfd:
if(show_status==0)//切换周末标志
alarm_wk^=0x1<break;
case0xfe:
if(show_status==0)//切换启用标志
alarm_en^=0x1<}//endofswitch(attr)
return;
}//endofif(keytmp==2)
if(keytmp==8&led_on)//第四个按钮,仅当数码管打开时有效
{switch(attr)
{case0xff:
if(show_status==1)//如果当前显示日期
show_status=3;//切换到调准日期状态
elseif(show_status==2)//如果当前显示时间
show_status=4;//切换到调准时间状态
attr=0x3f;break;//第一、二个数码管闪烁
case0x3f:
attr=0xcf;break;//第三、四个数码管闪烁
case0xcf:
attr=0xf3;break;//第五、六个数码管闪烁
case0xf3:
if(show_status==0)attr=0xfd;//第七个数码管闪烁