Code to read/Write an int from an external EEPROM/FRAM
This one works with a 4Kb FRAM, therefore the addressing is only 8 bits.
Possible to use with larger memories by adding the second address bit.
#include <Wire.h>
#define DEVICEADDRESS 0x57
//int temp = 0;
uint16_t temp = 0;
uint16_t test = 0b1111100101011000;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
}
void loop() {
EepromWrite(0x05, test);
temp = EepromRead(0x05);
Serial.println(temp, BIN);
//Serial.println(temp1, BIN);
delay(2000);
}
void EepromWrite(uint8_t theMemoryAddress, uint16_t u16Byte)
{
uint8_t lowByte = ((u16Byte) & 0xFF);
Serial.println(lowByte);
uint8_t highByte = ((u16Byte >> 8) & 0xFF);
Serial.println(highByte);
Wire.beginTransmission(DEVICEADDRESS);
Wire.write(theMemoryAddress);
Wire.write(lowByte);
Wire.write(highByte);
Wire.endTransmission();
}
// ----------------------------------------------------------------
uint16_t EepromRead(uint8_t theMemoryAddress)
{
Wire.beginTransmission(DEVICEADDRESS);
Wire.write(theMemoryAddress);
Wire.endTransmission();
Wire.requestFrom(DEVICEADDRESS,2);
uint8_t lowByte = Wire.read();
uint8_t highByte = Wire.read();
return ((lowByte) & 0xFF) + ((highByte << 8) & 0xFF00);
}
Tested compiled on arduino 1.5.6.
For EEPROM memories ADD delays for write.