给你段例程,仅供参考
sbit scl=0x96;
sbit sda=0x97;
sbit a0=0xe0; //定义ACC
sbit a1=0xe1;
sbit a2=0xe2;
sbit a3=0xe3;
sbit a4=0xe4;
sbit a5=0xe5;
sbit a6=0xe6;
sbit a7=0xe7;
sbit b0=0xf0; //定义B
sbit b1=0xf1;
sbit b2=0xf2;
sbit b3=0xf3;
sbit b4=0xf4;
sbit b5=0xf5;
sbit b6=0xf6;
sbit b7=0xf7;
struct time
{
unsigned char year,month,week,day,hour,min,sec;
} today;
void s24(void)
{
sda=1;_nop_();
scl=1;_nop_();
sda=0;_nop_();
scl=0;
}
void p24(void)
{
sda=0;_nop_();
scl=1;_nop_();
sda=1;_nop_();
}
void ask24(void)
{
sda=0;_nop_();
scl=1;_nop_();
scl=0;_nop_();
}
unsigned char rd24(void)
{
sda=1;scl=0;
scl=1;a7=sda;scl=0;
scl=1;a6=sda;scl=0;
scl=1;a5=sda;scl=0;
scl=1;a4=sda;scl=0;
scl=1;a3=sda;scl=0;
scl=1;a2=sda;scl=0;
scl=1;a1=sda;scl=0;
scl=1;a0=sda;scl=0;
return(ACC);
}
void wd24(unsigned char dd)
{
B=dd;
sda=b7;scl=1;scl=0;
sda=b6;scl=1;scl=0;
sda=b5;scl=1;scl=0;
sda=b4;scl=1;scl=0;
sda=b3;scl=1;scl=0;
sda=b2;scl=1;scl=0;
sda=b1;scl=1;scl=0;
sda=b0;scl=1;scl=0;
sda=1; scl=1;
while(sda);
scl=0;
}
unsigned char rdone(unsigned char add) //从add读1个数据
{
unsigned char dat;
s24();
wd24(0xa0); //从机地址
wd24(add);
s24();
wd24(0xa1);
dat=rd24();
p24();
return(dat);
}
void wrone(unsigned char add,unsigned char dat)
{
s24();
wd24(0xa0);
wd24(add);
wd24(dat);
p24();
soft_20ms();
}
void ReadClock(void)
{
unsigned char temp[5];
unsigned char i;
s24();
wd24(0xa0);
wd24(0x02);
s24();
wd24(0xa1);
for(i=0;i <4;i++)
{
temp[i]=rd24();
ask24();
}
temp[4]=rd24();
p24();
today.sec=((temp[0]&0xf0)> > 4)*10+(temp[0]&0x0f);
today.min=((temp[1]&0xf0)> > 4)*10+(temp[1]&0x0f);
today.hour=((temp[2]&0x30)> > 4)*10+(temp[2]&0x0f);
today.day=((temp[3]&0x30)> > 4)*10+(temp[3]&0x0f);
today.week=(temp[4]&0xe0)> > 5;
today.month=((temp[4]&0x10)> > 4)*10+(temp[4]&0x0f);
// today.year=(temp[3]&0xc0)> > 6;
today.year=rdone(0x0f);
}
void SetClock(void)
{
char temp[5];
temp[0]=(today.sec/10 < <4)+today.sec%10;
temp[1]=(today.min/10 < <4)+today.min%10;
temp[2]=(today.hour/10 < <4)+today.hour%10;
temp[3]=(today.day/10 < <4)+today.day%10+((today.year%3) < <6);
temp[4]=(today.month/10 < <4)+today.month%10+(today.week < <5);
wrone(0x0f,today.year);
s24();
wd24(0xa0);
wd24(02);
wd24(temp[0]);
wd24(temp[1]);
wd24(temp[2]);
wd24(temp[3]);
wd24(temp[4]);
p24();
soft_20ms();
}
void main(void)
{
.........
today.sec=0;
today.min=0;
today.hour=0;
today.day=0;
today.week=0;
today.month=0;
today.year=0;
wrone(0x00,0x00);
wrone(0x08,0x90);
............
}
发表时间:2003年4月28日9:57:27