当前位置:文档之家› PCF8563驱动程序(C语言版)

PCF8563驱动程序(C语言版)

#include 〈reg52.h>
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar TAB_T[]={ 0x31,0x06,0x10,//秒,分,时
0x13,0x01,0x07,0x08};//日,星期,月,年.
sbit SDA=P2^4; //数据
sbit SCL=P2^3; //时钟
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; //准备检测SDA Delay5us();
while((SDA==1)&&(i<100))i++;//SDA=0为应该信号,SDA=1为非应答
SCL=0; //准备下一变化数据
}
void WriteByte(uchar ucByte)
{
uchar i;
SCL=0;
for(i=0;i<8;i++)
{
SDA=(bit)(ucByte&0x80);//先写入高位
SCL=1;
Delay5us();
SCL=0;
ucByte<<=1;
}
SDA=1; //释放数据线
}
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); //写器件地址
Reack();
WriteByte(ucAddr); //写字节地址
Reack();
Start();
WriteByte(0xa3); //写器件地址,最低为1表示读
Reack();
ucData=ReadByte(); //写字节地址
Stop();
return ucData; //读数据
}
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++)
{
pTime[i]=Read8563(ucAddr);
ucAddr++;
}
pTime[0]&=0x7f; //屏蔽无效位
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码转十六进制
ucData2=pTime[i]%16;
pTime[i]=ucData1*10+ucData2;
}
}
 

相关主题
文本预览
相关文档 最新文档