#include <reg51.h>
#include <absacc.h>
#include <intrins.h>
#include "lcd.h"
#include "ds1302.h"
#include "lcdshow.h"
#include "key.h"
bit CLKSTOP;
uchar data count,temp,up_flag,down_flag,done;
uint data year;
/*********************************************/
sbit Set = P3^1; //模式切换键
sbit Up = P3^2; //加法按钮
sbit Down = P3^3; //减法按钮
sbit out = P3^4; //立刻跳出调整模式按钮
/*********************************************/
void mdelay(uint delay)
{ uint i;
for(;delay>0;delay--)
{for(i=0;i<62;i++) //1ms延时.
{;}
}
}
/*********************************************/
void setting() //选中后反白显示
{
uchar i;
switch(count)
{
case 1: //year
r_ShowNumber(0,24,date_buf1[0]);
r_ShowNumber(0,32,date_buf1[1]);
i = date_buf[6]>>4;
i &= 0x0f;
r_ShowNumber(0,40,i);
i = date_buf[6] & 0x0f;
r_ShowNumber(0,48,i);
break;
case 2: //month
show_date();
i = date_buf[4]>>4;
i &= 0x01;
r_ShowNumber(0,64,i);
i = date_buf[4] & 0x0f;
r_ShowNumber(0,72,i);
break;
case 3://day
show_date();
i = date_buf[3]>>4;
i &= 0x03;
r_ShowNumber(0,88,i);
i = date_buf[3] & 0x0f;
r_ShowNumber(0,96,i);
break;
case 4://week
i=date_buf[5]&0x0f;
r_ShowChina(4,72,i);
break;
case 5://hour
show_date();
i = date_buf[2]>>4;
i &= 0x03;
r_ShowNumber(2,16,i);
i = date_buf[2] & 0x0f;
r_ShowNumber(2,24,i);
break;
case 6://min
show_time();
i = date_buf[1]>>4;
i &= 0x07;
r_ShowNumber(2,48,i);
i = date_buf[1] & 0x0f;
r_ShowNumber(2,56,i);
break;
case 7://sec
show_time();
i = date_buf[0]>>4;
i &= 0x07;
r_ShowNumber(2,80,i);
i = date_buf[0] & 0x0f;
r_ShowNumber(2,88,i);
break;
default:
show_date();
show_time();
break;
}
}
void Setkey()
{
Set=1;
if(Set==0)
{
mdelay(8);
count=count+1; //Setkey按一次,count就加1
done=1; //进入调整模式
setting();
while(Set==0); //等待按键释放
}
}
void outkey() //跳出调整模式,返回默认显示
{ uchar Second;
if(out==0)
{ count=0;
Second=read_all_1302(DS1302_SECOND);
write_all_1302(0x8e,0x00); //写入允许
write_all_1302(0x80,Second&0x7f);
write_all_1302(0x8E,0x80); //禁止写入
done=0;
ClearScreen(1);
ClearScreen(2);
mdelay(2);
show_date();
show_time();
show_hz();
show_temp();
while(out==0);
//TR0=1;
}
}
/***************************************************************************************************/
void addtime()
{ Up=1;
if(Up==0)
{ mdelay(8);
switch(count)
{
case 1:
temp=read_all_1302(DS1302_YEAR); //读取年数
temp=temp+1; //年数加1
if((temp&0x0f)==0x0a) temp=(((temp>>4)&0x0f)+1)*16;
up_flag=1;
if(temp>0x85)
temp=0;
break;
case 2:
temp=read_all_1302(DS1302_MONTH); //读取月数
temp=temp+1; //月数加1
if (temp==0x0a) temp=0x10;
up_flag=1;
if(temp>0x12)
temp=1;
break;
case 3:
temp=read_all_1302(DS1302_DAY); //读取日数
temp=temp+1; //日数加1
if((temp&0x0f)==0x0a) temp=(((temp>>4)&0x0f)+1)*16;
up_flag=1;
if(temp>0x31)
temp=1;
break;
case 4:
temp=read_all_1302(DS1302_WEEK); //读取星期数
temp=temp+1; //星期数加1
if (temp==0x0a) temp=0x10;
up_flag=1;
if(temp>0x7)
temp=1;
break;
case 5:
temp=read_all_1302(DS1302_HOUR); //读取小时数
temp=temp+1; //小时数加1
if((temp&0x0f)==0x0a) temp=(((temp>>4)&0x0f)+1)*16;
up_flag=1;
if(temp>0x23) //超过23小时,清零
temp=0;
break;
case 6:
temp=read_all_1302(DS1302_MINUTE); //读取分数
temp=temp+1; //分数加1
if((temp&0x0f)==0x0a) temp=(((temp>>4)&0x0f)+1)*16;
up_flag=1;
if(temp>0x59) //超过59分,清零
temp=0;
break;
case 7:
temp=read_all_1302(DS1302_SECOND); //读取秒数
temp=temp+1; //秒数加1
if((temp&0x0f)==0x0a) temp=(((temp>>4)&0x0f)+1)*16;
up_flag=1; //数据调整后更新标志
if((temp&0x7f)>0x59) //超过59秒,清零
temp=0;
break;
default:break;
}
while(Up==0);
}
// flag=1;
}
/***************************************************************************************************/
void subtime()
{ Down=1;
if(Down==0)
{ mdelay(8);
switch(count)
{
case 1:
temp=read_all_1302(DS1302_YEAR); //读取年数
temp=temp-1; //年数减1
if(((temp&0x0f)==0x0f)&&(((temp>>4)&0x0f)>=0&&((temp>>4)&0x0f)<=8)) temp=temp&0x0f0+0x09;
down_flag=1;
if(temp==-1)
temp=0x85;
break;
case 2:
temp=read_all_1302(DS1302_MONTH); //读取月数
temp=temp-1; //月数减1
if(temp==0x0f) temp=0x09;
down_flag=1;
if(temp==0)
temp=0x12;
break;
case 3:
temp=read_all_1302(DS1302_DAY); //读取日数
temp=temp-1; //日数减1
if((temp&0x0f)==0x0f) temp=(((temp>>4)&0x0f))*16+9;
down_flag=1;
if(temp==0)
temp=0x31;
break;
case 4:
temp=read_all_1302(DS1302_WEEK); //读取星期数
temp=temp-1; //星期数减1
//if (temp==0x0f) temp=0x09;
down_flag=1;
if(temp==0)
temp=0x7;
break;
case 5:
temp=read_all_1302(DS1302_HOUR); //读取小时数
temp=temp-1; //小时数减1
if(((temp&0x0f)==0x0f)&&(((temp>>4)&0x0f)>=0&&((temp>>4)&0x0f)<=2)) temp=(((temp>>4)&0x0f)-1)*16+9;
down_flag=1;
if(temp==-1)
temp=0x23;
break;
case 6:
temp=read_all_1302(DS1302_MINUTE); //读取分数
temp=temp-1; //分数减1
if(((temp&0x0f)==0x0f)&&(((temp>>4)&0x0f)>=0&&((temp>>4)&0x0f)<=5)) temp=(((temp>>4)&0x0f))*16+9;
down_flag=1;
if(temp==-1)
temp=0x59; //小于0秒,返回59秒
break;
case 7:
temp=read_all_1302(DS1302_SECOND); //读取秒数
temp=temp-1; //秒数减1
if(((temp&0x0f)==0x0f)&&(((temp>>4)&0x0f)>=0&&((temp>>4)&0x0f)<=5)) temp=(((temp>>4)&0x0f))*16+9;
down_flag=1; //数据调整后更新标志
if(temp==0x7f) //小于0秒,返回59秒
temp=0x59;
break;
default:break;
}
while(Down==0);
}
// flag1=1;
}
/*****************************************************************************************/
void Deal_With_Time()
{
uchar second;
if(CLKSTOP==0) //关闭时钟,停止计时
{ write_all_1302(0x8e,0x00); //写入