Go Down

Topic: Writing/reading to external EEPROM (Read 677 times) previous topic - next topic

robertKrakowski

May 25, 2013, 04:34 pm Last Edit: May 25, 2013, 06:02 pm by robertKrakowski Reason: 1
I've tried to write/read from external EEPROM (standard 24LC256) 1024 bytes array byte by byte and by 16 bytes pages. It takes some time to save 512 int samples to EEPROM  -  about 10 000 000 us, and reading is much faster at 130 000 us. Next I wanted to improve code - I tried page writing and changed slightly writing and reading method to the EEPROM. New writing method on its own seems to be working however behavior is not exactly what I expected. Writing is much faster at about 600 000 us however reading is also 600 000 us hence 5 times more than old method. The code is:
Code: [Select]
#include "Wire.h"

#define s 512
#define chip1 0x50

byte conv[2*s];
byte reconv[2*s];
int val[s];
int val2[s];

unsigned long e;          
unsigned long f;          
unsigned long g;

unsigned int u = 0;
unsigned int a = 0;
unsigned int d[s];

void pageWrite(byte sampleNum)
{
 byte i; //Loop Counter
 int data = 0; //Array Index
 unsigned int address = (sampleNum * s);
 while (data < 2*s)
 {
   Wire.beginTransmission(0x50);
   Wire.write((int)((address) >> 8));   // High Byte
   Wire.write((int)((address) & 0xFF)); // Low Byte
   for (i=0; i < 16; i++)
   {
     Wire.write(conv[data]);
     data++;
   }
   Wire.endTransmission();
   delay(10);
   address = address + 16;
 }
}

void pageRead(byte sampleNum)
{
 int data = 0; // Array Index
 int i = 0; // Loop Counter
 unsigned int address = (sampleNum * 192);
 while (data<2*s)
 {
   Wire.beginTransmission(0x50);
   Wire.write((int)(address >> 8)); // High Byte
   Wire.write((int)(address & 0xFF)); // Low Byte
   Wire.endTransmission();
   Wire.requestFrom(0x50, 16);
   while(Wire.available())
   {
     for (int c=0; c<16; c++)
     {
       reconv[data] = Wire.read();
       data++;
     }
   }
   address = address + 16;
   delay(10);
 }
}

void IntToByteConv(void)
{
 int j = 0;
 for (int i=0; i<(2*s) ;i++)
 {    
   conv[i] = (val[j]>> 8);   // write MSB byte of sample to EEPROM
   i++;
   conv[i] = (val[j]& 0xFF); // write LSB byte of sample to EEPROM
   j++;  
 }
}

void ByteToInvConv(void)
{
 int a;
 int j = 0;
 for (int i=0; i<(2*s) ;i++)
 {
   a = reconv[i];
   a = a<<8;
   i++;
   a += reconv[i];
   val2[j] = a;
   j++;
 }
}

void writeData(int device, unsigned int add, byte data)
{
 Wire.beginTransmission(device);
 Wire.write((int)(add>>8));                 // left part of pointer address
 Wire.write((int)(add & 0xFF));             // and the right
 Wire.write(data);
 Wire.endTransmission();
 delay(10);
}

byte readData(int device, unsigned int add)
{
 byte result; //returned value
 Wire.beginTransmission(device);            // these three lines set the pointer position in the EEPROM
 Wire.write((int)(add>> 8));                // left part of pointer adsress
 Wire.write((int)(add & 0xFF));             // and the right
 Wire.endTransmission();
 Wire.requestFrom(device,1);                // now get he byte of data
 if (Wire.available()) result = Wire.read();
 return result;                             // and return it as a result of the function readData
}


void setup()
{
 Serial.begin(115200);            
 Wire.begin();
 analogReadResolution(12);

 pinMode(10, OUTPUT);
 digitalWrite(10, LOW);

 Serial.println("Ready");

 Serial.println("Sampling");
 for (int i=0; i<s; i++)
 {
   val[i] = analogRead(11);
 }
 Serial.println("sampling done");
 Serial.println("Value in array val: ");
 for (int i=0; i<s; i++)
 {
   Serial.println(val[i]);
 }
 Serial.println("Array done");

 IntToByteConv();  

 //    for (int i=0; i<(2*s) ;i++)
 //    {    
 //      Serial.println(conv[i]);
 //    }
 //      
 Serial.println("Writing in EEPROM");
 e=micros();
 pageWrite(0);
 f=micros();
 g = f-e;
 Serial.print("Writing done in :");
 Serial.print(g);
 Serial.println(" us");

 Serial.println("Reading from EEPROM");
 e=micros();
 pageRead(0);
 f=micros();
 g = f-e;
 Serial.print("Reading done in :");
 Serial.print(g);
 Serial.println(" us");

 //    for (int i=0; i<(2*s) ;i++)
 //    {    
 //      Serial.println(reconv[i]);
 //    }

 ByteToInvConv();

 Serial.println("Value in recreated array: ");
 for (int i=0; i<s; i++)
 {
   Serial.println(val2[i]);
 }
 Serial.println("End");

 Serial.println("Writing data");
 e=micros();

 u = 0;
 for (int i=0; i<(2*s) ;i++)
 {    
   writeData(chip1,i,val[u]>> 8);   // write MSB byte of sample to EEPROM
   i++;
   writeData(chip1,i,val[u]& 0xFF); // write LSB byte of sample to EEPROM
   u++;  
 }

 f=micros();
 g = f-e;
 Serial.print("Writing time :");
 Serial.print(g);
 Serial.println(" us");

 Serial.println("Reading data...");
 e=micros();
 u = 0;

 // reading data from EEPROM
 for (int i=0; i<(2*s) ;i++)
 {
   a = readData(chip1,i);
   a = a<<8;
   i++;
   a += readData(chip1,i);
   d[u] = a;
   u++;
 }

 f=micros();
 g = f-e;

 Serial.print("Reading time ");
 Serial.print(g);
 Serial.println(" us");

}
void loop()
{

Any advice or comment on code would be great

AmbiLobe

1024 bytes taking 600000 us is very slow 600us per byte
The expected access time is 900ns .
That is 600 times faster than you report.

http://www.mouser.com/ProductDetail/Microchip-Technology/24LC256-E-SM/?qs=Lo1urCY4z7to33SsbwG5QA==

My first area in your software that I suspect is called Object Oriented Programming (OOP)

You are using OOPs that includes hidden bloated facilities.  The overhead of OOPs is unpredictable and erratic. If you need precision software, do not use OOPs, use assembly language or a procedural language like C. Objects in OOP contain vast resources for making things easy and slow.
I am going to get going.

Go Up