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;
}