Dear all,
i meet with a problem recently, and i need your help.
I'm trying to read and write from the 24AA01 using the I2C .
my design is this: first, i write a data 0x88 into the 24AA01 (address 0x02).then, i want to read the data (0x88) from the 24AA01. theoritically, i will get the data 0x88. but what i get is 0xFF. and whatever i change the data , i can only get the data 0xFF. i don't know what's wrong with my code, maybe something wrong with the hardware.
the picture and the c code are attached. so, i need your help. thank you.

#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit SCL=P2^0;
sbit SDA=P2^1;
bit flag1;
bit flag2;
bit flag3;
bit flag4;
bit flag5;
bit flag6;
bit flag7;
uchar result;
void dly_usec(uchar t)
{
uint i;
for(i=t;i>0;i--);
}
void init_i2c()
{ SDA=1;
SCL=1;
}
void start(void)
{
SDA = 1 ;
SCL = 1 ; // initial state of the I2C bus
dly_usec(7) ;
SDA = 0 ; // SCL=1 , SDA=0 after 7 usecs
dly_usec(5) ;
SCL = 0 ; // SCL=0 , SDA=0 after other 5 usecs
}
void stop(void)
{
SDA = 0 ; // SDA=0,SCL=0, initial state of I2C
dly_usec(2) ;
SCL = 1 ; // SDA=0,SCL=1, after 2 usecs
dly_usec(6) ;
SDA = 1 ; // SDA=1,SCL=1, after other 5 usecs
}
void ACK(void)
{
SDA = 0 ;
dly_usec(3) ; // SDA=0,SCL=0 for 3 usec
SCL = 1 ;
dly_usec(6) ; // +pulse SCL for 6 usec
SCL = 0 ;
dly_usec(2) ; // 2 usec stabilization for SCL
SDA = 1 ;
dly_usec(2) ; // prepare SDA for reception
}
void NO_ACK(void)
{
SDA = 1 ;
dly_usec(3) ; // SDA=1,SCL=0 for 3 usecs
SCL = 1 ;
dly_usec(6) ; // SDA=1,SCL=1 for 6 usecs ; +pulse SCL
SCL = 0 ;
dly_usec(2) ; // SDA=1,SCL=0,SCL stabilization 2us
}
void BYTE_WRITE(uchar w_address,uchar w_add)
{
uchar w_control_word=0xa0;
uchar j,k,m;
start();
for(j=0;j<8;j++)
{
SCL=0;
dly_usec(3);
SCL=1;
dly_usec(3);
SDA=(bit)(w_control_word&0x80);
flag1=SDA;
w_control_word<<=1;
}
ACK();
for(k=0;k<8;k++)
{
SCL=0;
dly_usec(3);
SCL=1;
dly_usec(3);
SDA=(bit)(w_address&0x80);
flag2=SDA;
w_address<<=1;
}
ACK();
for(m=0;m<8;m++)
{
SCL=0;
dly_usec(3);
SCL=1;
dly_usec(3);
SDA=(bit)(w_add&0x80);
flag3=SDA;
w_add<<=1;
}
ACK();
stop();
}
uchar BYTE_READ(uchar r_address)
{
uchar r_control_word=0xa1;
uchar w_control_word=0xa0;
uchar r_data=0x00;
uchar a,b,c,d;
start();
for(a=0;a<8;a++)
{
SCL=0;
dly_usec(3);
SCL=1;
dly_usec(3);
SDA=(bit)(w_control_word&0x80);
flag4=SDA;
w_control_word<<=1;
}
ACK();
for(b=0;b<8;b++)
{
SCL=0;
dly_usec(3);
SCL=1;
dly_usec(3);
SDA=(bit)(r_address&0x80);
flag5=SDA;
r_address<<=1;
}
ACK();
start();
for(c=0;c<8;c++)
{
SCL=0;
dly_usec(3);
SCL=1;
dly_usec(3);
SDA=(bit)(r_control_word&0x80);
flag6=SDA;
r_control_word<<=1;
}
ACK();
for(d=0;d<8;d++)
{
r_data<<=1;
dly_usec(3);
SCL=1;
dly_usec(3);
flag7=SDA;
dly_usec(4);
if(SDA==1) r_data++;
SCL=0;
dly_usec(4);
}
NO_ACK();
stop();
return(r_data);
}
void main(void)
{
init_i2c();
P1=0xff;
BYTE_WRITE(0x02,0x88);
result=BYTE_READ(0x02);
P1=result;
while(1);
}