Go Down

Topic: Write external EEprom is takes 1-2 seconds (Read 465 times) previous topic - next topic

dgerler

I have some code that writes to an external EEProm. It take much longer that writing to the internal EEProm. The delay is annoying. The data is typically less than 100 characters but always less than 150.

Can anyone help me with a more efficient way, or is this as good as it gets?

I've thought about buffering the data through internal EEProm and when the program finishes a cycle move it to the external and Clear the internal.

Code: [Select]
  #include <Wire.h> //I2C library

  void write2ExtEeprom(int address, char* myDataStr)
  {
    Wire.beginTransmission(0x50);
    Wire.send((int)(address >> 8)); // MSB
    Wire.send((int)(address & 0xFF));
     for(int i=0; i< strlen(myDataStr); i++)
{
      Wire.send(myDataStr[i]);
      delay(20);
      //Serial.print(myDataStr[i]);
}  // sends one byte     
  Wire.endTransmission();    // stop transmitting
  //Serial.println();
}

void EwrCur(int Curaddr)
{ // Write Current Address to Memory-
  int CurMsbLsb = Curaddr;     // -Locations 0 and 1
  byte CurMsb = (CurMsbLsb>>8); // Break (int)Curaddr into two bytes
  byte CurLsb = (CurMsbLsb&0xFF);
  Ewr0(0,CurMsb);   // Write Curaddr Msb to Memory Location 0
  Ewr0(1,CurLsb);
} // Write Curaddr Lsb to Memory Location 1

void Ewr0(unsigned int Eaddr, byte data) // Write only to Loc 0
  { byte rdata = data;
    Wire.beginTransmission(0x50);
    Wire.send((int)(Eaddr >> 8)); // MSB
    Wire.send((int)(Eaddr & 0xFF)); // LSB
    Wire.send(rdata);
    //Serial.print("rdata = ");
    //Serial.print(rdata, HEX);
    //Serial.println();
    Wire.endTransmission();
    delay(20);
}
   
int ErdCur()
{ // Read Ext Eeprom Memory Locations 0 and 1 CurrAddress
   byte CMsb = Erd0(0);
   //Serial.print("CMsb = ");
    //Serial.print(CMsb, HEX);
    //Serial.println();
   byte CLsb = Erd0(1);
   //Serial.print("CLsb = ");
   // Serial.print(CLsb, HEX);
   // Serial.println();
   int CMsbLsb = ((CMsb<<8) + (CLsb&0xFF));
   //Serial.print("CMsbLsb = ");
   // Serial.print(CMsbLsb, HEX);
   // Serial.println();
   int Caddr = CMsbLsb;   
   //Serial.print("Caddr = ");
    //Serial.print(Caddr, HEX);
    //Serial.println();
    return Caddr;
}
   
  byte Erd0(unsigned int eaddr )
  {
    byte rdata = 0xFF;
    Wire.beginTransmission(0x50);
    Wire.send((int)(eaddr >> 8)); // MSB
    Wire.send((int)(eaddr & 0xFF)); // LSB
    Wire.endTransmission();
    Wire.requestFrom(0x50,1);
    if (Wire.available())
    rdata = Wire.receive();
    return rdata;
  }


void storeExtEeprom(char* dataStr1)
{
  int addr;
  addr = ErdCur();
  write2ExtEeprom(addr, dataStr1);
  addr+= strlen(dataStr1);
  EwrCur(addr);
}
   
 
//   void setup() {
//    Wire.begin(); // initialise the connection
//    Serial.begin(9600);
//    EwrCur(0x00);
//}
//
//  void loop() {
//  String dataStr = "Offender of The Universe.";
//  ExtEeprom(dataStr);
//  delay(2000);
//}   
//

mmcp42

well a 20 msec delay repeated 100 times = ??
there are only 10 types of people
them that understands binary
and them that doesn't

PaulS

The Wire.send() method has an overload that can write the whole array at once, without the need for the for loop (or the delay).

robtillaart


check - http://arduino.cc/playground/Main/LibraryForI2CEEPROM -
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

dgerler


dgerler

Doh! I didn't think of that, but it works like a charm. I looked for large delays, but didn't thhink of the *100.

OK. I was to be able to get the data out the same way I put it in, so I am thinking of storing the size of the data inline like so:

size|data|size|data

I think that is a fairly common way of doing it. Can someone help point me towards an example to write and then read that way. 

Go Up