Pages: 1 [2]   Go Down
Author Topic: Problem for reading and writting I2C Eeprom 24c02 6  (Read 5551 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I expect it wrap at 256 because you can't fit more than 255 into one byte, so it is wrapping.

Which device do you have? (How many K bits?)
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you have the 2 K bit device (that is, 256 bytes) then you won't fit more than 256 bytes into it, so naturally it wraps.
Logged

France
Offline Offline
Jr. Member
**
Karma: 1
Posts: 85
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes Nick, you might be right.

But as I wanted to try to write and read, I have a new issue.

The sketch I'm using :
Code:
#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<255; i++)
  {
    writeData(i,i);
  }
  Serial.println("DONE");
  Serial.println("Reading data...");
  for (int i=0; i<255; 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(1);
  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(1);
  Wire.requestFrom(i2caddr,1); // get the byte of data
  result = Wire.read();
  return result;
}

void loop()
{
}

The result I got in the Serial monitor that I put in a spread sheet.
You can notice, all the Bytes seems not to be written as they should be.

I modified the 10ms delay in the "writeData" subroutine but nothing change even with 20ms delay instead !

Any Idea ?

* 24C02 result W R.ods (9.81 KB - downloaded 19 times.)
Logged

Offline Offline
Edison Member
*
Karma: 43
Posts: 1550
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define i2caddr 0x50    // device address for left-hand chip on our breadboard

This comment implies that you also have another eeprom connected as well. You can't have two devices with the same address on the bus.

Pete
Logged

Where are the Nick Gammons of yesteryear?

France
Offline Offline
Jr. Member
**
Karma: 1
Posts: 85
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Pete (El Supremo).

As I said, I'm at the very beginning with I2C EEPROM devices, so I've just copied the code from the web without really understanding what is happening.

You're right about the comment, but I have only 1 EEPROM connected.
So I think it doesn't really matter for my present issue.
Logged

Offline Offline
Edison Member
*
Karma: 43
Posts: 1550
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In the writeData function you must not use Wire.endTransmission(1);
It must be either Wire.endTransmission();  or   Wire.endTransmission(0);

You only use (1) when doing a read - in between writing the address and then issuing the requestFrom.

Pete
Logged

Where are the Nick Gammons of yesteryear?

France
Offline Offline
Jr. Member
**
Karma: 1
Posts: 85
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Pete.

I've changed the (1) with the () ind the write, but it doesn't make any change. Still wrong data in the EEPROM.
The sketch in now as below :
Code:
#include <Wire.h>     // for I2C
#define i2caddr 0x50    // device address
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<257; i++)
  {
    writeData(i,i);
  }
  Serial.println("DONE");
  Serial.println("Reading data...");
  for (int i=0; i<257; 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(1);
  Wire.requestFrom(i2caddr,1); // get the byte of data
  result = Wire.read();
  return result;
}

void loop()
{
}


By the way. I'm having so hard time with this I2C EEPROM, would you have any good link with some sketches very well explained. I haven't been able to find something that could help me properly yet !

At the end, my project is to reprogram a Laser printer chip.
So, read an EEPROM (brand new and already programmed), then, write the data to the old one, so I've got a spare one.

I'll need some advice to do this. I've already read the data of the new 24C04.
I'll now need to inject these data in the old one.


* EepromWR2.ods (10.2 KB - downloaded 20 times.)
« Last Edit: February 15, 2013, 11:35:54 am by hary » Logged

Offline Offline
Edison Member
*
Karma: 43
Posts: 1550
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

At the end of writeData you have a delay. Try replacing it with this code:
Code:
    while(1) {
      Wire.beginTransmission(i2caddr);
      if(Wire.endTransmission() == 0)break;
    }
This code waits until the device has finished writing (it won't respond until it is done). But your delay of 20ms should be plenty of time to finish a write. It's worth a try.

I'm using larger EEPROMs (e.g. 24LC1026) but other than that your code is the same as mine.

Pete
Logged

Where are the Nick Gammons of yesteryear?

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's my attempt to read/write to a 24LC256 EEPROM using I2C:

http://www.gammon.com.au/forum/?id=10896&reply=2#reply2
Logged

France
Offline Offline
Jr. Member
**
Karma: 1
Posts: 85
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.

Sorry for the late response but I was on something else these time.

To make it working, I needed to power up the Arduino  through an external 12VDC power supply instead of the USB cable from the computer or even a 5vDC supply through Vin of the ARDUINO.

I also replace the delay by El Supremo's piece of code witch I thing is much more clever than wasting time with Delay and being not sure that the job is done !

But the problem was raelly the power supply of the ARDUINO. At least it now works fine.

Now I'm sure the writing and reading is ok, how can I write the proper number in each piece of memory ?

See, I've read the Eeprom I want to copy so I stored all the data in an open office format as .ods (OoCalc)or .odt (OoWriter) format)
1/   Read the data
2/   Copy from the SerialMonitor
3/   Paste in a calc or writer page to save the data in a safe place.

But I don't know how to program the new Eeprom with these data.
I'm not going to write each data at a time in the serial monitor. here must be a better way for sure.

Any advice.

How I can put these data in ?
Logged

Norway
Offline Offline
Full Member
***
Karma: 1
Posts: 142
likes to program Arduinos!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I recently intefaced with a similar EEPROM, 24LC01. I used the EEPROM24 library, which is really simple. Strongly reccomended, and support all kinds if 24xCxx EEPROMS
Logged

Pages: 1 [2]   Go Up
Jump to: