external eeprom writing issue

Members

I am trying to write zero to the entire page but get some numbers in between when its read back.

/*eeprom_LongStrings
 * write across pages
   http://www.hobbytronics.co.uk/eeprom-page-write
   DUE + one 24LC256
*/

#include <Wire.h>      //use SCL & SDA
extern TwoWire Wire1;  //use SCL1 & SDA1
#define disk1 0x50     //Address of 24LC256 eeprom chip
#define WRITE_CNT 285   //how many writes

unsigned char rdata[32];

void setup(void)
{
  Serial.begin(9600);
  Wire.begin();
  Wire1.begin();        // join i2c bus
  unsigned int i;

  // define large string of data to be written
  char str_data[] = {"0"};

  // Work out length of data
  char str_len = 0;
  do {
    str_len++;
  } while (str_data[str_len]);

  // Write out data several times consecutively starting at address 0
  for (i = 0; i < WRITE_CNT; i++) writeEEPROM(disk1, i * str_len, str_data);
  //write once only
  //writeEEPROM(disk1, i * str_len, str_data);
  
  // read back the data 28 bytes at a time
  // reading data doesn't suffer from the page boundary rules
  //Serial.println("DATA READ");
  for (i = 0; i < 10; i++) {
    readEEPROM(disk1, (i * 28), rdata, 28);
    Serial.write(rdata, 28);
    Serial.print(*rdata);
  }
}

void loop() {
}

void writeEEPROM(int deviceaddress, unsigned int eeaddress, char* data) {
  // Uses Page Write for 24LC256
  // Allows for 64 byte page boundary
  // Splits string into max 16 byte writes
  unsigned char i = 0, counter = 0;
  unsigned int  address;
  unsigned int  page_space;
  unsigned int  page = 0;
  unsigned int  num_writes;
  unsigned int  data_len = 0;
  unsigned char first_write_size;
  unsigned char last_write_size;
  unsigned char write_size;

  // Calculate length of data
  do {
    data_len++;
  } while (data[data_len]);

  // Calculate space available in first page
  page_space = int(((eeaddress / 64) + 1) * 64) - eeaddress;

  // Calculate first write size
  if (page_space > 16) {
    first_write_size = page_space - ((page_space / 16) * 16);
    if (first_write_size == 0) first_write_size = 16;
  }
  else
    first_write_size = page_space;

  // calculate size of last write
  if (data_len > first_write_size)
    last_write_size = (data_len - first_write_size) % 16;

  // Calculate how many writes we need
  if (data_len > first_write_size)
    num_writes = ((data_len - first_write_size) / 16) + 2;
  else
    num_writes = 1;

  i = 0;
  address = eeaddress;
  for (page = 0; page < num_writes; page++)
  {
    if (page == 0) write_size = first_write_size;
    else if (page == (num_writes - 1)) write_size = last_write_size;
    else write_size = 16;

    Wire1.beginTransmission(deviceaddress);
    Wire1.write((int)((address) >> 8));   // MSB
    Wire1.write((int)((address) & 0xFF)); // LSB
    counter = 0;
    do {
      Wire1.write((byte) data[i]);
      i++;
      counter++;
    } while ((data[i]) && (counter < write_size));
    Wire1.endTransmission();
    address += write_size; // Increment address for next write

    delay(100);  // needs =>9ms for page write
  }
}

void readEEPROM(int deviceaddress, unsigned int eeaddress,
                unsigned char* data, unsigned int num_chars)
{
  unsigned char i = 0;
  Wire1.beginTransmission(deviceaddress);
  Wire1.write((int)(eeaddress >> 8));   // MSB
  Wire1.write((int)(eeaddress & 0xFF)); // LSB
  Wire1.endTransmission();

  Wire1.requestFrom(deviceaddress, num_chars);

  while (Wire1.available()) data[i++] = Wire1.read();
}

Any ideas on how I can fix this please?

Thanks

So every 28 prints of 0, you get an 48, which is of course the decimal value of the ASCII for '0'

I wonder ?

srnet:
So every 28 prints of 0, you get an 48, which is of course the decimal value of the ASCII for '0'

I wonder ?

I saw the pattern. And according to http://www.asciitable.com/ you are correct. My next question is how can I get rid of it?

What cause this?

 char str_data[] = {"0"};

The double quotes make this a 2 element character array with a null terminator.

For a single char use single quotes

 char str_data[] = {'0'};

Is this the bit of code that print the values to the serial monitor;

for (i = 0; i < 10; i++) {
readEEPROM(disk1, (i * 28), rdata, 28);
Serial.write(rdata, 28);
Serial.print(*rdata);
}

srnet:
Is this the bit of code that print the values to the serial monitor;

Yes. And I notice that the '28' read fits the pattern above. Is this a coincidence?