#include<reg52.h>
#include "ds1302.h"
#include "main.h"
bit readtime_f; //定义读时间标志
bit key_f = 0;
bit readtemp_f = 0;key_fun_f = 0;
sbit ADJTIME = P1^3; //校准时间
sbit SETALARM = P1^4; //设置闹钟
unsigned char key_value,key_select,key_fun = 0;
unsigned int key_count = 0;
SYSTEMTIME timer,timer1;
void timer0_init() //T0初始化
{
TMOD = 0x01; //使用模式1,16位定时器
TH0 = 0X3C;
TL0 = 0XB0;
EA = 1; //总中断打开
ET0 = 1; //定时器中断打开
TR0 = 1; //定时器开关打开
}
void timer0_isr() interrupt 1
{
static unsigned char counter = 0,counter1 = 0;
TH0 = 0X3C;
TL0 = 0XB0;
counter ++;
key_f = 1;
if(counter ==10) //500ms
{
counter = 0;
readtime_f = 1; //读时间标志位置1
counter1 ++;
if(counter1 ==2) //5s读一次温度值
{
counter1 = 0;
readtemp_f = 1; //读温度标志位置1
}
}
}
void main()
{
unsigned char readtemp;
lcd_initial();
timer0_init();
ds1302_init();
while(1) //主循环
{
if(readtemp_f&&!key_fun_f)//读取温度
{
readtemp_f = 0;
readtemp = read_temperature();
LCD_Write_Char(13,1,readtemp/10+'0'); //显示温度值
LCD_Write_Char(14,1,readtemp%10+'0');
}
if(readtime_f&&!key_fun_f)
{
readtime_f = 0;
Ds1302_Read_Time(&timer);
timer_alarm(&timer,&timer1);
LCD_Write_Char(3,1,timer.Hour/10 + '0');
LCD_Write_Char(4,1,timer.Hour%10 + '0');
LCD_Write_Char(6,1,timer.Minute/10 + '0');//显示秒
LCD_Write_Char(7,1,timer.Minute%10 + '0');
LCD_Write_Char(9,1,timer.Second/10 + '0');//显示秒
LCD_Write_Char(10,1,timer.Second%10 + '0');
LCD_Write_Char(3,0,timer.Year/10 + '0'); //显示年
LCD_Write_Char(4,0,timer.Year%10 + '0');
LCD_Write_Char(6,0,timer.Month/10 + '0'); //显示月
LCD_Write_Char(7,0,timer.Month%10 + '0');
LCD_Write_Char(9,0,timer.Day/10+ '0'); //显示日
LCD_Write_Char(10,0,timer.Day%10 + '0');
LCD_Write_Char(14,0,timer.Week%10 + '0'); //显示周
}
if(key_f)
{
key_f = 0;
key_value = key();
/****************************加值键**************************************/
if(key_value==0x01)
{
if(key_select==0x01)//修改年
{
timer.Year ++;
if(timer.Year==100)
timer.Year = 0;
LCD_Write_Char(4,0,timer.Year%10 + '0');
LCD_Write_Char(3,0,timer.Year/10 + '0');
}
else if(key_select==0x02)//修改月
{
timer.Month ++;
if(timer.Month==13)
timer.Month = 1;
LCD_Write_Char(7,0,timer.Month % 10 + '0');
LCD_Write_Char(6,0,timer.Month / 10 + '0');
}
else if(key_select==0x03)//修改日
{
timer.Day ++;
if(timer.Day==32)
timer.Day = 1;
LCD_Write_Char(10,0,timer.Day % 10 + '0');
LCD_Write_Char(9,0,timer.Day / 10 + '0');
}
else if(key_select==0x04)//修改星期
{
timer.Week ++;
if(timer.Week==8)
timer.Week = 1;
LCD_Write_Char(14,0,timer.Week % 10 + '0');
LCD_Write_Char(13,0,':');
}
else if(key_select==0x05)//修改时
{
timer.Hour ++;
if(timer.Hour==24)
timer.Hour = 0;
LCD_Write_Char(4,1,timer.Hour % 10 + '0');
LCD_Write_Char(3,1,timer.Hour / 10 + '0');
}
else if(key_select==0x06)//修改分
{
timer.Minute ++;
if(timer.Minute==60)
timer.Minute = 0;
LCD_Write_Char(7,1,timer.Minute % 10 + '0');
LCD_Write_Char(6,1,timer.Minute / 10 + '0');
}
else if(key_select==0x07)//修改秒
{
timer.Second ++;
if(timer.Second==60)
timer.Second = 0;
LCD_Write_Char(10,1,timer.Second % 10 + '0');
LCD_Write_Char(9,1,timer.Second / 10 + '0');
}
}
/***************************选择键********************************************/
else if(key_value == 0x02)
{
key_select ++;
if(key_select>0x07)
{
key_select = 0x00;
WRDcomm(0x0c);
}
else
{
WRDcomm(0x0d);
switch(key_select)
{
case 0x01:LCD_Write_Char(3,0,timer.Year/10 + '0');break;
case 0x02:LCD_Write_Char(6,0,timer.Month/10 + '0');break;
case 0x03:LCD_Write_Char(9,0,timer.Day/10 + '0');break;
case 0x04:LCD_Write_Char(13,0,':');break;
case 0x05:LCD_Write_Char(3,1,timer.Hour/10 + '0');break;
case 0x06:LCD_Write_Char(6,1,timer.Minute/10 + '0');break;
case 0x07:LCD_Write_Char(9,1,timer.Second/10 + '0');break;
default:break;
}
}
}
/******************************减值键******************************************/
else if(key_value==0x04)
{
if(key_select==0x01)//修改年
{
timer.Year --;
if(timer.Year==-1)
timer.Year = 99;
LCD_Write_Char(4,0,timer.Year%10 + '0');
LCD_Write_Char(3,0,timer.Year/10 + '0');
}
else if(key_select==0x02)//修改月
{
timer.Month --;
if(timer.Month==0)
timer.Month = 12;
LCD_Write_Char(7,0,timer.Month % 10 + '0');
LCD_Write_Char(6,0,timer.Month / 10 + '0');
}
else if(key_select==0x03)//修改日
{
timer.Day --;
if(timer.Day==0)
timer.Day = 31;
LCD_Write_Char(10,0,timer.Day % 10 + '0');
LCD_Write_Char(9,0,timer.Day / 10 + '0');
}
else if(key_select==0x04)//修改星期
{
timer.Week --;
if(timer.Week==0)
timer.Week = 7;
LCD_Write_Char(14,0,timer.Week % 10 + '0');
LCD_Write_Char(13,0,':');
}
else if(key_select==0x05)//修改时
{
timer.Hour --;
if(timer.Hour==-1)
timer.Hour = 23;
LCD_Write_Char(4,1,timer.Hour % 10 + '0');
LCD_Write_Char(3,1,timer.Hour / 10 + '0');
}
else if(key_select==0x06)//修改分
{
timer.Minute --;
if(timer.Minute==-1)
timer.Minute = 59;
LCD_Write_Char(7,1,timer.Minute % 10 + '0');
LCD_Write_Char(6,1,timer.Minute / 10 + '0');
}
else if(key_select==0x07)//修改秒
{
timer.Second --;
if(timer.Second==-1)
timer.Second = 59;
LCD_Write_Char(10,1,timer.Second % 10 + '0');
LCD_Write_Char(9,1,timer.Second / 10 + '0');
}
}
/******************************功能键***************************************/
else if(key_value == 0x08)
{
key_fun ++ ;
if(key_fun == 0x01)
{
ADJTIME = 0;
key_fun_f = 1;
}
else if(key_fun == 0x02)
{
ADJTIME = 1;
key_fun_f = 0;
WRDcomm(0x0c);
key_select = 0x00;
Ds1302_Write_Time(ds1302_sec_add,timer.Second);
Ds1302_Write_Time(ds1302_min_add,timer.Minute);
Ds1302_Write_Time(ds1302_hr_add,timer.Hour);
Ds1302_Write_Time(ds1302_day_add,timer.Week);
Ds1302_Write_Time(ds1302_date_add,timer.Day);
Ds1302_Write_Time(ds1302_month_add,timer.Month);
Ds1302_Write_Time(ds1302_year_add,timer.Year);
}
else if(key_fun == 0x03)
{
SETALARM = 0;
key_fun_f = 1;
}
else if(key_fun == 0x04)
{
SETALARM = 1; //关闭设置闹钟指示
key_fun_f = 0; //清键盘功能标志位
key_fun = 0x00;
WRDcomm(0x0c); //关闭数字闪烁
timer1.Second = timer.Second;
timer1.Minute = timer.Minute;
timer1.Hour = timer.Hour;
timer1.Week = timer.Week;
timer1.Day = timer.Day;
timer1.Month = timer.Month;
timer1.Year = timer.Year;
}
}
}
}
}
|