void Ds1302_Write_Time(void)
{
unsigned char i,tmp;
for(i=0;i<8;i++)
{ //BCD处理
tmp=time_buf1[i]/10;
time_buf[i]=time_buf1[i]%10;
time_buf[i]=time_buf[i]+tmp*16;
}
Ds1302_Write_Byte(ds1302_control_add,0x00); //关闭写保护
Ds1302_Write_Byte(ds1302_sec_add,0x80); //暂停
//Ds1302_Write_Byte(ds1302_charger_add,0xa9); //涓流充电
Ds1302_Write_Byte(ds1302_year_add,time_buf[1]); //年
Ds1302_Write_Byte(ds1302_month_add,time_buf[2]); //月
Ds1302_Write_Byte(ds1302_date_add,time_buf[3]); //日
Ds1302_Write_Byte(ds1302_day_add,time_buf[7]); //周
Ds1302_Write_Byte(ds1302_hr_add,time_buf[4]); //时
Ds1302_Write_Byte(ds1302_min_add,time_buf[5]); //分
Ds1302_Write_Byte(ds1302_sec_add,time_buf[6]); //秒
Ds1302_Write_Byte(ds1302_day_add,time_buf[7]); //周
Ds1302_Write_Byte(ds1302_control_add,0x80); //打开写保护
}
/*------------------------------------------------
从DS1302读出时钟数据
------------------------------------------------*/
void Ds1302_Read_Time(void)
{
unsigned char i,tmp;
time_buf[1]=Ds1302_Read_Byte(ds1302_year_add); //年
time_buf[2]=Ds1302_Read_Byte(ds1302_month_add); //月
time_buf[3]=Ds1302_Read_Byte(ds1302_date_add); //日
time_buf[4]=Ds1302_Read_Byte(ds1302_hr_add); //时
time_buf[5]=Ds1302_Read_Byte(ds1302_min_add); //分
time_buf[6]=(Ds1302_Read_Byte(ds1302_sec_add))&0x7F;//秒
time_buf[7]=Ds1302_Read_Byte(ds1302_day_add); //周
for(i=0;i<8;i++)
{ //BCD处理
tmp=time_buf[i]/16;
time_buf1[i]=time_buf[i]%16;
time_buf1[i]=time_buf1[i]+tmp*10;
}
} |