/*------------------------------------------------ DS1302初始化 ------------------------------------------------*/ void Ds1302_Init(UINT8 *Timerbuff) { UINT8 flag; flag= Ds1302_Read_Byte(0x81); if(flag&0x80) //判断时钟芯片是否关闭 { Ds1302_Write_Time(Timerbuff); } }
/*------------------------------------------------ 从DS1302读出时钟数据 ------------------------------------------------*/ void Ds1302_Read_Time(void) { 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); //周 }
/*------------------------------------------------ 从DS1302读出时钟数据 ------------------------------------------------*/ void Ds1302_Read_Time(UINT8 *Timerbuff) { UINT8 i,tmp; UINT8 time_buf[8]; 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; Timerbuff[i]=time_buf[i]%16; Timerbuff[i]=Timerbuff[i]+tmp*10; } }