PCF8563的SCL为时钟输入端,数据随时钟信号同步输入器件或从器件输出;SDA为双向引脚,用于串行数据的输入输出;INT是中断信号输出端,可通过设置报警寄存器按指定时间在该脚产生报警信号,低电平有效;SDA、SCL、INT均为漏极开路,必须上拉电阻;X1、X2分别为反相放大器的输入、输出端;可在X1端接入32.768kHz的石英晶振,配置成片内振荡器。
PCF8563的从地址:读地址SLAR为A3H、写地址SLAW为A2H,PCF8563I2C通信实现有字节写/读两种状态。;屏蔽无效位 MOV MIN,A ;送秒寄存器 MOV A,41H ;取分字节 ANL A,#7EH ;屏蔽无效位
#i nclude <reg52.h> #i nclude <intrins.h> #define uchar unsigned char sbit SDA=P3^4; sbit SCL=P3^5; sbit P10=P1^0; uchar g8563_Store[4]; uchar code c8563_Store[4]={0x00,0x59,0x07,0x01};
void Delay() { // {P10=1;_nop_();P10=0;}
_nop_(); _nop_(); }
void Start() { EA=0; SDA=1; SCL=1; Delay(); SDA=0; Delay(); SCL=0; }
void Stop() { SDA=0; SCL=0; Delay(); SCL=1; Delay(); SDA=1; Delay(); EA=1; }
void WriteACK(uchar ack) { SDA=ack; Delay(); SCL=1; Delay(); SCL=0; }
void WaitACK() { uchar errtime=20; SDA=1; Delay(); SCL=1; Delay(); while(SDA) { errtime--; if(!errtime) Stop(); } SCL=0; Delay(); }
void writebyte(uchar wdata) { uchar i; for(i=0;i<8;i++) { if(wdata&0x80) SDA=1; else SDA=0; wdata<<=1; SCL=1; Delay(); SCL=0; } WaitACK(); //I2C器件或通讯出错,将会退出I2C通讯
}
uchar Readbyte() { uchar i,bytedata; SDA=1; for(i=0;i<8;i++) { SCL=1; bytedata<<=1; bytedata|=SDA; SCL=0; Delay(); } return(bytedata); }
void writeData(uchar address,uchar mdata) { Start(); writebyte(0xa2); writebyte(address); writebyte(mdata); Stop(); }
uchar ReadData(uchar address) { uchar rdata; Start(); writebyte(0xa2); writebyte(address); Start(); writebyte(0xa3); rdata=Readbyte(); WriteACK(1); Stop(); return(rdata); } void ReadData1(uchar address,uchar count,uchar * buff) { uchar i; Start(); writebyte(0xa2); writebyte(address); Start(); writebyte(0xa3); for(i=0;i<count;i++) { buff[i]=Readbyte(); if(i<count-1) WriteACK(0); } WriteACK(1); Stop(); }
void P8563_Read() { uchar time[7]; ReadData1(0x02,0x07,time); g8563_Store[0]=time[0]&0x7f; g8563_Store[1]=time[1]&0x7f; g8563_Store[2]=time[2]&0x3f; g8563_Store[3]=time[4]&0x07; }
void P8563_gettime() { P8563_Read(); if(g8563_Store[0]==0) P8563_Read(); }
void P8563_settime() { uchar i; for(i=2;i<=4;i++) { writeData(i,g8563_Store[i-2]); } writeData(6,g8563_Store[3]); }
void P8563_init() { uchar i; if((ReadData(0xa)&0x3f)!=0x8) { for(i=0;i<=3;i++) g8563_Store[i]=c8563_Store[i]; P8563_settime(); writeData(0x0,0x00); writeData(0xa,0x8); writeData(0x1,0x12); writeData(0xd,0xf0); } }
|
阅读(1562) | 评论(0) | 转发(2) |