51單片機(jī)的ds1302驅(qū)動程序
/*************************************
函數(shù)名:re1302b
函數(shù)功能:實(shí)時(shí)時(shí)鐘讀取一字節(jié)
參數(shù):無
返回:ACC
備注:配合re1302使用
*************************************/
uchar re1302b(void)
{
unsigned char i;
for(i=8; i>0; i--)
{
ACC = ACC >>1; //相當(dāng)于匯編中的 RRC
ACC7 = dssda;
dssck = 1;
dssck = 0;
}
return(ACC);
}
/*************************************
函數(shù)名:re1302
函數(shù)功能:讀數(shù)據(jù)
參數(shù):ucAddr(地址)
返回:ucData(數(shù)據(jù))
備注:無
*************************************/
uchar re1302(uchar ucAddr) //讀取DS1302某地址的數(shù)據(jù)
{
unsigned char ucData;
dsrst = 0;
dssck = 0;
dsrst = 1;
wr1302b(ucAddr|0x01); // 地址,命令
ucData = re1302b(); // 讀1Byte數(shù)據(jù)
dssck = 1;
dsrst = 0;
return(ucData);
}
/*************************************
函數(shù)名:dsinit
函數(shù)功能:時(shí)鐘初始化
參數(shù):無
返回:無
備注:無
*************************************/
void dsinit(void) //時(shí)鐘初始化可以不填
{
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);
}
/*************************************
函數(shù)名:dsgettime
函數(shù)功能:獲取時(shí)間到時(shí)間型指針
參數(shù):systime *ds1302(時(shí)間型數(shù)據(jù)指針)
返回:無
備注:無
*************************************/
void dsgettime(systime *ds1302) //獲取時(shí)鐘芯片的時(shí)鐘數(shù)據(jù)到自定義的結(jié)構(gòu)型數(shù)組
{
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);
}
/*************************************
函數(shù)名:timetostr
函數(shù)功能:將時(shí)間轉(zhuǎn)換液晶顯示字符放入數(shù)組timestr[]
參數(shù):systime *ds1302(時(shí)間型數(shù)據(jù)指針)
返回:無
備注:無
*************************************/
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;
}
else
{
ds1302->timestr[6] = ;
ds1302->timestr[7] = ;
}
ds1302->timestr[8] =