11 years, 2 months ago.

Problem with reading I2C EEPROM (24C02)

I am trying to read and write I2C EEPROM (24C02 from Microchip) but i failed every-time. i am using 2.2Kohms as a pull up resistor in the I2C bus. My code is following............. if there is any kind of wrong please inform me or new code.

#include "mbed.h"

int address;

I2C i2c(p28, p27);
DigitalOut myled(LED3);

int main(void)
{
int read=170,r;

    wait(1);
    
  
     
    while(1) 
    {
    
    i2c.start();
    
    i2c.write(160);
    i2c.write(3);
    i2c.write(170);
    i2c.stop();
    
    i2c.start();
    i2c.write(160);
    i2c.write(3);
    i2c.start();
    i2c.write(161);
    r=i2c.read(0u);
    i2c.stop();
    
    if(r==read)
        myled=1;
    else 
        myled=0;
        
    wait(10);

    }    
    
    
}

2 Answers

11 years, 2 months ago.

Please use code tags around your code, then it is much easier to find out what your code is.

For sure it is alot easier to use the 'complete' I2C commands, which will take care of the start and stop conditions for you. (See the I2C API). Officially you need to transmit a restart after setting the read address, although I dont think it should matter this time.

Handy to check first if you are getting a connection with the device, try running the following code:

for (int i=0; i<=254; i=i+2) {
        if (i2c.write(i, NULL, 0) ==0) printf("I2C device detected at address=%2.2X\n\r", i);
    }

Razu Ahmmed
poster
11 years, 2 months ago.

i used tags. suggest me.