51单片机控制EEPROM,哪位大神帮忙看i2c通信错在哪里了
#include <reg52.h>
#define uchar unsigned char
sbit scl=P3^0;
sbit sda=P3^1;
void delay();
void delayl(uchar times);
void start();
void stop();
void tack();
void nack();
void ack();
void write(uchar date);
uchar read();
void writeadd(uchar address,uchar date);
uchar readadd(uchar address);
void delay()
{
;
;
}
void delayl(uchar times)
{
uchar x,y;
for(x=times;x>0;x--)
for(y=110;y>0;y--);
}
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
scl=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
scl=0;
delay();
}
void tack()
{
scl=1;
delay();
scl=0;
delay();
}
void nack()
{
sda=1;
delay();
scl=1;
delay();
scl=0;
delay();
}
void ack()
{
sda=0;
delay();
scl=1;
delay();
scl=0;
delay();
}
void write(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
}
uchar read()
{
uchar temp,i,k;
for(i=0;i<8;i++)
{
scl=1;
delay();
if(sda==1)
k=1;
else
k=0;
temp=(temp<<1)+k;
scl=0;
delay();
}
return temp;
}
void writeadd(uchar address ,uchar date)
{
start();
write(0xae);
tack();
write(address);
tack();
write(date);
tack();
stop();
}
uchar readadd(uchar address)
{
uchar date;
start();
write(0xae);
tack();
write(address);
tack();
start();
write(0xaf);
tack();
date=read();
nack();
stop();
return date;
}
void main()
{
scl=1;
sda=1;
writeadd(0x00,0xfe);
delayl(1000);
P1=readadd(0x00);
while(1);
} |