Pages: [1]   Go Down
Author Topic: Write external EEprom is takes 1-2 seconds  (Read 380 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  #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);
//}   
//
Logged

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 20
Posts: 1318
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well a 20 msec delay repeated 100 times = ??
Logged

there are only 10 types of people
them that understands binary
and them that doesn't

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

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

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


check - http://arduino.cc/playground/Main/LibraryForI2CEEPROM -
Logged

Rob Tillaart

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

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

Thank you guys. I'll take a look see! smiley
Logged

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

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

Pages: [1]   Go Up
Jump to: