Basic write and read EEPROM?

I played few days with EEPROM AT24C256.

On I2C-Arduino UNO. To learn, I am using the code bellow (it does compile), found on the internet and slightly modified for easy learning.

I found the address in 2B (up to 512 kb chips) and the data in the sketch bellow is 2B.

Questions are:

a) can I mix floating variables and integers on the same chip (meaning, variable length of data field from 2 to 4 B)?

b) is there any other possibility to run write/read EEPROMs from outside the setup section?

Thank you!

[I could not reach this stage of my learning process without kind assistance from this forum members! Thank you!]

#include <Wire.h>    
 
#define disk1 0x50    //Address of 24LC256 eeprom chip
 
void setup(void)
{
  Serial.begin(9600);
  Wire.begin();  
  byte r= 3;
  byte result=0;
  unsigned int address = 0;
  writeEEPROM(disk1, address, r);
  result = readEEPROM(disk1, address);
  Serial.print(result, DEC);
  address=1;
  r=5;
  writeEEPROM(disk1, address, r);
  result = readEEPROM(disk1, address);
  Serial.print(result, DEC);
  }
 
void loop(){}
 
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) 
{
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.write(data);
  Wire.endTransmission();
 }
 
byte readEEPROM(int deviceaddress, unsigned int eeaddress) 
{
  byte rdata = 0xFF;
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();
  Wire.requestFrom(deviceaddress,1);
  if (Wire.available()) rdata = Wire.read();
  return rdata;
}

a) can I mix floating variables and integers on the same chip (meaning, variable length of data field from 2 to 4 B)?

Yes, as long as you keep track of whether a given location, or series of locations, stores a byte, a char, an int, or a float.

b) is there any other possibility to run write/read EEPROMs from outside the setup section?

As long as the code is in a function, it doesn't matter which function it is in. You can read from and write to an EEPROM from loop() as easily as in setup().

falexandru:
I played few days with EEPROM AT24C256.

Modul EEPROM AT24C256

On I2C-Arduino UNO. To learn, I am using the code bellow (it does compile), found on the internet and slightly modified for easy learning.

I found the address in 2B (up to 512 kb chips) and the data in the sketch bellow is 2B.

Questions are:

a) can I mix floating variables and integers on the same chip (meaning, variable length of data field from 2 to 4 B)?

b) is there any other possibility to run write/read EEPROMs from outside the setup section?

Thank you!

The 24C256 has a 64 byte write page, as long as the data you send to the chip does not cross one of the 64 byte boundary, it will work fine.

But, if you try to store a 4 byte variable at address 126 (0x7e) it will not do what you expect.

unsigned long bigVar = 0x12345678

Wire.beginTransmission(eepromchip);
Wire.write(0); // high byte of address
Wire.write(126); // low byte
unsigned long tempBig=bigVar;
for(uint8_t i=0;i<4;i++){
  uint8_t data = tempBig & 0xff;
  tempBig = tempBig >>8; // shift right  
  Wire.write(data);
  }
Wire.endTransmission();
the following is what you expected to store

address  content
0x007E  0x78
0x007F  0x56
0x0080  0x34
0x0081  0x12

Actually content of Memory Chip

0x0040  0x34
0x0041  0x12
...
0x007E 0x78
0x007F 0x12

When the EEPROM updates its internal memory cell array it copies the current data from the selected 64 byte 'page' into the write buffer, then it merges the data you send it into this buffer, then erases the 64byte page of the eeprom array, then write a copy of the data in the write buffer onto the eeprom array.

when you send the EEPROM address (2bytes) that selects which 64byte page you are going to change. If you send enough bytes of data such that the internal address pointer increments past a 64 byte boundary, the write buffer pointer starts back at zero.

You have to handle any page overflow during write commands in your program.

Read command do not encounter this 64byte boundary. The read address Pointer only overflows at the end (maximum address for a 24c256 is 0x7FFF, 32k).

Chuck.