24LC256 write int

Hello, hopefully someone can give me some advice where my logic is not right.

Ive taken the page write code http://www.hobbytronics.co.uk/eeprom-page-write

Im trying to write my own basic class that will write basic datatypes to the eeprom. I can successfully write bool and bytes, but the problem starts when i try and write a int or long. Ive attached the int code which then calls the writeEEPROM code the handle paging.

The problem is this i write 128 to the eeprom (atleast thats what im thinking is beeing written, and then when i do a read i get 511
i then try and write 255 and i get 511

  int testIntA = 128;
  writeInt(2,testIntA);  
  int testIntB = readInt(2);
  Serial.print("int test A:");
  Serial.print(testIntA);
  Serial.print(" ");
  Serial.println(testIntB);
  testIntA = 255;
  writeInt(2,testIntA);  
  testIntB = readInt(2); 
  Serial.print("int test B:");
  Serial.print(testIntA);
  Serial.print(" ");
  Serial.println(testIntB);




int readInt(unsigned int _eeaddress) 
{
	int rdata = 0;

	byte dataTemp[2];
	readEEPROM(_eeaddress, dataTemp, sizeof(dataTemp));
	rdata = (unsigned int)(dataTemp[1] << 8) | dataTemp[0];
	
	return rdata;
}


void writeInt(unsigned int _eeaddress, int _data) 
{
	writeEEPROM(_eeaddress, (byte*)(&_data), sizeof(_data));
}






void writeEEPROM(unsigned int eeaddress, byte *data, unsigned int data_len) 
{
  // Uses Page Write for 24LC256
  // Allows for 64 byte page boundary
  // Splits string into max 16 byte writes
  byte i=0;
  byte counter=0;
  unsigned int  address;
  unsigned int  page_space;
  unsigned int  page=0;
  unsigned int  num_writes;
  //unsigned int  data_len=0;
  byte first_write_size=0;
  byte last_write_size=0;  
  byte write_size=0;  
  
  // 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;
  
     Wire.beginTransmission(deviceaddress);
     Wire.write((int)((address) >> 8));   // MSB
     Wire.write((int)((address) & 0xFF)); // LSB
     counter=0;
     do{ 
        Wire.write((byte) data[i]);
        i++;
        counter++;
     } while((data[i]) && (counter<write_size));  
     Wire.endTransmission();
     address+=write_size;   // Increment address for next write
     
     delay(6);  // needs 5ms for page write
  }
}
 
void readEEPROM(unsigned int eeaddress, byte *data, unsigned int data_len) 
{
  
	Wire.beginTransmission(deviceaddress);
	Wire.write((int)(eeaddress >> 8));   // MSB
	Wire.write((int)(eeaddress & 0xFF)); // LSB
	Wire.endTransmission();

	Wire.requestFrom(deviceaddress,data_len);

	byte i = 0;
	while(Wire.available()){ 
		data[i++] = Wire.read();
	}

}

Im sure im doing something very dumb but i cannot see where ive gone wrong.

Regards

Trev

do {
      Wire.write((byte) data[i]);
      i++;
      counter++;
    } while((data[i]) && (counter < write_size));

You are encountering situations where the value of data[]==0 and you are breaking from the while loop.

Try

while ((i < data_len) && (counter < write_size));

This works for the integer situation

#include <Wire.h>
int deviceaddress = 0x50;

void setup()
{
  Wire.begin();
  Serial.begin(115200);
  int testIntA = 128;
  writeInt(2, testIntA);
  int testIntB = readInt(2);
  Serial.print("int test A:");
  Serial.print(testIntA);
  Serial.print(" ");
  Serial.println(testIntB);
  // testIntA = 255;
  //testIntA = 512;
  testIntA = 12345;
  writeInt(2, testIntA);
  testIntB = readInt(2);
  Serial.print("int test B:");
  Serial.print(testIntA);
  Serial.print(" ");
  Serial.println(testIntB);
}
void loop() {}

int readInt(unsigned int _eeaddress)
{
  int rdata = 0;

  byte dataTemp[2];
  readEEPROM(_eeaddress, dataTemp, sizeof(dataTemp));
  rdata = (unsigned int)(dataTemp[1] << 8) | dataTemp[0];

  return rdata;
}


void writeInt(unsigned int _eeaddress, int _data)
{
  writeEEPROM(_eeaddress, (byte*)(&_data), sizeof(_data));
}

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

  // 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;

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

    delay(6);  // needs 5ms for page write
  }
}

void readEEPROM(unsigned int eeaddress, byte *data, unsigned int data_len)
{

  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();

  Wire.requestFrom(deviceaddress, data_len);

  byte i = 0;
  while (Wire.available()) {
    data[i++] = Wire.read();
  }

}

I would recommend that you use Jack Christensens library extEEPROM.h available through the library manager. It handles the page boundaries and block writes.

Thank you loads for the quick reply. I tried your change and it works well. Ive also took your advice and used extEEPROM class and added a wrapper to do bool byte int and longs so far.

Thank you again

Trev