#ifndef SDA
#define SDA P0-0
#define SCL P0-1
#endif
extern uchar SystemError;
#define uchar unsigned char
#define uint unsigned int
#define Byte unsigned char
#define Word unsigned int
#define bool bit
#define true 1
#define false 0
#define SomeNOP(); -nop-();-nop-();-nop-();-nop-();
void I2CStart(void)
{
EA=0;
SDA=1; SCL=1; SomeNOP();
SDA=0; SomeNOP();
SCL=0;
}
void I2CStop(void)
{
SCL=0; SDA=0; SomeNOP();
SCL=1; SomeNOP(); SDA=1;
EA=1;
}
bool WaitAck(void)
{
uchar errtime=255;
SDA=1;SomeNOP();
SCL=1;SomeNOP();
while(SDA) {errtime--; if (!errtime) {I2CStop();SystemError=0x11;return false;}}
SCL=0;
return true;
}
void SendAck(void)
{
SDA=0; SomeNOP();
SCL=1; SomeNOP();
SCL=0;
}
void SendNotAck(void)
{
SDA=1; SomeNOP();
SCL=1; SomeNOP();
SCL=0;
}
void I2CSendByte(Byte ch)
{
uchar i=8;
while (i--)
{
SCL=0;-nop-();
SDA=(bit)(ch&0x80); ch<<=1; SomeNOP();
SCL=1; SomeNOP();
}
SCL=0;
}
Byte I2CReceiveByte(void)
{
uchar i=8;
Byte ddata=0;
SDA=1;
while (i--)
{
ddata<<=1;
SCL=0;SomeNOP();
SCL=1;SomeNOP();
ddata|=SDA;
}
SCL=0;
return ddata;
}
void GetPCF8563(uchar firsttype,uchar count,uchar *buff)
{
uchar i;
I2CStart();
I2CSendByte(0xA2);
WaitAck();
I2CSendByte(firsttype);
WaitAck();
I2CStart();
I2CSendByte(0xA3);
WaitAck();
for (i=0;i<count;i++)
{
buff[i]=I2CReceiveByte();
if (i!=count-1) SendAck();
}
SendNotAck();
I2CStop();
}
void SetPCF8563(uchar timetype,uchar value)
{
I2CStart();
I2CSendByte(0xA2);
WaitAck();
I2CSendByte(timetype);
WaitAck();
I2CSendByte(value);
WaitAck();
I2CStop();
}
void SetAlarm(uchar alarmtype,uchar count)
{
SetPCF8563(0x01,0x02);
SetPCF8563(alarmtype,count);
}
void CleanAlarm(void)
{
SetPCF8563(0x01,0x00);
SetPCF8563(0x09,0x80);
SetPCF8563(0x0A,0x80);
SetPCF8563(0x0B,0x80);
SetPCF8563(0x0C,0x80);
}
uchar read1380 (uchar command)
{
uchar time;
GetPCF8563(command,1,&time);
return time;
}
void write1380(uchar command ,uchar time)
{
SetPCF8563(command,time);
}
void time-display(uchar x0,uchar y0,bit type)
{
uchar time[]="00:00:00";
uchar con[3];
uchar time-type;
GetPCF8563(0x02,3,con);
time[0]=(con[2]>>4)+'0';
time[1]=(con[2]&0x0f)+'0';
time[3]=(con[1]>>4)+'0';
time[4]=(con[1]&0x0f)+'0';
time[6]=(con[0]>>4)+'0';
time[7]=(con[0]&0x0f)+'0';
time[8]=0;
if(type==1)
{
time-type=0xff;
}
else
{
time-type=0;
}
dipchar0(x0,y0,F57,1,time-type,time);
}