Go Down

Topic: Problem for reading and writting I2C Eeprom 24c02 6 (Read 6 times) previous topic - next topic

hary

Feb 11, 2013, 02:26 pm Last Edit: Feb 12, 2013, 09:14 am by hary Reason: 1
Hello.

I'm doing my first step working with I2C Eeprom and I can't get it working.

For my test, I'm using a 24C02 6 and I'm using this Sketch
Code: [Select]


#include <Wire.h>     // for I2C
#define i2caddr 0x50    // device address for left-hand chip on our breadboard
byte d=0; // data to store in or read from the EEPROM

void setup()
{
  Serial.begin(9600); // Initialize the serial line
  Wire.begin();         // wake up the I2C

  Serial.println("Writing data...");
  for (int i=0; i<20; i++)
  {
    writeData(i,i);
  }
  Serial.println("DONE");
  Serial.println("Reading data...");
  for (int i=0; i<20; i++)
  {
    Serial.print(i);
    Serial.print(" : ");
    d=readData(i);
    Serial.println(d, DEC);
  }
  Serial.println("DONE");

}

// writes a byte of data in memory location addr
void writeData(unsigned int addr, byte data)
{
  Wire.beginTransmission(i2caddr);
  // set the pointer position
  Wire.write((int)(addr >> 8));
  Wire.write((int)(addr & 0xFF));
  Wire.write(data);
  Wire.endTransmission();
  delay(10);
}

// reads a byte of data from memory location addr
byte readData(unsigned int addr)
{
  byte result;
  Wire.beginTransmission(i2caddr);
  // set the pointer position
  Wire.write((int)(addr >> 8));
  Wire.write((int)(addr & 0xFF));
  Wire.endTransmission();
  Wire.requestFrom(i2caddr,1); // get the byte of data
  result = Wire.read();
  return result;
}

void loop()
{
}


My circuit is as on the picture attached :

It gives me only 255 value faor all Bytes !!!
I've tried with another Eeprom (from the printer chip I'm trying to reprogram) and it would give me the same value 255 on each Byte.

Is there anything I'm doing wrong ?


PaulS

Quote
Is there anything I'm doing wrong ?

Does your code really look like that? Try again.

Nick Gammon

Read this before posting a programming question


Don't use "copy for forum" in the IDE. Just select the code, copy it, and paste it here, inside code tags.

hary

Thanks for the advice.

I modified it. Yes it looks much better.

Best regards.

Nick Gammon

I can't see anything obviously wrong. Try running my I2C scanner and see what it reports:

http://www.gammon.com.au/forum/?id=10896&reply=6#reply6

Go Up