Eeprom problem when interfacing with 8051

Status
Not open for further replies.

arupananda26

Newbie level 3
Joined
May 16, 2012
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Bangalore
Activity points
1,329
I have interfaced EEPROM (24C02A) with AT89C2051 ...
but data is not written to EEPROM ,can u anyone tell me where is the problem.
take a look on attached code

#define SDA P3_4
#define SCL P3_5

void I2C_Delay()
{
unsigned char i;
for(i=0;i<I2C_DELAY;i++)
;

}


/* Used for Clok Initiation */

void I2C_Clock()
{
I2C_Delay();
SCL = 1; /* Start clock */
I2C_Delay();
SCL = 0; /* Clear SCL */
}

/* Used for i2c communication start */

void I2C_Start()
{
if(SCL)
SCL = 0; /* Clear SCL */

SDA = 1; /* Set SDA */
SCL = 1; /* Set SCL */

I2C_Delay();
SDA = 0; /* Clear SDA */

I2C_Delay();
SCL = 0; /* Clear SCL */
}

/* Used for stop communication */

void I2C_Stop()
{
if(SCL)
SCL = 0; /* Clear SCL */

SDA = 0; /* Clear SDA */
I2C_Delay();

SCL = 1; /* Set SCL */

I2C_Delay();

SDA = 1; /* Set SDA */
}

/* Used for check the bus is idle or not */

void I2C_Idle()
{
if(SDA)
SDA=0;
if(SCL)
SCL=0;

}

// Used for Acknowledgement

void I2C_Ack()
{
// SDA = 0; /* Clear SDA */
//I2C_Delay();
I2C_Clock(); /* Call for send data to i2c bus */
// SDA = 1; /* Set SDA */

}


/* Used for write data through I2C data pin */

void I2C_Write(unsigned char dat)
{
// bit data_bit;
unsigned char i;

for(i=0;i<8;i++) // For loop 8 time(send data 1 byte)
{
if((dat& 0x80)==0x80) // Filter MSB to keep data bit
SDA=1; // put the value on SDA pin
else
SDA=0;
I2C_Clock(); /* Call for send data to i2c bus */
dat = (dat <<1);
}

SDA=0;

}

/* Used for read data through I2C data pin */

unsigned char I2C_Read()
{
bit rd_bit;
unsigned char i,dat;

dat = 0x00;

for(i=0;i<8;i++) /* For loop read data 1 byte */
{
I2C_Delay();

SCL = 1; /* Set SCL */

I2C_Delay();

rd_bit = SDA; /* Keep for check acknowledge */
dat = dat <<1;
dat = dat | rd_bit; /* Keep bit data in dat */

SCL = 0; /* Clear SCL */
}
SDA=0;

return dat;
}


void EEPROM_Write(unsigned char EAddress,unsigned char Edata)
{
// I2C_Delay(); // Generate i2c delay condition
// I2C_Idle(); // Generate i2c idle condition
I2C_Start(); // Generation of start condition
I2C_Write(0xA0); // 0xA0 is the EEPROM ADDRESS for Write Operation
I2C_Ack();
// I2C_Idle();
I2C_Delay();

// I2C_Write(EAddress>>8); // send higher address
// I2C_Delay();
// I2C_Write(EAddress); // send lower byte
I2C_Write(EAddress);
I2C_Ack(); // Get Acknowledgement
//
// I2C_Delay();
// I2C_Write(Edata>>8); // send higher data
// I2C_Ack(); // Get Acknowledgement

// I2C_Delay();
I2C_Write(Edata); // send higher data
I2C_Ack();

// I2C_Delay();
I2C_Stop(); // Generate a stop condition
I2C_Ack(); // Get Acknowledgement
Delay(2); // used for internal write cycle..

}


/* This function is used for fetching data from EEPROM */

unsigned char EEPROM_Read(unsigned char EAddress)
{
unsigned char i;
unsigned int Read_Data; // used to store the read data
// I2C_Idle(); // Generate a i2c idle condtion
I2C_Start(); // Generate a i2c start condition
I2C_Write(0xA0); // 0xA0 is the EEPROM ADDRESS for Write Operation
I2C_Ack(); // Get Acknowledgement

I2C_Delay(); // Generate a delay condtion
// I2C_Write(EAddress>>8); // send higher address
// I2C_Delay();
I2C_Write(EAddress); // send the address where data will be stored
I2C_Ack(); // Get Acknowledgement

// I2C_Delay();
I2C_Start(); // Restart the I2C for read operation

I2C_Write(0xA1); // 0xA1 is used for EEPROM Read opeartion
I2C_Ack(); // Get Acknowledgement

i=I2C_Read();
I2C_Ack(); // Get Acknowledgement
// I2C_Delay();
// j=I2C_Read();
// I2C_Ack(); // Get Acknowledgement
I2C_Delay();

I2C_Stop(); // Generate stop condition to stop i2c
I2C_Ack(); // Get the acknowledgement
// Read_Data=100*i+j;
Read_Data=100*i;
return Read_Data;

}

please find the problem
arup26pradhan@gmail.com
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…