PCF8563驅(qū)動(dòng)(C語(yǔ)言版)
#include 〈reg52.h>
本文引用地址:http://m.butianyuan.cn/article/201611/319370.htm#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar TAB_T[]={ 0x31,0x06,0x10,//秒,分,時(shí)
0x13,0x01,0x07,0x08};//日,星期,月,年.
sbit SDA=P2^6; //數(shù)據(jù)
sbit SCL=P2^7; //時(shí)鐘
void Delay5us(void)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
void Start(void)
{
SDA=1;
SCL=1;
Delay5us();
SDA=0;
Delay5us();
}
void Stop(void)
{
SDA=0;
SCL=1;
Delay5us();
SDA=1;
Delay5us();
void Reack(void)
{
uchar i=0;
SCL=1; //準(zhǔn)備檢測(cè)SDA Delay5us();
while((SDA==1)&&(i<100))i++;//SDA=0為應(yīng)該信號(hào),SDA=1為非應(yīng)答
SCL=0; //準(zhǔn)備下一變化數(shù)據(jù)
}
void WriteByte(uchar ucByte)
{
uchar i;
SCL=0;
for(i=0;i<8;i++)
{
SDA=(bit)(ucByte&0x80);//先寫(xiě)入高位
SCL=1;
Delay5us();
SCL=0;
ucByte<<=1;
}
SDA=1; //釋放數(shù)據(jù)線
}
uchar ReadByte(void)
{
uchar i,ucByte;
SCL=0;
for(i=0;i<8;i++)
{
ucByte<<=1;
if(SDA)
ucByte++;
SCL=1;
Delay5us();
SCL=0;
}
return ucByte;
}
void Write8563(uchar ucAddr,uchar ucData)
{
Start();
WriteByte(0xa2);
Reack();
WriteByte(ucAddr);
Reack();
WriteByte(ucData);
Reack();
Stop();
}
uchar Read8563(uchar ucAddr)
{
uchar ucData;
Start();
WriteByte(0xa2); //寫(xiě)器件地址
Reack();
WriteByte(ucAddr); //寫(xiě)字節(jié)地址
Reack();
Start();
WriteByte(0xa3); //寫(xiě)器件地址,最低為1表示讀
Reack();
ucData=ReadByte(); //寫(xiě)字節(jié)地址
Stop();
return ucData; //讀數(shù)據(jù)
}
void Init8563(void)
{
uchar i,ucAddr=0x02;
Write8563(0x00,0x00);
Write8563(0x01,0x11);
for(i=0;i<7;i++)
{
Write8563(ucAddr,TAB_T[i]);
ucAddr++;
}
}
void GetTime(void)
{
uchar i,ucData1,ucData2,ucAddr=0x02;
uchar *pTime=TAB_T;
for(i=0;i<7;i++)
{
ucAddr++;
}
pTime[0]&=0x7f; //屏蔽無(wú)效位
pTime[1]&=0x7f;
pTime[2]&=0x3f;
pTime[3]&=0x3f;
pTime[4]&=0x07;
pTime[5]&=0x1f;
for(i=0;i<7;i++)
{
ucData1=pTime[i]/16; //BCD碼轉(zhuǎn)十六進(jìn)制
ucData2=pTime[i]%16;
pTime[i]=ucData1*10+ucData2;
}
}
評(píng)論