Pages: [1]   Go Down
Author Topic: Can't read 24lc256 eeprom  (Read 448 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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




« Last Edit: May 07, 2013, 02:46:15 pm by mepowers601 » Logged

Offline Offline
Edison Member
*
Karma: 31
Posts: 1417
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do this first How to post code properly

Pete
Logged

Where are the Nick Gammons of yesteryear?

Pages: [1]   Go Up
Jump to: