Go Down

Topic: Can't read 24lc256 eeprom (Read 629 times) previous topic - next topic

May 07, 2013, 05:54 pm Last Edit: May 07, 2013, 09:46 pm by mepowers601 Reason: 1
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.

Code: [Select]


#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;
}







Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy