/***********************************************************************************/
/* LCD(HS12864图形点阵液晶)电子钟+万年历程序 */
/* LCD接线见LCD.H定义,P3口接独立键盘。 */
/* P3.0切换模式(正常显示/设置),P3.1设置值加1,P3.2设置值减,P3.3切换设置项 */
/* Designer : CanycalL chx111081@163.com */
/* 版本:V 1.0 */
/* 浙江天煌科技实业有限公司 */
/***********************************************************************************/
#include
#include
#include
#include
#include
uint year = 2007;
uchar month = 8;
uchar day = 1;
uchar hour = 12;
uchar minute = 00;
uchar second = 00;
uchar week_day;
uchar irq_count=0; //中断计数
uint year_old, month_old, day_old;
// 定义显示寄存器
uchar dsp_data_year[5]="2007";
uchar dsp_data_month[3]="08";
uchar dsp_data_day[3]="01";
uchar dsp_time_hh[3]="12";
uchar dsp_time_mm[3]="00";
uchar dsp_time_ss[3]="00";
uchar dsp_week[3]="三";
// 每个月的天数
uchar month_day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
uchar code week_tab[] = {0,1,4,4,0,2,5,0,3,6,1,4,6};
// 判断是否为闰年,并设置2月份的天数。
void run_nian(uint year)
{ if ((year%4==0 && year%100 != 0 ) || year%400==0)
month_day[1]=29;
else
month_day[1]=28;
return ;
}
// 由日期计算星期
uchar CalculateWeekDay(uint Year,uchar Month,uchar Date)
{
uchar week;
if((Month<3) && (!(Year&0x03) && (Year%1000) || (!(Year%400))))
{ Date--; }
week = (Date + Year + Year/4 + Year/400 - Year/100 + week_tab[Month]-2)%7;
return week;
}
//----------------------------主函数-----------------------------//
void main(void)
{ bit mode_set=0; //模式控制 1 设置模式,0 正常模式
uchar sign[6]={1,1,1,1,1,1}; //放置日期和时间反白显示标志位:1 正常显示,0 反白显示
char i=0;
RST=1; // LCD reset signal ,when RST=0 LCD reset
LCD_Init(); // LCD initialization
P0=0xff;
EA=1; ET0=1;
TMOD=0x01; // T0方式1计时
TH0=0x4C; // 晶振:11.0592MHz
// TH0=0x3C; // 晶振:12MHz
TL0=0x00; // 晶振:11.0592MHz
// TL0=0xB0; // 晶振:12MHz
TR0=1; /* 开中断,启动定时器 */
Out_Char(40,0,1,"年");
Out_Char(72,0,1,"月");
Out_Char(104,0,1,"日");
Out_Char(48,4,1,":");
Out_Char(72,4,1,":");
Out_Char(40,2,1,"星期");
Out_Char(0,6,1,"天煌科技有限公司");
week_day=CalculateWeekDay(year, month,day);
switch(week_day)
{ case 0 : Out_Char(72,2,1,"日"); break;
case 1 : Out_Char(72,2,1,"一"); break;
case 2 : Out_Char(72,2,1,"二"); break;
case 3 : Out_Char(72,2,1,"三"); break;
case 4 : Out_Char(72,2,1,"四"); break;
case 5 : Out_Char(72,2,1,"五"); break;
case 6 : Out_Char(72,2,1,"六"); break;
default : break;
}
while(1)
{
if(LEFT_KEY)
{ mode_set = ~mode_set;
if (mode_set == 1) //进入设置模式
{
EA = 0; //关中断(时钟停止走动)
sign[0]=0; //设置反白显示标志位
}
else
{
EA = 1; i = 0;
week_day=CalculateWeekDay(year, month,day);
switch(week_day)
{ case 0 : Out_Char(72,2,1,"日"); break;
case 1 : Out_Char(72,2,1,"一"); break;
case 2 : Out_Char(72,2,1,"二"); break;
case 3 : Out_Char(72,2,1,"三"); break;
case 4 : Out_Char(72,2,1,"四"); break;
case 5 : Out_Char(72,2,1,"五"); break;
case 6 : Out_Char(72,2,1,"六"); break;
default : break;
}
sign[0]=sign[1]=sign[2]=sign[3]=sign[4]=sign[5]=1; // 清所有反白显示标志位
}
while(LEFT_KEY);
}
if(RIGHT_KEY && mode_set == 1)
{ sign[i]=1;
if ( i==5 ) i=0;
else i=i+1;
sign[i]=0;
while(RIGHT_KEY);
}
if(UP_KEY && mode_set == 1)
{ switch(i)
{ case 0 :
year=year+1; run_nian(year);
dsp_data_year[0]=year/1000+'0';
dsp_data_year[1]=year/100%10+'0';
dsp_data_year[2]=year%100/10+'0';
dsp_data_year[3]=year%100%10+'0';
break;
case 1 :
month=month+1; if (month > 12) month=1;
dsp_data_month[0]=month/10+'0';
dsp_data_month[1]=month%10+'0';
break;
case 2 :
day=day+1; if (day > month_day[month-1]) day=1;
dsp_data_day[0]=day/10+'0';
dsp_data_day[1]=day%10+'0';
break;
case 3 :
hour=hour+1; if (hour > 23) hour=0;
dsp_time_hh[0]=hour/10+'0';
dsp_time_hh[1]=hour%10+'0';
break;
case 4 :
minute=minute+1; if (minute > 59) minute=0;
dsp_time_mm[0]=minute/10+'0';
dsp_time_mm[1]=minute%10+'0';
break;
case 5 :
second=second+1; if (second > 59) second=0;
dsp_time_ss[0]=second/10+'0';
dsp_time_ss[1]=second%10+'0';
break;
default : break;
} //end switch(i)
while(UP_KEY);
}
if(DOWN_KEY && mode_set == 1)
{ switch(i)
{ case 0 :
year=year-1; run_nian(year);
dsp_data_year[0]=year/1000+'0';
dsp_data_year[1]=year/100%10+'0';
dsp_data_year[2]=year%100/10+'0';
dsp_data_year[3]=year%100%10+'0';
break;
case 1 :
month=month-1; if (month < 1) month=12;
dsp_data_month[0]=month/10+'0';
dsp_data_month[1]=month%10+'0';
break;
case 2 :
day=day-1; if (day < 1) day=month_day[month-1];
dsp_data_day[0]=day/10+'0';
dsp_data_day[1]=day%10+'0';
break;
case 3 :
if (hour == 0)
hour=23;
else
hour=hour-1;
dsp_time_hh[0]=hour/10+'0';
dsp_time_hh[1]=hour%10+'0';
break;
case 4 :
if (minute == 0)
minute=59;
else
minute=minute-1;
dsp_time_mm[0]=minute/10+'0';
dsp_time_mm[1]=minute%10+'0';
break;
case 5 :
if (second == 0)
second=59;
else
second=second-1;
dsp_time_ss[0]=second/10+'0';
dsp_time_ss[1]=second%10+'0';
break;
default : break;
} //end switch(i)
while(DOWN_KEY);
}
if ( day_old != day || month_old != month || year_old != year )
{ week_day=CalculateWeekDay(year, month,day);
switch(week_day)
{ case 0 : Out_Char(72,2,1,"日"); break;
case 1 : Out_Char(72,2,1,"一"); break;
case 2 : Out_Char(72,2,1,"二"); break;
case 3 : Out_Char(72,2,1,"三"); break;
case 4 : Out_Char(72,2,1,"四"); break;
case 5 : Out_Char(72,2,1,"五"); break;
case 6 : Out_Char(72,2,1,"六"); break;
default : break;
} // end switch(week_day)
year_old = year; month_old = month; day_old = day;
} // end if (day_old != day)
Out_Char(8,0,sign[0],dsp_data_year); // year
Out_Char(56,0,sign[1],dsp_data_month); // month
Out_Char(88,0,sign[2],dsp_data_day); // day
Out_Char(32,4,sign[3],dsp_time_hh); // hour
Out_Char(56,4,sign[4],dsp_time_mm); // minute
Out_Char(80,4,sign[5],dsp_time_ss); // second
} // end while
} // end main
/* 定时计数器0的中断服务子程序 */
//*
void timer0(void) interrupt 1 using 1 // 50mS中断一次
{
TH0=0x4C; // 晶振:11.0592MHz
// TH0=0x3C; // 晶振:12MHz
TL0=0x00; // 晶振:11.0592MHz
// TH0=0xB0; // 晶振:12MHz
irq_count++;
if (irq_count>=20) // 1 S
{ irq_count = 0;
second++;
if (second >= 60)
{ second = 0;
minute++;
if (minute >= 60)
{ minute = 0;
hour++ ;
if (hour >= 24)
{ hour = 0 ;
day++;
if (day > month_day[month-1])
{ day=1;
month++;
if (month > 12)
{ month=1;
year++;
run_nian(year);
dsp_data_year[0]=year/1000+'0';
dsp_data_year[1]=year/100%10+'0';
dsp_data_year[2]=year%100/10+'0';
dsp_data_year[3]=year%100%10+'0';
} //end if(month > 12)
dsp_data_month[0]=month/10+'0';
dsp_data_month[1]=month%10+'0';
} // end if(day > month_day[month-1])
dsp_data_day[0]=day/10+'0';
dsp_data_day[1]=day%10+'0';
} // end if (hour >= 24)
dsp_time_hh[0]=hour/10+'0';
dsp_time_hh[1]=hour%10+'0';
} // end if(minute >= 60)
dsp_time_mm[0]=minute/10+'0';
dsp_time_mm[1]=minute%10+'0';
} // end if (second >= 60)
dsp_time_ss[0]=second/10+'0';
dsp_time_ss[1]=second%10+'0';
} // end if (irq_count>=20)
}
//*/