#include"include.h"
bit write=0;
uchar sec;
uchar segment1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void IIC_Init(void)
{
IIC_SDA=1;
IIC_SCL=1;
}
void IIC_Start(void)
{
IIC_SDA=1;
Delay();
IIC_SCL=1;
Delay();
IIC_SDA=0;
Delay();
}
void IIC_Stop(void)
{
IIC_SDA=0;
Delay();
IIC_SCL=1;
Delay();
IIC_SDA=1;
Delay();
}
void IIC_Response(void)
{
uchar i;
IIC_SCL=1;
Delay();
while((IIC_SDA==1)&&(i<250))
i++;
IIC_SCL=0;
Delay();
}
void IIC_WriteOneByte(uchar Data)
{
uchar i,temp;
temp=Data;
for(i=0;i<8;i++)
{
temp=temp<<1;
IIC_SCL=0;
Delay();
IIC_SDA=CY;
Delay();
IIC_SCL=1;
Delay();
}
IIC_SCL=0;
Delay();
IIC_SDA=1;
Delay();
}
uchar IIC_ReadOneByte(void)
{
uchar i,k;
IIC_SCL=0;
Delay();
IIC_SDA=1;
Delay();
for(i=0;i<8;i++)
{
IIC_SCL=1;
Delay();
k=(k<<1)|IIC_SDA;
IIC_SCL=0;
Delay();
}
return k;
}
void IIC_WriteOneByteOnAddr(uchar Addr,uchar Data )
{
IIC_Start();
IIC_WriteOneByte(0xa0);
IIC_Response();
IIC_WriteOneByte(Addr);
IIC_Response();
IIC_WriteOneByte(Data);
IIC_Response();
IIC_Stop();
}
uchar IIC_ReadOneByteOnAddr(uchar Addr)
{
uchar Data;
IIC_Start();
IIC_WriteOneByte(0xa0);
IIC_Response();
IIC_WriteOneByte(Addr);
IIC_Response();
IIC_Start();
IIC_WriteOneByte(0xa1);
IIC_Response();
Data=IIC_ReadOneByte();
IIC_Stop();
return Data;
}
void IIC_Func(void)
{
IIC_Init();
sec=IIC_ReadOneByteOnAddr(2);
if(sec>100)
sec=0;
TMOD=0x01;
ET0=1;
EA=1;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
TR0=1;
while(1)
{
Segment1_Display(sec/10, sec%10);
if(write=1)
{
write=0;
IIC_WriteOneByteOnAddr(2, sec);
}
}
}
void Segment1_Display(uchar bai_c,uchar shi_c)
{
dula=0;
P0=segment1[bai_c];
dula=1;
dula=0;
wela=0;
P0=0x7e;
wela=1;
wela=0;
Delay_MS(5);
dula=0;
P0=segment1[shi_c];
dula=1;
dula=0;
wela=0;
P0=0x7d;
wela=1;
wela=0;
Delay_MS(5);
}