Arduino - Error erasing EEPROM memory in MEGA

2

I'm new to Arduino and I'm having a problem with Arduino ... I have 2 plates. One is an Arduino UNO and another is an Arduino Mega. I run the following program in both, that the only thing it does is clean the EEPROM memory and show how it was. The idea is that the output should always be 0 because the memory is erasing. I put the code and the different outputs.

#include <EEPROM.h>

void EEPROMWriteInt(int address, int value); //2 Bytes, LE DIGO COMO GUARDAR
int EEPROMReadInt(int address);  

int32_t numberSens = 0; 
int32_t numberDESP = 0; 
int32_t numberGI = 0;
int32_t numberGD = 0;

void setup() {
  Serial.begin(9600);
  for (int nL = 0; nL < EEPROM.length(); nL++) { 
    EEPROM.write(nL, 0);
  }

  numberSens = EEPROMReadInt(1);
  numberDESP = EEPROMReadInt(3);
  numberGI = EEPROMReadInt(5);
  numberGD = EEPROMReadInt(7);

  Serial.print("numberSens ");
  Serial.println(numberSens);
  Serial.print("numberDESP ");
  Serial.println(numberDESP);
  Serial.print("numberGI ");
  Serial.println(numberGI);
  Serial.print("numberGD ");
  Serial.println(numberGD);
}

void loop() {

}

void EEPROMWriteInt(int address, int value) {
  byte hiByte = highByte(value);
  byte loByte = lowByte(value);

  EEPROM.write(address, hiByte);
  EEPROM.write(address + 1, loByte);   
}

int EEPROMReadInt(int address)
{
  byte hiByte = EEPROM.read(address);
  byte loByte = EEPROM.read(address + 1);

  return word(hiByte, loByte); 
}

Then, the output in Arduino UNO is:

numberSens 0
numberDESP 0
numberGI 0
numberGD 0

The output in Arduino MEGA is:

numberSens 4353
numberDESP 1408
numberGI 0
numberGD 256

Does anyone have an idea or a clue because this may be happening? I thank you in advance.

    
asked by Emiliano Torres 31.07.2018 в 20:35
source

2 answers

0

your example worked well in the one and bad in the mega. BUT I found the / the problems. The first problem is that the MEGA has the bytes in front (approximately 0 to 6 damaged). This I realized since the same example I do record in higher memory positions and it works correctly. The second problem was that int32_t occupies 4 bytes and in my example I'm trying to write it in 2 bytes, with which, the first number keeps it well, but from the second it starts to overlap. To solve it, I used a reading method and a writing method that uses the number of bytes according to the type of data. I leave the code in case someone serves. Thanks to everyone.

template <class T> int EEPROM_write(int ee, const T& value);
template <class T> int EEPROM_read(int ee, T& value);

template <class T> int EEPROM_write(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
          EEPROM.write(ee++, *p++);
    return i;
}

template <class T> int EEPROM_read(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
          *p++ = EEPROM.read(ee++);
    return i;
}
    
answered by 04.08.2018 / 14:10
source
1

The write () method works with bytes, if you are going to use different types of variables, such as int or even data structures, use the put () and get () methods:

#include <EEPROM.h>
const int NEXT_POS = sizeof(int) + 1; // siguiente posicion a escribir de la eeprom para valores int
const int MAX_REGISTERS = EEPROM.length()/sizeof(int); // Maximo de registros segun el tipo de variable,en este caso int
const int FIRST_POS = 0; // Primer registro eeprom
void setup() {
  Serial.begin(9600);
  int position = 0;
  for(int cont = 0;cont < MAX_REGISTERS;cont++){ // Pone un 0 en todos los registros
    EEPROM.put(position,0);
    position += NEXT_POS; 
    delay(10);
  }
  position = 0;
  int value;
   for(int cont = 0;cont < MAX_REGISTERS;cont++){ // Lectura
    EEPROM.get(position,value);
    position += NEXT_POS; 
    Serial.println(value);
    delay(10);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

I have not tried it on the mega, it should work.

    
answered by 02.08.2018 в 13:49