I'm using an Arduino Uno to read an external 24lc256 eeprom. This program always shows me zeros in every location no matter what i write to the eeprom.
#include <Wire.h>
const byte DEVADDR = 0x50;
void setup() {
Wire.begin();
Serial.begin(9600);
Serial.flush();
delay(3000);
// Now dump 256 bytes
Serial.println("eeprom_dump(DEVADDR, 0, 256)");
eeprom_dump(DEVADDR, 0, 256);
Serial.println();
}
void loop() {
}
void eeprom_dump(byte devaddr, unsigned addr, unsigned length)
{
// Start with the beginning of 16-bit page that contains the first byte
unsigned startaddr = addr & (~0x0f);
// stopaddr is address of next page after the last byte
unsigned stopaddr = (addr + length + 0x0f) & (~0x0f);
for (unsigned i = startaddr; i < stopaddr; i += 16) {
byte buffer[16]; // Hold a page of EEPROM
char outbuf[6]; //Room for three hex digits and ':' and ' ' and '\0'
sprintf(outbuf, "%03x: ", i);
Serial.print(outbuf);
eeprom_read_buffer(devaddr, i, buffer, 16);
for (int j = 0; j < 16; j++) {
if (j == 8) {
Serial.print(" ");
}
sprintf(outbuf, "%02x ", buffer[j]);
Serial.print(outbuf);
}
Serial.print(" |");
for (int j = 0; j < 16; j++) {
if (isprint(buffer[j])) {
Serial.print(buffer[j]);
}
else {
Serial.print('.');
}
}
Serial.println("|");
}
}
int eeprom_read_buffer(byte deviceaddr, unsigned eeaddr,
byte * buffer, byte length)
{
// Three lsb of Device address byte are bits 8-10 of eeaddress
byte devaddr = deviceaddr | ((eeaddr >> 8) & 0x07);
byte addr = eeaddr;
Wire.beginTransmission(devaddr);
Wire.write(int(addr));
Wire.endTransmission();
Wire.requestFrom(devaddr, length);
int i;
for (i = 0; i < length && Wire.available(); i++) {
buffer[i] = Wire.read();
}
return i;
}