//读出时间 void DS1302_GetTime(SYSTEMTIME *Time){ unchar ReadValue; ReadValue = Read1302(DS1302_SECOND); Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f); ReadValue = Read1302(DS1302_MINUTE); Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f); ReadValue = Read1302(DS1302_HOUR); Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f); }
void InitClock(void) { uint8_t tmp; uint8_t flag; flag=Read1302(0x81); if(flag&0x80) { //判断时钟芯片是否关闭 Write1302(0x8E,0x00); //根据写状态寄存器命令字,写入不保护指令 Write1302(0x80,((51/10)<<4|(51%10))); //根据写秒寄存器命令字,写入秒的初始值 Write1302(0x82,((2/10)<<4|(2%10))); //根据写分寄存器命令字,写入分的初始值 Write1302(0x84,((12/10)<<4|(12%10))); //根据写小时寄存器命令字,写入小时的初始值 Write1302(0x86,((27/10)<<4|(27%10))); //根据写日寄存器命令字,写入日的初始值 Write1302(0x88,((7/10)<<4|(7%10))); //根据写月寄存器命令字,写入月的初始值 Write1302(0x8c,((15/10)<<4|(15%10))); //根据写年寄存器命令字,写入年的初始值 Write1302(0x8E,0x80); //根据写状态寄存器命令字,写入保护指令 } tmp = ReadDS1302Ram(0); if(tmp^0xa5) { WriteDS1302Ram(0,0xa5); Write1302(DS1302_WRITE,0x00); //关闭写保护 Write1302(0x90,0x03); //禁止涓流充电 Write1302(DS1302_HOUR,0x00); //设置成24小时制 Write1302(DS1302_SECOND,0x00); //使能时钟运行 Write1302(DS1302_WRITE,0x80); //打开写保护 } }
//初始化DS1302 void DS1302_Initial (void) { unchar Second=Read1302(DS1302_SECOND); if(Second&0x80)//bit7=CH CH=0 振荡器允许工作,CH=1振荡器停止工作 DS1302_SetTime(DS1302_SECOND,0); }
/* 读取DS1302中的RAM addr:地址,从0到30,共31个字节的空间 返回为所读取的数据 */ uint8_t ReadDS1302Ram(uint8_t addr) { uint8_t tmp,res; tmp = (addr<<1)|0xc0; res = Read1302(tmp); return(res); }
/* 读取DS1302中的RAM addr:地址,从0到30,共31个字节的空间 返回为所读取的数据 */ u8 ReadDS1302Ram(u8 addr) { u8 tmp,res; tmp = (addr<<1)|0xc0; res = Read1302(tmp); return(res); }
/***************************************************** 函数功能:根据命令字,从1302读取一个字节数据 入口参数:Cmd ***************************************************/ uchar ReadSet1302(uchar Cmd) { uchar dat; RST=0; //拉低RST SCLK=0; //确保写数居前SCLK被拉低 RST=1; //启动数据传输 Write1302(Cmd); //写入命令字 dat=Read1302(); //读出数据 SCLK=1; //将时钟电平置于已知状态 RST=0; //禁止数据传递 return dat; //将读出的数据返回 }
//读取时间函数 void DS1302_GetTime(uint8_t *time) { time[0] = Read1302(DS1302_YEAR); time[1] = Read1302(DS1302_WEEK); time[2] = Read1302(DS1302_MONTH); time[3] = Read1302(DS1302_DAY); time[4] = Read1302(DS1302_HOUR); time[5] = Read1302(DS1302_MINUTE); time[6] = Read1302(DS1302_SECOND); }
/***************************************************** 函数功能:根据命令字,从1302读取一个字节数据 入口参数:Cmd ***************************************************/ UINT8 Ds1302_Read_Byte(UINT8 Cmd) { UINT8 ucData; DS1302_RST = 0; DS1302_CLK = 0; DS1302_RST = 1; //enable Write1302(Cmd|0x01); // 地址,命令 ucData = Read1302(); // 读1Byte数据 DS1302_CLK = 1; //RST 0->1->0,CLK 0->1 DS1302_RST = 0; return(ucData); }
//读出时间 void DS1302_GetTime(SYSTEMTIME *Time) { unchar ReadValue; ReadValue = Read1302(DS1302_SECOND); Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f); ReadValue = Read1302(DS1302_MINUTE); Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f); ReadValue = Read1302(DS1302_HOUR); Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f); ReadValue = Read1302(DS1302_DAY); Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f); ReadValue = Read1302(DS1302_WEEK); Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f); ReadValue = Read1302(DS1302_MONTH); Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f); ReadValue = Read1302(DS1302_YEAR); Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f); }