DS1302_WriteData(0x8E,0x80); //写入控制命令,启用写保护
}
//读取DS1302时间到ReadingData中
void DS1302_GetData(unsigned char *str)
{
unsigned char temp;
unsigned char addr = 0x8D;
for(temp=0;temp<7;temp++)
{
str[temp] = DS1302_ReadData(addr);//年
addr -= 2;
}
}
//****************************显示时间子程序
unsigned char Show_time(void)
{
unsigned int p=0,q=1;
unsigned char Data[12];
unsigned char Time[9];
Data[0]=(ReadingData[0]>>4)/1+'0'; //年
Data[1]=(ReadingData[0]&0x0f)/1+'0';
Data[2]='/';
Data[3]=(ReadingData[2]>>4)/1+'0'; //月
Data[4]=(ReadingData[2]&0x0f)/1+'0';
Data[5]='/';
Data[6]=(ReadingData[3]>>4)/1+'0'; // 日
Data[7]=(ReadingData[3]&0x0f)/1+'0';
Data[8]='/';
Data[9]=(ReadingData[1]>>4)/1+'0'; // 星期
Data[10]=(ReadingData[1]&0x0f)/1+'0';
Data[11]='\0';
Time[0]=((ReadingData[4]>>4))/1+'0';// 时
Time[1]=(ReadingData[4]&0x0f)/1+'0';
Time[2]=':';
Time[3]=(ReadingData[5]>>4)/1+'0'; //分
Time[4]=(ReadingData[5]&0x0f)/1+'0';
Time[5]=':';
Time[6]=(ReadingData[6]>>4)/1+'0';// 秒
Time[7]=(ReadingData[6]&0x0f)/1+'0';
Time[8]='\0';
if((Setting_Time[4]>0x04)&&(p==0))
{
if((Time[0]=='0')&&(Time[1]=='4')&&(Time[3]=='0')&&(Time[4]=='0')&&(Time[6]=='0')&&(Time[7]=='0'))
{
DS1302_WriteData(0x8E,0x00); //写入控制命令,禁用写保护
Setting_Time[4]=0x00;
DS1302_WriteData(0x84,Setting_Time[4]);
DS1302_WriteData(0x8E,0x80); //写入控制命令,启用写保护
}
}
if((Setting_Time[4]<=0x04)&&(q==0))
{
if((Time[0]=='0')&&(Time[1]=='4')&&(Time[3]=='0')&&(Time[4]=='0')&&(Time[6]=='0')&&(Time[7]=='0'))
{
DS1302_WriteData(0x8E,0x00); //写入控制命令,禁用写保护
Setting_Time[4]=0x00;
DS1302_WriteData(0x84,Setting_Time[4]);
DS1302_WriteData(0x8E,0x80); //写入控制命令,启用写保护
}
}
if(ReadingData[4]>0x04)
{ p=0;q=0;}
else {p=1;q=1;}
DispStr(2,0,Data);
DispStr(4,1,Time);
_NOP();
return 0;
}
//===========================时间调整=======================================
void Time_tz(void)
{
DS1302_WriteData(0x8E,0x00); //1302写入控制命令,禁用写保护
DS1302_WriteData(0x80,0x80);
DS1302_WriteData(0x8E,0x80); //1302写入控制命令,启用写保护
flag=0;
for(;;)
{
unsigned int zsm;
zsm=(~P2IN)&0X0f;
if(flag==3) flag=0;
if(zsm==0x01) flag++; //选择调整小时还是分钟
key_flag=(~P2IN)&0x0f; //选择加调整还是减调整
//****************************************************小时调整
if(flag==1)
{
LocateXY(5,1);
LcdWriteCommand(0x0e,1);
if(key_flag==0x02) //加
{
if(Setting_Time[4]>=0x23) Setting_Time[4]=0x00;
else if((Setting_Time[4]&0x0F)==0x09) Setting_Time[4]=(Setting_Time[4]&0xf0)+0x10;
else Setting_Time[4]+=1;
}
if(key_flag==0x04) //减
{
if(Setting_Time[4]<=0x00) Setting_Time[4]=0x23;
else if((Setting_Time[4]&0x0F)==0x00) Setting_Time[4]=(Setting_Time[4]&0xf0)-0x10+0x09;
else Setting_Time[4]-=1;
}
LocateXY(5,1);
LcdWriteCommand(0x0e,1);
DS1302_WriteData(0x8E,0x00); //1302写入控制命令,禁用写保护
DS1302_WriteData(0x84,Setting_Time[4]); //写入1302修改值
DS1302_WriteData(0x8E,0x80); //1302写入控制命令,启用写保护
LocateXY(5,1);
LcdWriteCommand(0x0e,1);
DS1302_GetData(ReadingData);
Show_time();
LocateXY(5,1);
LcdWriteCommand(0x0e,1);
}
//*************************************************************分钟调整
if(flag==2)
{
LocateXY(8,1);
LcdWriteCommand(0x0e,1);
if(key_flag==0x02) //加
{
if(Setting_Time[5]>=0x59) Setting_Time[5]=0x00;
else if((Setting_Time[5]&0x0F)==0x09) Setting_Time[5]=(Setting_Time[5]&0xf0)+0x10;
else Setting_Time[5]+=1;
}
if(key_flag==0x04) //减
{
if(Setting_Time[5]<=0x00) Setting_Time[5]=0x59;
else if((Setting_Time[5]&0x0F)==0x00) Setting_Time[5]=(Setting_Time[5]&0xf0)-0x10+0x09;
else Setting_Time[5]-=1;
}
LocateXY(8,1);
LcdWriteCommand(0x0e,1);
DS1302_WriteData(0x8E,0x00); //1302写入控制命令,禁用写保护
DS1302_WriteData(0x82,Setting_Time[5]); //写入1302修改值
DS1302_WriteData(0x8E,0x80); //1302写入控制命令,启用写保护
LocateXY(8,1);
LcdWriteCommand(0x0e,1);
DS1302_GetData(ReadingData);
Show_time();
LocateXY(8,1);
LcdWriteCommand(0x0e,1);
}
if(key_flag==0x08) {flag=0;key_flag=0;break;}
//*******************************************************
}
DS1302_WriteData(0x8E,0x00); //1302写入控制命令,禁用写保护
DS1302_WriteData(0x80,0x00);
DS1302_WriteData(0x8E,0x80); //1302写入控制命令,启用写保护
LcdWriteCommand(0x0c,1);
}
//****************************
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
LcdReset();
DS1302_Reset();
DS1302_SettingData();
while(1)
{
P2DIR|=0x00;
unsigned int sm=0;
sm=(~P2IN)&0X0f;
if(sm==0x01)
{
flag=1;
Time_tz(); //调整程序
DS1302_WriteData(0x8E,0x00); //1302写入控制命令,禁用写保护
DS1302_WriteData(0x80,0x00);
DS1302_WriteData(0x8E,0x80); //1302写入控制命令,启用写保护
}
DS1302_GetData(ReadingData);
Show_time(); //调用显示程序
}
//****************************/
} |