/*
  Reading and writing data to 24C08 EEPROM over I2C
  CC0 
  
  Datasheet for 24C08 
HTTP 301 This page has been moved
  
  WIRING
            __ __
      PRE -|     |- VCC
      NC  -|     |- MODE
      E   -|     |- SCL
      VSS -|_____|- SDA
          
      PRE  = Write Protect              -> GROUND
      NC   = Not connected       
      E    = Chip Enable Input          -> first EEPROM -> GROUND for I2C addresses 0x50,0x51,0x52,0x53; second EEPROM -> 5V for I2C addresses 0x54,0x55,0x56,0x57
      VSS  = Ground                     -> GROUND
      VCC  = Supply Voltage             -> 5V
      MODE = Multibyte/Page Write Mode  -> GROUND (not used here)
      SCL  = Serial Clock               -> Analog PIN 5 (Uno)
      SDA  = Serial Data Address IN/OUT -> Analog PIN 4 (Uno)
  
  
*/
#include <Wire.h>     // for I2C
// variable to receive data coming from the EERPROMs
byte data_received=0; 
// eeprom address array, use I2C scanner sketch to find the addresses. This array contains 2 chips with 4 device addresses (like virtual chips) each
byte chip[] = {0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57}; 
void setup()
{
  Serial.begin(9600); // for screen output
  Wire.begin();       // waking up I2C
  delay(1000);
  Serial.println("Setup finished");
}
void writeData(int device_address, byte memory_address, byte data) 
// write one byte of data 'data' to eeprom memory address 'memory_address' to chip with I2C address 'device_address'
{
  Wire.beginTransmission(device_address);  // device address
  Wire.write(memory_address );             // memory address
  Wire.write(data);                        // data to send
  Wire.endTransmission();                  // end
  delay(10);
}
byte readData(int device_address, byte memory_address) 
// reads one byte of data from memory location 'memory_address' in chip at I2C address 'device_address' 
{
  byte result;  // return value
  Wire.beginTransmission(device_address); // device address
  Wire.write(memory_address);             // memory address
  Wire.endTransmission();                 // end
  Wire.requestFrom(device_address,1);     // get one byte of data from device
  if (Wire.available()) 
    result = Wire.read();
  return result;                          // return the read data byte
  delay(10);
}
void loop()
{
  // WRITING DATA
  int ascii_counter = 33;        // to see something we start with the first readable character in the ASCII table which is 33 == '!'
  Serial.println();
  Serial.print("Writing data ");
  Serial.println();
  for (int a=0; a<8; a++)                                    // loop to go through the address array of the eeprom i.e. chip[a]
  {
    for (int b=0; b<256;b++)                                 // each chip has one byte of addressable memory
    {
      writeData(chip[a],b,ascii_counter);                    // writing the value ascii_counter to chip chip[a] at address b
      Serial.print("chip :");                                // let's see what it did
      Serial.print(chip[a], HEX);
      Serial.print(" address :");
      Serial.print(b);
      Serial.print(" value :");
      Serial.println(char(ascii_counter));
      ascii_counter++;                                       // next character of ASCII table
      if (ascii_counter > 126) {ascii_counter = 33;};        // ASCII characters greater 126 are not visible, go back to the first readable character at 33
    }
    Serial.println();
  }
  delay(10000);                                              // let's wait some seconds
  // READING DATA
    
  Serial.println("Reading data...");
  for (int a=0; a<8; a++)                                    // same as above, we toogle through the 8 chips ....
  {
    Serial.print("Chip: ");
    Serial.println(chip[a],HEX);
    for (int b=0; b<256;b++)                                 // and all 256 addresses in each (virtual) chip...
    {
      data_received=readData(chip[a],b);                     // and read the data there....
      Serial.print(char(data_received));                     // and print it to serial....
    }
    Serial.println();                                        // new line for each chip
  }
// since the chip has only a limited number of write cycles, we don't want to write again and again. Let's end the sketch
  do                                                         
  {
    delay(10000);
  } while ( 1 != 0);
}