}
//---------日期与时间值转换为数字字符----------------// void Format_DateTime(uchar d,uchar *a) {
a[0]=d/10+'0'; a[1]=d+'0'; }
//判断是否为闰年
uchar isLeapYear(uint y)
{ return (y%4==0&&y0!=0)||(y@0==0);} //求自2000.1.1开始的任何一天是星期几 //函数没有通过,求出总天数后再求星期几 //因为求总天数可能会超出uint的范围 void RefreshWeekDay()
{ uint i,d,w=5; //已知1999.12.31是周五 for(i=2000;i<2000+DateTime[6];i++) { } d=0;
for(i=1;i //保存星期,0~6表示星期日,星期一,二,...,六,为了与DS1302的星期 格式匹配,返回值需要加1 26 / 53 d=isLeapYear(i)?366:365; w=(w+d)%7; DateTime[5]=(w+d)%7+1; } //*****年月日时分++/--********// void DateTime_Adjust(char x) { { 中断二月 天数 27 / 53 switch(Adjust_Index) case 6: //年00-99 if(x==1&&DateTime[6]<99) DateTime[6]++; if(x==-1&&DateTime[6]>0) DateTime[6]--; //获取2月天数 MonthsDays[2]=isLeapYear(2000+DateTime[6])?29:28; //如果年份变化后当前月份的天数大于上限则设为上限 if(DateTime[3]>MonthsDays[DateTime[4]]) { DateTime[3]=MonthsDays[DateTime[4]];} RefreshWeekDay(); //刷新星期 break; //月01-12 case 4: if(x==1&&DateTime[4]<12) DateTime[4]++; if(x==-1&&DateTime[4]>1) DateTime[4]--; MonthsDays[2]=isLeapYear(2000+DateTime[6])?29:28; if(DateTime[3]>MonthsDays[DateTime[4]]) { DateTime[3]=MonthsDays[DateTime[4]];} RefreshWeekDay(); break; //日00-28、29、30、31,调节之前首先根据年份得出该年 case 3: } } MonthsDays[2]=isLeapYear(2000+DateTime[6])?29:28; //根据当前月份决定调节日期的上限 if(x==1&&DateTime[3] //时 case 2: if(x==1&&DateTime[2]<23) DateTime[2]++; if(x==-1&&DateTime[2]>0) DateTime[2]--; break; //分 case 1: if(x==1&&DateTime[1]<59) DateTime[1]++; if(x==-1&&DateTime[1]>0) DateTime[1]--; break; //---定时器0每秒刷新LCD显示----// void T0_INT() interrupt 1 { TH0=-50000/256; TL0=-50000%6; if(++tCount !=2) return; tCount=0; //按指定格式生成待显示的日期时间串 Format_DateTime(DateTime[6],LCD_DSY_BUFFER1+5); Format_DateTime(DateTime[4],LCD_DSY_BUFFER1+8); 28 / 53 Format_DateTime(DateTime[3],LCD_DSY_BUFFER1+11); //星期 strcpy(LCD_DSY_BUFFER1+13,WEEK[DateTime[5]-1]); //时分秒 Format_DateTime(DateTime[2],LCD_DSY_BUFFER2+5); Format_DateTime(DateTime[1],LCD_DSY_BUFFER2+8); Format_DateTime(DateTime[0],LCD_DSY_BUFFER2+11); //显示年月日,星期,时分秒 Display_LCD_String(0x00,LCD_DSY_BUFFER1); Display_LCD_String(0x40,LCD_DSY_BUFFER2); } //----------键盘中断(INT0)-------------// void EX_INT0() interrupt 0 { if(K1==0) //选择调整对象(Y M D H M) { DelayMS(10); if(K1==0){ //while(K1==0); if(Adjust_Index==-1||Adjust_Index==1) { } Adjust_Index--; if(Adjust_Index==5) Adjust_Index=4; LCD_DSY_BUFFER2[13]='['; 29 / 53 Adjust_Index=7; LCD_DSY_BUFFER2[14]=Change_Flag[Adjust_Index]; //显示调节对象 } else if(K2==0) //加 LCD_DSY_BUFFER2[15]=']'; } { //while(K2==0); DelayMS(10); if(K2==0) DateTime_Adjust(1); } else if(K3==0) { } else if(K4==0) { //减 DelayMS(10);//while(K3==0); if(K3==0) DateTime_Adjust(-1); //确定 //while(K4==0); DelayMS(10); if(K4==0){ SET_DS1302(); //将调整后的时间写入DS1302 LCD_DSY_BUFFER2[13]=' '; LCD_DSY_BUFFER2[14]=' '; LCD_DSY_BUFFER2[15]=' '; Adjust_Index=-1; 30 / 53