Implementation of an eeprom integer read / write

Good afternoon,

I made two functions to read and write a 2 byte integer to and from the arduino’s eeprom.

Cheers,

Marc

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

//This function will write a 2 byte integer to the eeprom at the specified address and address + 1
void EEPROMWriteInt(int p_address, int p_value)
      {
      byte lowByte = ((p_value >> 0) & 0xFF);
      byte highByte = ((p_value >> 8) & 0xFF);

      EEPROM.write(p_address, lowByte);
      EEPROM.write(p_address + 1, highByte);
      }

//This function will read a 2 byte integer from the eeprom at the specified address and address + 1
unsigned int EEPROMReadInt(int p_address)
      {
      byte lowByte = EEPROM.read(p_address);
      byte highByte = EEPROM.read(p_address + 1);

      return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
      }

void setup()
      {
      Serial.begin(9600);
      
      EEPROMWriteInt(0, 0xABCD);
      
      Serial.print("Read the following int at the eeprom address 0: ");
      Serial.println(EEPROMReadInt(0), HEX);
      }

void loop()
      {
      }

Thanks, exactly what I was looking for.

Actually these functions already exist. The issue is that Arduino EEProm library wraps <avr/eeprom.h> very poor. Instead of using the Arduino library you can go directly for the superior <avr/eeprom.h>. Then you get this functionality for free.

Udo