51單片機的ds1302驅動程序
/*************************************
函數名:re1302b
函數功能:實時時鐘讀取一字節
參數:無
返回:ACC
備注:配合re1302使用
*************************************/
uchar re1302b(void)
{
unsigned char i;
for(i=8; i>0; i--)
{
ACC = ACC >>1; //相當于匯編中的 RRC
ACC7 = dssda;
dssck = 1;
dssck = 0;
}
return(ACC);
}
/*************************************
函數名:re1302
函數功能:讀數據
參數:ucAddr(地址)
返回:ucData(數據)
備注:無
*************************************/
uchar re1302(uchar ucAddr) //讀取DS1302某地址的數據
{
unsigned char ucData;
dsrst = 0;
dssck = 0;
dsrst = 1;
wr1302b(ucAddr|0x01); // 地址,命令
ucData = re1302b(); // 讀1Byte數據
dssck = 1;
dsrst = 0;
return(ucData);
}
/*************************************
函數名:dsinit
函數功能:時鐘初始化
參數:無
返回:無
備注:無
*************************************/
void dsinit(void) //時鐘初始化可以不填
{
wr1302(dsbaohu,off);
wr1302(dsyear,0x09);
wr1302(dsmonth,0x11);
wr1302(dsday,0x02);
wr1302(dsweek,0x01);
wr1302(dshour,0x23);
wr1302(dsminute,0x57);
wr1302(dssecond,0x55);
wr1302(dsbaohu,on);
}
/*************************************
函數名:dsgettime
函數功能:獲取時間到時間型指針
參數:systime *ds1302(時間型數據指針)
返回:無
備注:無
*************************************/
void dsgettime(systime *ds1302) //獲取時鐘芯片的時鐘數據到自定義的結構型數組
{
unsigned char ReadValue;
ReadValue = re1302(dssecond);
ds1302->Second = bcdtohex(ReadValue);
ReadValue = re1302(dsminute);
ds1302->Minute = bcdtohex(ReadValue);
ReadValue = re1302(dshour);
ds1302->Hour = bcdtohex(ReadValue);
ReadValue = re1302(dsday);
ds1302->Day = bcdtohex(ReadValue);
ReadValue = re1302(dsweek);
ds1302->Week = bcdtohex(ReadValue);
ReadValue = re1302(dsmonth);
ds1302->Month =bcdtohex(ReadValue);
ReadValue = re1302(dsyear);
ds1302->Year = bcdtohex(ReadValue);
}
/*************************************
函數名:timetostr
函數功能:將時間轉換液晶顯示字符放入數組timestr[]
參數:systime *ds1302(時間型數據指針)
返回:無
備注:無
*************************************/
void timetostr(systime *ds1302)
{ if(flaghour<2)
{
ds1302->timestr[0] = ds1302->Hour/10 + 0;
ds1302->timestr[1] = ds1302->Hour%10 + 0;
}
else
{
ds1302->timestr[0] = ;
ds1302->timestr[1] = ;
}
ds1302->timestr[2] = :;
if(flagmin<2)
{
ds1302->timestr[3] = ds1302->Minute/10 + 0;
ds1302->timestr[4] = ds1302->Minute%10 + 0;
}
else
{
ds1302->timestr[3] = ;
ds1302->timestr[4] = ;
}
ds1302->timestr[5] = :;
if(flagsec<2)
{
ds1302->timestr[6] = ds1302->Second/10 + 0;
ds1302->timestr[7] = ds1302->Second%10 + 0;