#include <AT89X52.h>
sbit KEY_SET =P1^5;
sbit KEY_UP =P1^6;
sbit KEY_DOWN =P1^7;
unsigned char key_state=0;
//消除抖动
void key_delay(void)
{
unsigned char i=30;
while(i--);
}
void key_delay500ms(void)
{
unsigned char i=100,j=250;
while(i--)
while(j--);
}
void key(void)
{
char time_temp[7];
if(KEY_SET==0)
{
key_delay();
if(KEY_SET==0)
{
while(KEY_SET==0);
key_state++; //非零状态表示调节年、月、日、周、时、分、秒
if(key_state>7) key_state=0;
}
}
if(key_state!=0)
switch(key_state)
{
case 1:
{
LCD1602Disp1(0x82,'0'+DS12887read(DS12887_Year_Addr)/10);
LCD1602Disp1(0x83,'0'+DS12887read(DS12887_Year_Addr)%10);
key_delay500ms();
LCD1602Disp1(0x82,0);
LCD1602Disp1(0x83,0);
key_delay500ms();
} break;
case 2:
{
LCD1602Disp1(0x85,'0'+DS12887read(DS12887_Month_Addr)/10);
LCD1602Disp1(0x86,'0'+DS12887read(DS12887_Month_Addr)%10);
key_delay500ms();
LCD1602Disp1(0x85,0);
LCD1602Disp1(0x86,0);
key_delay500ms();
} break;
case 3:
{
LCD1602Disp1(0x88,'0'+DS12887read(DS12887_Day_Addr)/10);
LCD1602Disp1(0x89,'0'+DS12887read(DS12887_Day_Addr)%10);
key_delay500ms();
LCD1602Disp1(0x88,0);
LCD1602Disp1(0x89,0);
key_delay500ms();
} break;
case 4:
{
LCD1602Disp1(0x8E,'0'+DS12887read(DS12887_Week_Addr));
key_delay500ms();
LCD1602Disp1(0x8E,0);
key_delay500ms();
} break;
case 5:
{
LCD1602Disp1(0xC0,'0'+DS12887read(DS12887_Hour_Addr)/10);
LCD1602Disp1(0xC1,'0'+DS12887read(DS12887_Hour_Addr)%10);
key_delay500ms();
LCD1602Disp1(0xC0,0);
LCD1602Disp1(0xC1,0);
key_delay500ms();
} break;
case 6:
{
LCD1602Disp1(0xC3,'0'+DS12887read(DS12887_Min_Addr)/10);
LCD1602Disp1(0xC4,'0'+DS12887read(DS12887_Min_Addr)%10);
key_delay500ms();
LCD1602Disp1(0xC3,0);
LCD1602Disp1(0xC4,0);
key_delay500ms();
} break;
case 7:
{
LCD1602Disp1(0xC6,'0'+DS12887read(DS12887_Sec_Addr)/10);
LCD1602Disp1(0xC7,'0'+DS12887read(DS12887_Sec_Addr)%10);
key_delay500ms();
LCD1602Disp1(0xC6,0);
LCD1602Disp1(0xC7,0);
key_delay500ms();
} break;
}
if(key_state!=0)
{
//读出当前时间,然后准备调整,升级建议:可以将年月日读取地址放到数组里,方便循环读写。
time_temp[0]=DS12887read(DS12887_Year_Addr);
time_temp[1]=DS12887read(DS12887_Month_Addr);
time_temp[2]=DS12887read(DS12887_Day_Addr);
time_temp[3]=DS12887read(DS12887_Week_Addr);
time_temp[4]=DS12887read(DS12887_Hour_Addr);
time_temp[5]=DS12887read(DS12887_Min_Addr);
time_temp[6]=DS12887read(DS12887_Sec_Addr);
//增加,减小按键判断
if(KEY_UP==0)
{
key_delay();
if(KEY_UP==0)
{
while(KEY_UP==0);
time_temp[key_state-1]++;
}
}
if(KEY_DOWN==0)
{
key_delay();
if(KEY_DOWN==0)
{
while(KEY_DOWN==0);
time_temp[key_state-1]--;
}
}
/*调整时间范围限定*/
if(time_temp[0]>99) time_temp[0]=0;
if(time_temp[0]<0) time_temp[0]=99;
if(time_temp[1]>12) time_temp[1]=1;
if(time_temp[1]<1) time_temp[1]=12;
//先不考虑不同月份不同天数
if(time_temp[2]>31) time_temp[2]=1;
if(time_temp[2]<1) time_temp[2]=31;
if(time_temp[3]>7) time_temp[3]=1;
if(time_temp[3]<1) time_temp[3]=7;
if(time_temp[4]>23) time_temp[4]=0;
if(time_temp[4]<0) time_temp[4]=23;
if(time_temp[5]>59) time_temp[5]=0;
if(time_temp[5]<0) time_temp[5]=59;
if(time_temp[6]>59) time_temp[6]=0;
if(time_temp[6]<0) time_temp[6]=59;
DS12887write(0x0a,0x00); //开始调时,DS12CR887关闭时钟振荡器
DS12887write(DS12887_Sec_Addr, time_temp[6]);
DS12887write(DS12887_Min_Addr, time_temp[5]);
DS12887write(DS12887_Hour_Addr, time_temp[4]);
DS12887write(DS12887_Week_Addr, time_temp[3]);
DS12887write(DS12887_Day_Addr, time_temp[2]);
DS12887write(DS12887_Month_Addr,time_temp[1]);
DS12887write(DS12887_Year_Addr, time_temp[0]);
/*显示调整后的时间*/
LCD1602Disp1(0x80,'0'+2);
LCD1602Disp1(0x81,'0'+0);
LCD1602Disp1(0x82,'0'+DS12887read(DS12887_Year_Addr)/10);
LCD1602Disp1(0x83,'0'+DS12887read(DS12887_Year_Addr)%10);
LCD1602Disp1(0x84,'-');
LCD1602Disp1(0x85,'0'+DS12887read(DS12887_Month_Addr)/10);
LCD1602Disp1(0x86,'0'+DS12887read(DS12887_Month_Addr)%10);
LCD1602Disp1(0x87,'-');
LCD1602Disp1(0x88,'0'+DS12887read(DS12887_Day_Addr)/10);
LCD1602Disp1(0x89,'0'+DS12887read(DS12887_Day_Addr)%10);
LCD1602Disp1(0x8D,'-');
LCD1602Disp1(0x8E,'0'+DS12887read(DS12887_Week_Addr));
LCD1602Disp1(0x8F,'-');
LCD1602Disp1(0xC0,'0'+DS12887read(DS12887_Hour_Addr)/10);
LCD1602Disp1(0xC1,'0'+DS12887read(DS12887_Hour_Addr)%10);
LCD1602Disp1(0xC2,':');
LCD1602Disp1(0xC3,'0'+DS12887read(DS12887_Min_Addr)/10);
LCD1602Disp1(0xC4,'0'+DS12887read(DS12887_Min_Addr)%10);
LCD1602Disp1(0xC5,':');
LCD1602Disp1(0xC6,'0'+DS12887read(DS12887_Sec_Addr)/10);
LCD1602Disp1(0xC7,'0'+DS12887read(DS12887_Sec_Addr)%10);
}
}