Skip to content

Instantly share code, notes, and snippets.

@bcelenza
Created March 31, 2016 16:03
Show Gist options
  • Save bcelenza/ca0e11b3a5e45e890dd25ef5840fd574 to your computer and use it in GitHub Desktop.
Save bcelenza/ca0e11b3a5e45e890dd25ef5840fd574 to your computer and use it in GitHub Desktop.
EEPROM Arduino
#include <Wire.h> // specify use of Wire.h library.
#define FLOAT_LENGTH 4
#define DOUBLE_LENGTH 8
#define UINT_LENGTH 2
const int EEPROM_DEVICE_ADDR = 0x50;
int loops = 0;
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // setup serial for output
float foo = 0.654321;
int ret1 = EEPROM_writeFloat(0x590, foo);
Serial.print("First write: ");
Serial.print(ret1);
Serial.print("\n");
// writes take a few ms and are non-blocking
delay(10);
float bar = 0.123456;
int ret2 = EEPROM_writeFloat(0x5b0, bar);
Serial.print("Second write: ");
Serial.print(ret2);
Serial.print("\n");
// writes take a few ms and are non-blocking
delay(10);
double baz = 1.123456789;
int ret3 = EEPROM_writeDouble(0x450, baz);
Serial.print("Third write: ");
Serial.print(ret3);
Serial.print("\n");
// writes take a few ms and are non-blocking
delay(10);
unsigned int boo = 23;
int ret4 = EEPROM_writeDouble(0x400, boo);
Serial.print("Fourth write: ");
Serial.print(ret4);
Serial.print("\n");
}
void loop()
{
Serial.print("Loop ");
Serial.print(loops);
Serial.print("\n");
float foo;
EEPROM_readFloat(0x590, &foo);
Serial.print("foo=");
Serial.print(foo, 6);
Serial.print("\n");
float bar;
EEPROM_readFloat(0x5b0, &bar);
Serial.print("bar=");
Serial.print(bar, 6);
Serial.print("\n");
if(loops == 5)
{
EEPROM_erase(0x450, DOUBLE_LENGTH);
}
double baz;
EEPROM_readDouble(0x450, &baz);
Serial.print("baz=");
Serial.print(baz, 15);
Serial.print("\n");
double boo;
EEPROM_readDouble(0x400, &boo);
Serial.print("boo=");
Serial.print(boo);
Serial.print("\n");
loops++;
delay(1000);
}
// stored types
union storedFloat {
float value;
byte bytes[FLOAT_LENGTH];
};
union storedDouble {
double value;
byte bytes[DOUBLE_LENGTH];
};
union storedUnsignedInt {
unsigned int value;
byte bytes[UINT_LENGTH];
};
int EEPROM_writeBytes(int address, byte bytes[], unsigned int writeLength)
{
// save bytes
Wire.beginTransmission(EEPROM_DEVICE_ADDR);
// send start address
Wire.write(address);
// send each byte
for(int i = 0; i < writeLength; i++)
{
Wire.write(bytes[i]);
}
return Wire.endTransmission();
}
int EEPROM_readBytes(int address, unsigned int readLength)
{
// indicate starting position
Wire.beginTransmission(EEPROM_DEVICE_ADDR);
Wire.write(address);
Wire.endTransmission();
// request bytes
return Wire.requestFrom(EEPROM_DEVICE_ADDR, readLength);
}
// EEPROM methods
int EEPROM_writeFloat(int address, float value)
{
// cast value to union structure
union storedFloat s;
s.value = value;
// write bytes
return EEPROM_writeBytes(address, s.bytes, FLOAT_LENGTH);
}
void EEPROM_readFloat(int address, float * value)
{
// create structure
union storedFloat s;
// capture bytes
int i = 0;
EEPROM_readBytes(address, FLOAT_LENGTH);
while(Wire.available())
{
s.bytes[i] = Wire.read();
i++;
}
// populate value
*value = s.value;
}
int EEPROM_writeDouble(int address, double value)
{
// cast value to union structure
union storedDouble s;
s.value = value;
// write bytes
return EEPROM_writeBytes(address, s.bytes, DOUBLE_LENGTH);
}
void EEPROM_readDouble(int address, double * value)
{
// create structure
union storedDouble s;
// capture bytes
int i = 0;
EEPROM_readBytes(address, DOUBLE_LENGTH);
while(Wire.available())
{
s.bytes[i] = Wire.read();
i++;
}
// populate value
*value = s.value;
}
int EEPROM_writeUnsignedInt(int address, unsigned int value)
{
// cast value to union structure
union storedUnsignedInt s;
s.value = value;
// write bytes
return EEPROM_writeBytes(address, s.bytes, UINT_LENGTH);
}
void EEPROM_readUnsignedInt(int address, unsigned int * value)
{
// create structure
union storedUnsignedInt s;
// capture bytes
int i = 0;
EEPROM_readBytes(address, UINT_LENGTH);
while(Wire.available())
{
s.bytes[i] = Wire.read();
i++;
}
// populate value
*value = s.value;
}
int EEPROM_erase(int address, unsigned int eraseLength)
{
byte eraseBytes[eraseLength];
for(int i = 0; i < eraseLength; i++)
{
eraseBytes[i] = 0;
}
return EEPROM_writeBytes(address, eraseBytes, eraseLength);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment