#include "reg52.h"
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
sbit scl=P1^5;
sbit sda=P3^6;
uchar code table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar sec;
uchar wr;
void nop()
{
_nop_();
_nop_();
}
void EEPROM_Init()
{
scl=1;
nop();
sda=1;
nop();
}
void start()
{
scl=1;
nop();
sda=1;
nop();
sda=0;
nop();
scl=0;
nop();
}
void stop()
{
scl=1;
nop();
sda=0;
nop();
sda=1;
nop();
}
void ack()
{
uchar i=0;
scl=1;
nop();
while((sda==1)&&(i<255))
{
i++;
}
scl=0;
nop();
}
uchar readx()
{
uchar i,j,k=0;
for(i=0;i<8;i++)
{
scl=0;
nop();
if(sda==1) j=1;
else j=0;
k=(k<<1)|j;
scl=1;
nop();
}
scl=0;
nop();
return(k);
}
void writex(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
scl=0;
nop();
sda=(bit)(dat&0x80);
nop();
scl=1;
nop();
dat<<=1;
}
scl=0;
nop();
}
void write(uchar add,uchar info)
{
start();
writex(0xa0);
ack();
writex(add);
ack();
writex(info);
ack();
stop();
}
uchar read(uchar add)
{
uchar i;
start();
writex(0xa0);
ack();
writex(add);
ack();
start();
writex(0xa1);
ack();
i=readx();
stop();
return(i);
}
void Delay_1ms(uchar i)
{
uchar x, j;
for(j=0; j<i; j++)
for(x=0; x<=110; x++);
}
void LED() //LED显示函数
{
P2 = 6;
P0 = table[sec / 10];
Delay_1ms(5);
P2 = 7;
P0 = table[sec % 10];
Delay_1ms(5);
}
void time0() interrupt 1 using 3
{
static uchar count=0;
TH0=0x4c;
TL0=0x00;
count++;
if(count==20)
{
count=0;
sec++;
wr=1;
if(sec==60) sec=0;
}
}
void Time0_Init(void)
{
TMOD = 0x01; //定时器工作在方式1
ET0 = 1;
EA = 1;
TH0 = 0x4c; //对TH0 TL0赋值
TL0 = 0x00; //使定时器0.05秒中断一次
TR0 = 1; //开始计时
}
void Main()
{
EEPROM_Init();
sec=read(0);
LED();
Time0_Init();
while(1)
{
LED();
if(wr==1)
{
wr=0;
write(0,sec);
}
}
}