Author Topic: EEPROM clearing between resets  (Read 15643 times)

mikkelg

  • Newbie
  • *
  • Posts: 10
EEPROM clearing between resets
« on: April 03, 2014, 02:11:17 am »
Hey everyone

I posted this in the projects section under the EEPROM library, but thought it might belong more in here.
I want to use the EEPROM to store the settings in my project so that the last used settings can be restored even if power is interrupted and the sketch restarts.
I am having no problem using the library to read and write values to the EEPROM, but the problem arises when the arduino is reset or restarted... then the previous values seem to be erased and replaced by random numbers.
Is there something i am not aware of with the EEPROM that i need to do before values will be stored indefinetely? Like a jumper that needs to be set or some settings in the sketch?

gogol

  • Sr. Member
  • ****
  • Posts: 398
Re: EEPROM clearing between resets
« Reply #1 on: April 03, 2014, 04:30:33 am »
You should post some example code, how do you store information into the EEPROM, and how do you retrieve it.

For me the EEPROM works just es expected.  I am able to power-off the Digix, to reset it, to erase the builtin flash and reprogram, all without losing once stored information.

Example:  Store this sketch as STORE-EEPROM
Code: [Select]
#include <Wire.h>
#include <Extensive_EEPROM.h>

const int EEPROM_addr = 0x50;


void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);
  while(!Serial.available()){
    Serial.println("Enter any key to begin EEPROM test");
    delay(1000);
  }
  Serial.println("//byte-wise writing");
  for(int i = 0; i < 32; i++)  {
      extEEPROMwrite(EEPROM_addr, i, i);
      Serial.print("write data= ");
      Serial.println(i);
 }
 Serial.println("Writing Bytes to EEPROM finished!");

}

void loop()
{
     
}

Example:  Store this sketch as READ-EEPROM
Quote
#include <Wire.h>
#include <Extensive_EEPROM.h>

const int EEPROM_addr = 0x50;


void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);
  while(!Serial.available()){
    Serial.println("Enter any key to begin EEPROM test");
    delay(1000);
  }
  Serial.println("//byte-wise reading");
  int data_back;
  for(int i = 0; i < 32; i++)   {
        data_back = extEEPROMread(EEPROM_addr, i); //byte extEEPROMread(int chip_address, int address);
        Serial.print("expected data= ");
        Serial.print(i);
        Serial.print(" read_back= ");
        Serial.println(data_back);
  }
        Serial.println("Read back test finished!");
}

void loop()
{
     
}

The first of those scripts just stores the values 0-31 into the first 32 bytes of the EEPROM.

After the program has run, you can unplug the DigiX, reset it, and do what you think.

After that, store the second sketch to the DigiX, and you will see, that it reads exactly the expected values back.

Furthermore:  You should post questions like this to the "DigiX Libraries" section, with an reference to "Extensive_EEPROM" in the title. 

Perhaps digistump can move this thread ?  done 2014-04-04

« Last Edit: April 03, 2014, 10:57:13 pm by gogol »

mikkelg

  • Newbie
  • *
  • Posts: 10
Re: EEPROM clearing between resets
« Reply #2 on: April 04, 2014, 07:02:10 am »
thanks for your reply, and sorry i put the topic in the wrong forum :)

I was more interested in if there was something obvious i was missing. I tried your 2 code examples and the work like a charm.
I noticed my code was slightly different than your since i am storing ints instead of bytes (need bigger numbers for the application). Then i tried changing the code to do the exact same thing, except for changing the data type and using extEEPROMwriteInt and extEEPROMreadInt instead. Then i saw a very different value read back from EEPROM compared to expected values. I wrote data using:

Code: [Select]
#include <Wire.h>
#include <Extensive_EEPROM.h>

const int EEPROM_addr = 0x50;


void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);
  while(!Serial.available()){
    Serial.println("Enter any key to begin EEPROM test");
    delay(1000);
  }
  Serial.println("//byte-wise writing");
  for(int i = 0; i < 32; i++)  {
      extEEPROMwriteInt(EEPROM_addr, i, i);
      Serial.print("write data= ");
      Serial.println(i);
 }
 Serial.println("Writing Bytes to EEPROM finished!");

}

void loop()
{
     
}

and i read them back using:

Code: [Select]
#include <Wire.h>
#include <Extensive_EEPROM.h>

const int EEPROM_addr = 0x50;


void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);
  while(!Serial.available()){
    Serial.println("Enter any key to begin EEPROM test");
    delay(1000);
  }
  Serial.println("//byte-wise reading");
  int data_back;
  for(int i = 0; i < 32; i++)   {
        data_back = extEEPROMreadInt(EEPROM_addr, i); //byte extEEPROMread(int chip_address, int address);
        Serial.print("expected data= ");
        Serial.print(i);
        Serial.print(" read_back= ");
        Serial.println(data_back);
  }
        Serial.println("Read back test finished!");
}

void loop()
{
     
}

Basically the exact same sketches as you just posted, but with the 2 functions changed to writing ints instead of bytes. But the output when doing this is very different:

Code: [Select]
Enter any key to begin EEPROM test
Enter any key to begin EEPROM test
//byte-wise reading
expected data= 0 read_back= 50331648
expected data= 1 read_back= 67305472
expected data= 2 read_back= 84148992
expected data= 3 read_back= 100992003
expected data= 4 read_back= 117835012
expected data= 5 read_back= 134678021
expected data= 6 read_back= 151521030
expected data= 7 read_back= 168364039
expected data= 8 read_back= 185207048
expected data= 9 read_back= 202050057
expected data= 10 read_back= 218893066
expected data= 11 read_back= 235736075
expected data= 12 read_back= 252579084
expected data= 13 read_back= 269422093
expected data= 14 read_back= 286265102
expected data= 15 read_back= 303108111
expected data= 16 read_back= 319951120
expected data= 17 read_back= 336794129
expected data= 18 read_back= 353637138
expected data= 19 read_back= 370480147
expected data= 20 read_back= 387323156
expected data= 21 read_back= 404166165
expected data= 22 read_back= 421009174
expected data= 23 read_back= 437852183
expected data= 24 read_back= 454695192
expected data= 25 read_back= 471538201
expected data= 26 read_back= 488381210
expected data= 27 read_back= 505224219
expected data= 28 read_back= 522067228
expected data= 29 read_back= 2039325
expected data= 30 read_back= 7966
expected data= 31 read_back= 285212703
Read back test finished!

I tried writing and reading several times and the error is somewhat consistent: If i power off the DigiX and then power it back on, the values read are the same (although still not the right values).
However if i run the first sketch again to re-write the same values to the EEPROM, i get completely new, random values.

What could cause such a large deviation from to occur? Is there something i am missing regarding the datatype? I dont see what it would be though since it is ints being passed to the function.

Any ideas would be greatly appreciated :)

gogol

  • Sr. Member
  • ****
  • Posts: 398
Re: EEPROM clearing between resets
« Reply #3 on: April 04, 2014, 09:52:21 am »
Its just an address problem. You write never the right data to the EEPROM.

Unfortunately my example was not so helpful, as I just pasted parts from the example where only the first byte of an int was written.

As you are writing true ints, which are 4 byte big on the ARM, you need to use:

Code: [Select]
extEEPROMwriteInt(EEPROM_addr, i*sizeof(int), i);
Code: [Select]
data_back = extEEPROMreadInt(EEPROM_addr, i*sizeof(int));
and you are set!


mikkelg

  • Newbie
  • *
  • Posts: 10
Re: EEPROM clearing between resets
« Reply #4 on: April 06, 2014, 07:29:07 am »
Thanks a lot... i had a feeling it was something really simple that i was unaware of :D

Just tested the previous code with you modifications and now it works perfectly... the values are retained in the EEPROM between programmings, resets and power off/on.

thanks for the help, i really appreciate it :)

rholt

  • Newbie
  • *
  • Posts: 35
Re: EEPROM clearing between resets
« Reply #5 on: July 25, 2014, 04:25:53 pm »
This helped me a lot. I've been trying for sometime now, without success, to get my MMA7455 Accelerometer to give me data. works on the Arduino Mega I have, but so far not so much on the Digix. The Wire.h error i get back is error=1. something to do with data overflow and now i believe i can move forward with my program utilizing information you gave here.
(not to mention i now have working sketches on how to read & write to & from the EEPROM!)

thanks,
Russ from Coral Springs, Fl.