/*用定时器0,1602液晶,设计一个数字时钟,可以由三个按钮控制调节时间
*/
#include<reg52.h>
#include"lcd1602.h"
#include"ds18b20.h"
#include"ir.h"
#define uchar unsigned char
#define uint unsigned int
#define TIMER0_COUNT 0xEE11
uchar count,s1num,timer0_tick,count=0;
typedef struct{
char hour;
char minute;
char second;
}time;
typedef struct{
uint year;
uchar month;
char day;
}date;
time now={8,20,0};
date today={2011,6,14};
char code dayofmonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
char code weekday[7][4]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
uchar monthday(uchar year,uchar month)
{
if(month==2 && year%4==0) //润年的2月有29天
return(29);
else
return(dayofmonth[month-1]); //非闰年时的该月份天数
}
void display_week() //由年月日计算星期
{
char days;
days=(today.day+1+2*today.month+3*(today.month+1)/5+today.year+today.year/4-today.year/100+today.year/400)%7;
display_string(&weekday[days][0]);
}
static void timer0_initialize(void) //timer0 initialize
{
EA=0; //设置不接受所有中断
timer0_tick=0;
TR0=0; //关闭Timer0
TMOD=0X01; //设置Timer0为模式2,16位工作模式
TL0=(TIMER0_COUNT & 0X00FF); //设置Timer0低八位数值
TH0=(TIMER0_COUNT >> 8); //设置Timer0高八位数值
PT1=1; //设置Timer0的优先级为最高
ET0=1; //设置接受Timer0的中断
TR0=1; //启动Timer0
EA=1; //设置系统接受中断
}
void write_time(uchar add,uchar number)
{
gotoxy(2,add);
display_data(number);
}
void write_riqi(uchar add,uchar number)
{
gotoxy(1,add);
display_data(number);
}
void keyscan()
{
uchar mode_num;
if(mode==0||mode1==0)
{
delay(5);
if(mode==0||mode1==0)
{ mode_num++;
while(!mode);
if(mode1==0) mode=plus=dec=mode1=plus1=dec1=1;
TR0=0;
write_com(0x0f);
}
if(mode_num==1) gotoxy(1,4);
if(mode_num==2) gotoxy(1,7);
if(mode_num==3) gotoxy(1,10);
if(mode_num==4) gotoxy(2,2);
if(mode_num==5) gotoxy(2,5);
if(mode_num==6) gotoxy(2,8);
if(mode_num==7)
{
mode_num=0;
write_com(0x0c);
TR0=1;
}
}
if(mode_num!=0)
{
if(plus==0||plus1==0)
{
delay(5);
if(plus==0||plus1==0)
{
while(!plus);
if(plus1==0) mode=plus=dec=mode1=plus1=dec1=1;
if(mode_num==1)
{
today.year++;
write_riqi(1,today.year/100);
write_riqi(3,today.year%100);
gotoxy(1,12);
display_week();
gotoxy(1,4);
}
if(mode_num==2)
{
today.month++;
if(today.month==13) today.month=1;
write_riqi(6,today.month);
gotoxy(1,12);
display_week();
gotoxy(1,7);
}
if(mode_num==3)
{
today.day++;
if(today.day>monthday(today.year,today.month))
today.day=1;
write_riqi(9,today.day);
gotoxy(1,12);
display_week();
gotoxy(1,10);
}
if(mode_num==4)
{
now.hour++;
if(now.hour==24) now.hour=0;
write_time(1,now.hour);
gotoxy(2,2);
}
if(mode_num==5)
{
now.minute++;
if(now.minute==60) now.minute=0;
write_time(4,now.minute);
gotoxy(2,5);
}
if(mode_num==6)
{
now.second++;
if(now.second==60) now.second=0;
write_time(7,now.second);
gotoxy(2,8);
}
}
}
if(dec==0||dec==0)
{
delay(5);
if(dec==0||dec==0)
{
while(!dec);
if(plus1==0) mode=plus=dec=mode1=plus1=dec1=1;
if(mode_num==1)
{
today.year--;
write_riqi(1,today.year/100);
write_riqi(3,today.year%100);
gotoxy(1,12);
display_week();
gotoxy(1,4);
}
if(mode_num==2)
{
today.month--;
if(today.month==0) today.month=12;
write_riqi(6,today.month);
gotoxy(1,12);
display_week();
gotoxy(1,7);
}
if(mode_num==3)
{
today.day--;
if(today.day==0)
today.day=monthday(today.year,today.month);
write_riqi(9,today.day);
gotoxy(1,12);
display_week();
gotoxy(1,10);
}
if(mode_num==4)
{
now.hour--;
if(now.hour<0) now.hour=23;
write_time(1,now.hour);
gotoxy(2,2);
}
if(mode_num==5)
{
now.minute--;
if(now.minute<0) now.minute=59;
write_time(4,now.minute);
gotoxy(2,5);
}
if(mode_num==6)
{
now.second--;
if(now.second<0) now.second=59;
write_time(7,now.second);
gotoxy(2,8);
}
}
}
}
}
void display_temp()
{
uint wendu;
uchar A1,A2;
tmpchange();
wendu=tmp();
A1=wendu/10;
A2=wendu%10;
gotoxy(2,10);
display_data(A1);
display_string(".");
write_date(int_to_char[A2]);
}
void timer0(void) interrupt 1
{
TR0=0;
TL0=(TIMER0_COUNT & 0X00FF);//设置Timer0低八位数值
TH0=(TIMER0_COUNT >> 8);//设置Timer0高八位数值
TR0=1;
count++;
if(count==30) display_temp();
if(count==200)
{
count=0;
now.second++;
if(now.second==60)
{
now.second=0;
now.minute++;
if(now.minute==60)
{
now.minute=0;
now.hour++;
if(now.hour==24)
{
now.hour=0;
today.day++;
if(today.day>monthday(today.year,today.month))
{
today.day=1;
today.month++;
if(today.month==13)
{
today.month=1;
today.year++;
}
write_riqi(6,today.month);
}
write_riqi(9,today.day);
gotoxy(1,12);
display_week();
}
write_time(1,now.hour);
}
write_time(4,now.minute);
}
write_time(7,now.second);
}
}
void main()
{
init_lcd();
IR_init();
timer0_initialize();
gotoxy(1,1);
display_string("2011/06/14 ");
display_week();
gotoxy(2,1);
display_string("08:20:00");
gotoxy(2,10);
display_string("00.0C");
while(1) keyscan();
}