0

I have a very big problem by writing a list of strings to an eeprom (24LC1025) Your help with my problem highly appericiated.

I have a data matrix, PageData[111][96] and I want to write all them at once, line by line to the eeprom. I am using arduino for coding.

the code is :

{

#include <Wire.h>

#define EEPROM_ADDR 0xA0;

void setup()
{

  Wire.begin();         

  Serial.begin(9600);
int PageData[111][96]={ my data is here ... }

 for (t=0; t<111; t++){                                   
  for (unsigned long i=0; i<96; i++){     
    m=i*2;
    value[m] = highByte(PageData[t][i]);
    value[m+1] = lowByte(PageData[t][i]);   }

  at=((t*192));

 for (unsigned long i=0; i< 192; i++) {            
    i2c_eeprom_write_byte(EEPROM_ADDR,(at+i),value[i]);   
    delay(5);                             }
      }

void i2c_eeprom_write_byte( int deviceaddress, unsigned long eeaddress, byte data )
{
if( eeaddress > 65535 )
    deviceaddress = deviceaddress | B00001000;    

    deviceaddress = deviceaddress >> 1;  

  byte rdata = data;
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));    // Address High Byte
  Wire.write((int)(eeaddress & 0xFF));  // Address Low Byte
  Wire.write(rdata);
  Wire.endTransmission();
}

My problem in this code: when I want to read the eeprom for the checking the strings, I realize, it does not write the eeprom the whole 111 line. It just writes randomly sometimes half of this sometimes less than this amount.

I tried several ways to solve the problem. but now I am desperate , please check this and tell me where i miss a point. Please let me know, if any information you need for the answer.

6
  • 2
    111 x 96 x 2 Byte = 21312 Byte. How is this supposed to fit into your maybe 4096 Bytes SRAM? You're probably getting a stack overflow and undefined behavior. – Rev Feb 13 '17 at 17:44
  • @brhans: I am not talking about the Flash, I am refering to the SRAM of the Arduino controller. "Stack overflow" has nothing to do with external storage. – Rev Feb 13 '17 at 17:57
  • @Rev1.0 - doh - I should read more carefully - didn't even consider that the OP might be messing up the basics ... – brhans Feb 13 '17 at 18:07
  • arduino due has 96kbyte SRAM not 4096 bytes. arduino.cc/en/Main/arduinoBoardDue – Ahmed Tarhan Feb 13 '17 at 19:53
  • @AhmedTarhan: Granted, but you left us guessing what kind of Arduino you have. And since most have 8-bit controllers with very little ram, limited SRAM was a potential issue. – Rev Feb 13 '17 at 21:44
1

Writing a single byte is really bad. You are causing a page write with each bye, and each page has a limited lifetime (100,000 - 1,000,000 cycles) so use block instead.

When writing any amount of data, including a single byte using this function, the EEPROM will refresh the entire page (128 bytes) of data. EEPROMs have a limited write endurance (often 1 million cycles per page). Therefore, if you write one byte at a time, instead of writing a full block at once, you are wasting the chip's lifetime (doing so is also MUCH slower). ONLY use this if you really only need to write ONE byte, and avoid it when you can. Using a block write (below), you can write 128 bytes and only use a single page write (or at most two, if the write is not aligned to a page boundary).

This library should do the trick for you: https://github.com/exscape/electronics/tree/master/Arduino/Libraries/EEPROM/EEPROM_24XX1025

2
  • Duh! Please, expand your answer. Other than avoid unnecesary writings, I haven't found much about EEPROMs. Is your info valid for all kinds of EEPROM? – user31481 Oct 29 '17 at 20:57
  • Im sorry buddy, I don't get what you mean? Expand my answer of not writing one byte at a time? I referenced a well written out description on why, and a library that will take care of writing with pages. If you need more information, you can click on the link and read about which modules are supported. Do you need help for a specific module, or a specific scenario? The original post is about a 24LC1025 module, which is supported for the first block (64KB) by the referenced library. – Lev Nov 20 '17 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.