EEPROM problems

Hello,

I’m trying to write to an EEPROM module (see picture).

The test code is based on code from this page:

#include <Wire.h>

#define EEPROM_ADDR   0x50

void setup() 
{
  Serial.begin(9600);
  Serial.println(F("Initializing."));
  
  Wire.begin();

  Serial.print(F("Writing to EEPROM "));
  writeToEeprom();
  Serial.println(F("Done"));
  delay(200);
}

void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data) 
{
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));
  Wire.write((int)(eeaddress & 0xFF));
  Wire.write(data);
  Wire.endTransmission();
}

byte readEEPROM(int deviceaddress, unsigned int eeaddress, bool *rStatus)
{
  byte rdata = 0xFF;
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));
  Wire.write((int)(eeaddress & 0xFF));
  Wire.endTransmission();
  Wire.requestFrom(deviceaddress, 1);
  if (Wire.available()) {
    rdata = Wire.read();
    *rStatus = true;
  } else {
    *rStatus = false;
  }
  
  return rdata;
}

void writeToEeprom(void)
{
  for (unsigned int addr = 0; 256 > addr; addr++) {
    writeEEPROM(EEPROM_ADDR, addr, 0);
  }
}

void loop() 
{
  byte  data;
  bool  rStatus;
  
  // Read from EEPROM.
  Serial.println("");
  Serial.println(F("Reading EEPROM:"));
  for (unsigned int addr = 0; 256 > addr; addr++) {
    data = readEEPROM(EEPROM_ADDR, addr, &rStatus);

    if (rStatus) {
      Serial.print(data, HEX);
    } else {
      Serial.print(F("--"));
    }
    Serial.print(F(" "));

    if (19 == addr % 20) {
      Serial.println("");
    }
  }

  delay(10000);
}

The output is:

16:05:46.736 -> Reading EEPROM:
16:05:46.736 -> 0 FF FF FF FF FF FF FF FF FF FF FF FF FF 0 F FF FF FF FF 
16:05:46.804 -> FF FF FF FF FF FF FF FF 1C 0 FF FF FF FF FF FF FF FF FF 27 
16:05:46.872 -> FF FF 2A 0 FF 2D FF FF 30 FF FF 33 FF FF 36 FF 38 0 0 FF 
16:05:46.940 -> 3C FF FF 3F FF FF 42 FF FF 45 46 FF 0 0 FF 4B FF FF 4E FF 
16:05:47.008 -> FF 51 FF FF 54 FF 0 0 FF FF 5A FF FF 5D FF FF 60 FF FF 63 
16:05:47.076 -> 64 0 0 FF FF 69 FF FF 6C FF FF 6F FF 71 72 73 0 75 FF FF 
16:05:47.110 -> 78 FF FF 7B FF FF 7E 7F FF 81 0 83 84 FF FF 87 FF FF 8A FF 
16:05:47.178 -> FF 8D FF FF 90 0 92 93 FF FF 96 FF FF 99 FF 9B 9C FF FF 0 
16:05:47.246 -> A0 FF A2 FF FF A5 FF FF A8 A9 FF AB FF AD 0 AF FF B1 FF FF 
16:05:47.314 -> B4 FF FF B7 FF FF BA FF BC 0 BE FF C0 FF FF C3 FF C5 C6 FF 
16:05:47.382 -> FF C9 FF 0 CC FF FF CF FF FF D2 D3 FF D5 FF FF D8 FF 0 DB 
16:05:47.450 -> FF FF DE FF FF E1 FF FF E4 FF FF E7 FF 0 EA FF FF ED FF FF 
16:05:47.518 -> F0 FF FF F3 FF FF F6 FF 0 F9 FF FF FC FF FF FF

which is not what I expected (I expected 0). I have tried to remove and switch position of the WP jumper, without luck.

Bad EEPROM module or? Suggestions/feedback would be nice.

How do you know the EEPROM in the code is the same as the one you bought?

Good question. Did no though about it. The chip I use is a Atmel 24C256.

Just changed the test and it seem to write to some addresses. Guess I have to search for code that is using a 24C256.

Can someone please tell me that I’m s**id and that I should RTFM??

This EEPROM has 64 byte block and the test code cannot work the way I wrote it.

I looked at the sketch I wrote some month ago where I’m suppose to use this EEPROM was written for 64 byte block. I just had forgotten about that. By using parts from the sketch in my test sketch, it seem to work OK.

New code:

#include <Wire.h>
#include <LCD.h>
#include <MyLcd_I2C.h>
#include <extEEPROM.h>

#define EEPROM_ADDR   0x50
#define LCD_ADDR      0x27

/*
 * The EEPROM module using I2C.
 */
extEEPROM eeProm(kbits_256, 1, 64, EEPROM_ADDR);

MyLcd_I2C lcd1(
  LCD_ADDR, // LCD address on the I2C bus.
  2,        // En: (Enable) pin connected to the IO extender module
  1,        // Rw: (Read/write) pin connected to the IO extender module
  0,        // Rs: (Reset) pin connected to the IO extender module
  4,        // d4: data 0 pin map on IO extender module
  5,        // d5: data 1 pin map on IO extender module
  6,        // d6: data 2 pin map on IO extender module
  7         // d7: data 3 pin map on IO extender module
);

void setup() 
{
  Serial.begin(9600);
  Serial.println(F("Initializing."));

  lcd1.begin(16, 2);
  lcd1.setBacklightPin(3, POSITIVE);
  lcd1.setBacklight(HIGH);
  lcd1.print("EEPROM test");

  // Set up the EEPROM module.
  uint8_t eeStatus = eeProm.begin(extEEPROM::twiClock400kHz);
  if (eeStatus) {
    lcd1.setCursor(1, 0);
    lcd1.print("EEPROM err: ");
    lcd1.print(eeStatus);
    while (1);
  }

  Wire.begin();

  char buffer[30];
  byte databuffer[64], readbuffer[64];
  int addr = 0x0000, errcnt = 0;
  
  for (byte value = 0; 10 > value; value++) {
    sprintf(buffer, "Writing %d to EEPROM. ", (int)value);
    Serial.print(buffer);

    for (uint8_t i = 0; 64 > i; i++) {
      databuffer[i] = value;
    }

    sprintf(buffer, "Writing %d", (int)value);
    lcd1.at(1, 0, buffer);
    eeProm.write(addr, databuffer, 64);
    Serial.println(F("Done"));
    lcd1.at(1, 0, "EEPROM updated");
    delay(2000);

    // Read it.
    lcd1.at(1, 0, "Reading... ");
    Serial.print("Reading... ");
    eeProm.read(addr, readbuffer, 64);

    errcnt = 0;
    for (uint8_t i = 0; 64 > i; i++) {
      if (readbuffer[i] != databuffer[i]) {
        ++errcnt;
      }
    }

    if (0 == errcnt) {
      lcd1.at(1, 11, "OK ");
      Serial.println("OK");
    } else {
      sprintf(buffer, "%d", errcnt);
      lcd1.at(1, 11, buffer);
      Serial.println(buffer);
    }
    delay(5000);
  }
}

void loop() 
{
}

And the output:

20:53:03.045 -> Initializing.
20:53:03.181 -> Writing 0 to EEPROM. Done
20:53:05.221 -> Reading... OK
20:53:10.251 -> Writing 1 to EEPROM. Done
20:53:12.293 -> Reading... OK
20:53:17.324 -> Writing 2 to EEPROM. Done
20:53:19.367 -> Reading... OK
20:53:24.371 -> Writing 3 to EEPROM. Done
20:53:26.444 -> Reading... OK
20:53:31.443 -> Writing 4 to EEPROM. Done
20:53:33.517 -> Reading... OK
20:53:38.512 -> Writing 5 to EEPROM. Done
20:53:40.552 -> Reading... OK
20:53:45.585 -> Writing 6 to EEPROM. Done
20:53:47.626 -> Reading... OK
20:53:52.659 -> Writing 7 to EEPROM. Done
20:53:54.699 -> Reading... OK
20:53:59.730 -> Writing 8 to EEPROM. Done
20:54:01.769 -> Reading... OK
20:54:06.799 -> Writing 9 to EEPROM. Done
20:54:08.838 -> Reading... OK