Pages: [1] 2   Go Down
Author Topic: Problem for reading and writting I2C Eeprom 24c02 6  (Read 5622 times)
0 Members and 1 Guest are viewing this topic.
France
Offline Offline
Jr. Member
**
Karma: 1
Posts: 85
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

#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 ?



* BILD3742.JPG (856.87 KB, 3072x2304 - viewed 89 times.)
« Last Edit: February 12, 2013, 03:14:06 am by hary » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there anything I'm doing wrong ?
Does your code really look like that? Try again.
Logged

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

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.
Logged

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

Thanks for the advice.

I modified it. Yes it looks much better.

Best regards.
Logged

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

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
Logged

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

Hello Nick

It gives :

I2C scanner. Scanning ...
Found address: 80 (0x50)
Done.
Found 1 device(s).


I've test another one in SOIC package and I get :
I2C scanner. Scanning ...
Found address: 80 (0x50)
Found address: 81 (0x51)
Done.
Found 2 device(s).


But when I try to write and read, there is only 255 in each Byte.





« Last Edit: February 12, 2013, 08:50:17 am by hary » Logged

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

In the readData function try this instead of endTransmission():
Code:
  Wire.endTransmission(1);

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

Unfortunately, no change El Supreremo!


still 255 all the time !


* COM.3.png (6.87 KB, 663x632 - viewed 63 times.)
« Last Edit: February 12, 2013, 10:47:59 am by hary » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  Wire.write((int)(addr >> 8));
  Wire.write((int)(addr & 0xFF));
After going to the trouble of splitting the int address into low order byte and high order byte, why are you then casting the bytes to ints?
Logged

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

The 24c02 only has 256 bytes. It only uses one byte of addressing so you would have to address it like this:
Code:
  Wire.beginTransmission(i2caddr);
  // set the pointer position
  Wire.write((int)(addr & 0xFF));
  Wire.write(data);
  Wire.endTransmission();
I don't know if it needs the repeated start bit. If the above doesn't work, try it with Wire.endTransmission(1)

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

From the datasheet:

Quote
WRITE PROTECT (WP): The AT24C01A/02/04/08A/16A has a Write Protect pin that provides hardware data protection. The Write Protect pin allows normal Read/Write operations when connected to ground (GND).

How do you have the write protect pin wired?
Logged

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

From the datasheet:

Quote
BYTE WRITE: A write operation requires an 8-bit data word address following the device address word and acknowledgment. Upon receipt of this address, the EEPROM will again respond with a zero and then clock in the first 8-bit data word. Following receipt of the 8-bit data word, the EEPROM will output a zero and the addressing device, such as a microcontroller, must terminate the write sequence with a stop condi- tion. At this time the EEPROM enters an internally timed write cycle, tWR, to the nonvolatile memory. All inputs are disabled during this write cycle and the EEPROM will not respond until the write is complete (see Figure 8 on page 11).

The way I read that, you send the low-order address and the data. You are sending two bytes of address and then the data.

Ditto for the read (see Figure 11). You should send one byte of address, then do a requestFrom.
Logged

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

Well, thanks for your answers but I'm at the very beginning with this I2C EEPROM and you get me a little bet lost.

PaulS;
I'm just using a Sketch found somewhere on the web.
As it is my very first go with I2C EEPROM, I'm not really understanding the code actually.
And I'm not a programmer, so till now I've been mostly copying different pieces of code and modifying some part, then look what was happening.


Nick Gammon ;
The WP pin is to GND, so writing should be allowed.

You are talking about 2 Bytes of address that should be replaced by a only 1Byte address because the 24C02 has only 256 memory blocks. (if I understand well)
I've seen something talking about LSB and MSB concerning the address.
So I think, you might be talking about something that has to do with this. Is that right ?

But can you notice, on a previous message when I'm using Nick I2C Scanner on 2 different EEPROM, I got :
For the 24C02 :
I2C scanner. Scanning ...
Found address: 80 (0x50)
Done.
Found 1 device(s).


I've tested another one in SOIC package (Reference unreadable) and I get :
I2C scanner. Scanning ...
Found address: 80 (0x50)
Found address: 81 (0x51)
Done.
Found 2 device(s).

So, I deduce that the 2nd EEPROM was more than 256Bytes and needs an address on 2 byte (LSB and MSB)

Fortunately, we can see what happen on 2 examples. (2kB or more)
Unfortunately, the Write/Read sketch don't work for both !

I hope I'm clear enough as I'm a beginner and English is not my first language.

Many thanks for your help.


Logged

Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So, I deduce that the 2nd EEPROM was more than 256Bytes and needs an address on 2 byte (LSB and MSB)

No, the device address is the extra address bit.

Please post your code with the suggested modifications in it.
Logged

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



Well seen Miran Ligowyr !
Sorry, I'm guilty, I haven't properly done the modification.
I haven't neutralised the "Wire.write((int)(addr >> smiley-cool);" piece of code and then I've done it for the readData subroutine but not for the "writeData" subroutine.

Here is the code and now it works great !

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<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(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()
{
}

To see what happen,for the 24C02, I replaced "for (int i=0; i<20; i++)" with
"for (int i=0; i<260; i++)
  and I got
Quote
Writing data...
DONE
Reading data...
0 : 0
1 : 1
2 : 2
3 : 3
4 : 4
.
.
.
252 : 252
253 : 253
254 : 254
255 : 255
256 : 0
257 : 1
258 : 2
259 : 3
DONE

At #256 step, it seems to be rewriting on the #0 Byte ?

I'm playing with this ans I'll call you back later if needed.

Thanks so much for your help !
Logged

Pages: [1] 2   Go Up
Jump to: