EEPROM Read Write Long

Reading and Writing long numbers to EEPROM


  • EEPROM: it's an older technology to implement rewritable non-volatile memory. It's normally used to store settings and other parameters between resets.
    This memory supports at least 100,000 writes. You can use the EEPROM library in the stock Arduino library to read from and write to this memory.


EEPROMWritelong()

Description
Cut a long variable into four bytes variables and store into the Eeprom memory.

Syntax
EEPROMWritelong(address,variable)

Parameters
address: The address value (can be a int or long).
variable: The variable do you want to save (can be int or long).

Returns
Nothing.

Code

//This function will write a 4 byte (32bit) long to the eeprom at
//the specified address to adress + 3.
void EEPROMWritelong(int address, long value)
      {
      //Decomposition from a long to 4 bytes by using bitshift.
      //One = Most significant -> Four = Least significant byte
      byte four = (value & 0xFF);
      byte three = ((value >> 8) & 0xFF);
      byte two = ((value >> 16) & 0xFF);
      byte one = ((value >> 24) & 0xFF);

      //Write the 4 bytes into the eeprom memory.
      EEPROM.write(address, four);
      EEPROM.write(address + 1, three);
      EEPROM.write(address + 2, two);
      EEPROM.write(address + 3, one);
      }


EEPROMReadlong()

Description
This function will take and assembly 4 byte of the Eeprom memory in order to form a long variable.

Syntax
EEPROMReadlong(address)

Parameters
address: The address value (can be a int or long).

Returns
A long variable.

Code

long EEPROMReadlong(long address)
      {
      //Read the 4 bytes from the eeprom memory.
      long four = EEPROM.read(address);
      long three = EEPROM.read(address + 1);
      long two = EEPROM.read(address + 2);
      long one = EEPROM.read(address + 3);

      //Return the recomposed long by using bitshift.
      return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
      }


Example:


/////////////////////////////////////////////////////////////////
// Created by Kevin Elsenberger                                //
// June 2, 2013                                                //
// elsenberger.k at gmail.com                                   //
/////////////////////////////////////////////////////////////////

//Needed to access the eeprom read write functions
#include <EEPROM.h>

long number1 = 123456789;
long number2 = 987654321;

//This function will write a 4 byte (32bit) long to the eeprom at
//the specified address to adress + 3.
void EEPROMWritelong(int address, long value)
      {
      //Decomposition from a long to 4 bytes by using bitshift.
      //One = Most significant -> Four = Least significant byte
      byte four = (value & 0xFF);
      byte three = ((value >> 8) & 0xFF);
      byte two = ((value >> 16) & 0xFF);
      byte one = ((value >> 24) & 0xFF);

      //Write the 4 bytes into the eeprom memory.
      EEPROM.write(address, four);
      EEPROM.write(address + 1, three);
      EEPROM.write(address + 2, two);
      EEPROM.write(address + 3, one);
      }

//This function will return a 4 byte (32bit) long from the eeprom
//at the specified address to adress + 3.
long EEPROMReadlong(long address)
      {
      //Read the 4 bytes from the eeprom memory.
      long four = EEPROM.read(address);
      long three = EEPROM.read(address + 1);
      long two = EEPROM.read(address + 2);
      long one = EEPROM.read(address + 3);

      //Return the recomposed long by using bitshift.
      return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
      }

void setup()
      {
      Serial.begin(9600);
      }

void loop()
      {
      //Adding a delay in order to have the time to open the
      //Arduino serial monitor.
      delay(5000);

      //Starting at the first byte on the eeprom.
      long address=0;

      //Writing first long.
      EEPROMWritelong(address, number1);
      address+=4;
      //Writing second long.
      EEPROMWritelong(address, number1);
      address+=4;


      Serial.println("If numbers are equals, it's working !");
      Serial.print(number1);
      Serial.print(" and ");
      //Readind and sending first long.
      Serial.println(EEPROMReadlong(0));

      Serial.print(number2);
      Serial.print(" and ");
      //Readind and sending second long.
      Serial.println(EEPROMReadlong(4));
      }
 

Share