I'm trying to write to an EEPROM a number and then retrieve it.
I'm seeing some strange behavior.
When reading the value I get 255, but I was trying to write the number 12.
This was meant to be a small example of what I ultimately want to do, but it doens't seem to work correctly. I cannot see where the problem is with my code.
(My end goal is to be able to write in location 0 and 1 a "program" length [greater than 255] and then from 2-on write values between 0-255..but I need to be able to successfully write and retrieve first!)
Thanks for taking the time to review this!
#include <Wire.h>
#define disk1 0x50 //Address of 24LC256 eeprom chip
int runOnce = 0;
void setup(void)
{
Serial.begin(9600);
if (!Serial)
{
}
Wire.begin();
}
void loop() {
unsigned int address = 0;
if (runOnce == 0) {
//write to disk1 (eeprom addr), address (mem location on EEPROM), data
writeEEPROM(disk1, address, byte(12));
delay(15);
Serial.print(readEEPROM(disk1, address), DEC);
runOnce = 1;
}
}
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data )
{
Serial.println("in write eeprom \n");
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(data);
Wire.endTransmission();
delay(5);
}
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;
}