Leer y escribir EEPROM desde Arduino

Vamos a ver como leer y escribir en una EEPROM I2C utilizando las entradas analógicas de Arduino.
La EEPROM que he utilizado en el ejemplo es una 24LC256, podéis ver más información en su datasheet.

El esquema de conexiones con Arduino es el siguiente:

Una vez está conectado todo al Arduino desde el IDE copiamos el siguiente código:

    #include <Wire.h>

    /* --- START ARDUINO FUNCS ---
     * From: http://www.arduino.cc/playground/Code/I2CEEPROM
     */

    void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
      int rdata = data;
      Wire.beginTransmission(deviceaddress);
      Wire.write((int)(eeaddress >> 8)); // MSB
      Wire.write((int)(eeaddress & 0xFF)); // LSB
      Wire.write(rdata);
      Wire.endTransmission();
    }

    // WARNING: address is a page address, 6-bit end will wrap around
    // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
    void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
      Wire.beginTransmission(deviceaddress);
      Wire.write((int)(eeaddresspage >> 8)); // MSB
      Wire.write((int)(eeaddresspage & 0xFF)); // LSB
      byte c;
      for ( c = 0; c < length; c++)
            Wire.write(data[c]);
      Wire.endTransmission();
    }

    byte i2c_eeprom_read_byte( 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;
    }

    // maybe let's not read more than 30 or 32 bytes at a time!
    void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
      Wire.beginTransmission(deviceaddress);
      Wire.write((int)(eeaddress >> 8)); // MSB
      Wire.write((int)(eeaddress & 0xFF)); // LSB
      Wire.endTransmission();
      Wire.requestFrom(deviceaddress,length);
      int c = 0;
      for ( c = 0; c < length; c++ )
            if (Wire.available()) buffer[c] = Wire.read();
    }

    /* --- END ARDUINO FUNCS --- */

    void writeString() {
      char cadena[] = "HOLA NENITAS :-)";
      i2c_eeprom_write_page(0x50, 0, (byte *)cadena, sizeof(cadena));
      delay(10);
    }

    void setup() {
      Wire.begin();
      Serial.begin(9600);
    }

    void loop() {
      int addr = 0;
      char b   = 'X';

      Serial.println("### READ EEPROM ###");

      while (b != 0) {
            b = i2c_eeprom_read_byte(0x50, addr);
            Serial.print((char)b);
            addr++;
      }

      Serial.print("\n### EOF ###\n");

      delay(2000);
    }

Veremos los datos de la EEPROM en ASCII por la salida serie a 9600 baudios. El programa, como prueba de concepto dejará de leer cuando encuentre un nulo, con lo que para hacer dumps de EEPROM es mejor meter un contador por cada byte leído y cortar el bucle cuando se alcance el límite de la memoria.

Si la EEPROM está virgen, puede llamarse a writeString() desde setup :-)

Salu2.