出错的是这个段代码void read_byte(uchar date)
{
uchar i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i--)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
delay();
return(k);
}
这个是全部代码#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
bit write=0;
sbit sda=P2^0;
sbit scl=P2^1;
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,
0x71};
void delay()
{;;}
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void start() //启动信号
{
sda=1;
delay();
scl=1;
delay();
scl=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons() //应答信号
{
uchar i;
scl=1;
delay();
while(sda=1&&(i<250))
i++;
scl=0;
delay();
}
void init()
{
sda=1;
delay();
scl=1;
delay();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i--)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
}
scl=0;
delay();
sda=1;
delay();
}
void read_byte(uchar date)
{
uchar i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i--)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
delay();
return(k);
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xa1);
respons();
date=read_byte();
stop();
return date;
}
void display(uchar bai_c,uchar sh_c)
{
dula=1;
P0=table[bai_c];
dula=0;
P0=0xff;
wela=1;
P0=0x7e;
wela=0;
delayms(5);
dula=1;
P0=table[sh_c];
dula=0;
P0=0xff;
wela=1;
P0=0x7d;
wela=0;
delayms(5);
}
void main()
{
init();
sec=read_add(2);
if(sec>100)
{
sec=0;
}
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
display(sec/10;sec%10);
if(write=1) //判断计时器是否计时到一秒
{
write=0;
write_add(2,sec); //在24c02的地址中写入数据sec
}
}
}
void to() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
tcnt++;
if(tcnt>=20)
{
tcnt=0;
sec++;
write=1;
if(sec==100) //定时100,从零开始
{
sec=0;
}
}
} 百度上说把void改为uchar,但还是不得,求高手指点 |