#include <Wire.h>
#include <EEPROM.h>
unsigned int EEPROMBank = 0x50; // Base address of 1st EEPROM chip
unsigned int EEPROMAddress = 0; // Sets the start EEPROM address
unsigned int EEPROMValue = 0; // EEPROM value
byte Dummy;
// EEPROM list taken from:
//
http://vascoferraz.com/projects/24c-eeprom-check/
// Sets the Maximum EEPROM address, uncomment accordingly:
//#define MaxBankAddress = 128 - 1; //24C01 -> 1024 bit -> 128 byte
//#define MaxBankAddress = 256 - 1; //24C02 -> 2048 bit -> 256 byte
//#define MaxBankAddress = 512 - 1; //24C04 -> 4096 bit -> 512 byte
unsigned int MaxBankAddress = 1024 - 1; //24C08 -> 8192 bit -> 1024 byte
//#define MaxBankAddress = 2048 - 1; //24C16 -> 16384 bit -> 2048 byte
//#define MaxBankAddress = 4096 - 1; //24C32 -> 32768 bit -> 4096 byte
//#define MaxBankAddress = 8192 - 1; //24C64 -> 65536 bit -> 8192 byte
//#define MaxBankAddress = 16384 - 1; //24C128 -> 131072 bit -> 16384 byte
//#define MaxBankAddress = 32768 - 1; //24C256 -> 262144 bit -> 32768 byte
//#define unsigned int maxaddress = 65536 - 1; //24C512 -> 524288 bit -> 65536 byte
#define SCLPin D1 // I2C SCL Pin
#define SDAPin D2 // I2C SDA Pin
void setup() {
Wire.begin(SDAPin, SCLPin); // Initialize 2-Wire bus
pinMode(D1, INPUT_PULLUP); // I Need to validate this line
pinMode(D2, INPUT_PULLUP); // I Need to validate this line
Serial.begin(19200); // Initialize serial port
while(!Serial); // wait for serial
delay(3000); // Waits 3 seconds
Serial.println("\n============================="); // Sends preamble and Cr+Lf
EEPROMAddress = 0; // EEPROM word variable
EEPROMValue = 120; // EEPROM value
Serial.print("EEPROMAddress: ");
Serial.print(EEPROMAddress);
Serial.print(", EEPROMValue: ");
Serial.print(EEPROMValue);
Serial.print(", EEPROMBank: 0x");
Serial.println(EEPROMBank,HEX);
writeEEPROM(EEPROMBank, EEPROMAddress, byte(EEPROMValue));
for (EEPROMAddress = 0; EEPROMAddress < 3; EEPROMAddress++) {
Serial.print("EEPROMAddress: ");
Serial.print(EEPROMAddress);
Serial.print(", Value: ");
Serial.println(readEEPROM(EEPROMBank, EEPROMAddress), 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((int)(eeaddress)); //only for 8 bit addressing
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.write((int)(eeaddress)); //only for 8 bit addressing
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}