FRAM Read/Write INT(16bit)

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.

an int is 2 bytes so you need to read two consecutive addresses and merge them

Wire.requestFrom(DEVICEADDRESS, 2); // forgot the 2

robtillaart:
an int is 2 bytes so you need to read two consecutive addresses and merge them

Wire.requestFrom(DEVICEADDRESS, 2); // forgot the 2

The final code is above. Happy to receive some comments for improvements.

Hi casemod,

I bought a FRAM module on ebay , it is based on a FM24CL16B chip and is 16 kilobits ( 2 K bytes obviously )

After trying 3 or 4 FRAM 'libraries', including a failed attempt at the supplied code (was C code, but not 'IDE'), I found your code and it works!!

May I please ask you how to extend your code above 256 bytes to 2048 bytes, if its a quick fix?

Thanks

Hi, if i d like to write 32 byte value?
Can someone help me?

Thank You