writing to i2c eeprom numbers over 255

Hello,
i would use two bytes to write numbers over 255 into the i2c eeprom.

here is a not running file.

#include <Wire.h> //Include the Arduino I2C Library</pre>

void i2c_eeprom_schreibe_zahl( int deviceaddress, unsigned int eeaddress, int data ) {
int rdata = data;

int rdata_low = lowByte(rdata); 
int rdata_high = highByte(rdata);

Serial.println(rdata_low);
Serial.println(rdata_high);


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

Wire.beginTransmission(deviceaddress); 
Wire.write((int)(eeaddress+1 >> 8)); // MSB
Wire.write((int)(eeaddress+1 & 0xFF)); // LSB
 
Wire.write(rdata_high); 
 
Wire.endTransmission();
}
 
byte i2c_eeprom_lese_zahl( int deviceaddress, unsigned int eeaddress ) {
byte rdata_low;
byte rdata_high; 
 
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
 
if (Wire.available()) rdata_low = Wire.read();

Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress+1 >> 8)); // MSB
Wire.write((int)(eeaddress+1 & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
 
if (Wire.available()) rdata_high = Wire.read();

Serial.println(rdata_low);
Serial.println(rdata_high);

int rdata = ((rdata_low << 0) & 0xFF) + ((rdata_high << 8) & 0xFF00);

//int rdata = word(rdata_low, rdata_high); 

Serial.println(rdata);

return rdata;
} 
 
void setup() {
int Wert = 111; //Data
Wire.begin(); //Start I2C connections
Serial.begin(115200);
 
delay(10); //add a small delay
 
Serial.println("Setup done");
}
 
void loop() {
int addr=0; //EEPROM Address 0

i2c_eeprom_schreibe_zahl(0x50, 1, 3999); // write to EEPROM

int b = i2c_eeprom_lese_zahl(0x50, 1); // read eeprom
Serial.print(b);

 
Serial.println("ausgelesen");
delay(2000);
}

try this?

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

Wire.write(rdata_low);
Wire.write(rdata_high); 

Wire.endTransmission();
}

check also - http://playground.arduino.cc/Main/LibraryForI2CEEPROM -

Another way is to use a union:

union {
   byte myIntVal[2];
   int val;
} myUnion;

To write an int to EEPROM, put the value into myUnion with:

  myUnion.val = number;

You can then write the value to EEPROM using a loop with myIntVal[]. You can read from EEPROM by simply reversing the process. The size of myIntVal[] must match the size of the other member of the union (e.g., myValLong[4], long val). The only advantage is it is a little more portable since you don't have to know the byte-order of the value being written.

There are three corrections I have made to your code to make it run as written. As Rob says, it can be simplified by performing the sequential writes, and also request 2 bytes on the read. But those are not the reasons the code would not work as intended.

1) Add a small delay after the two Wire.endTransmission statements in the write function. I used delay(5), but it may depend on the eeprom and may need to be as long as delay(10).

2) Change the read function to return an int instead of a byte. You want int b after the read.

int i2c_eeprom_lese_zahl( int deviceaddress, unsigned int eeaddress )

3) There is some confusion in the address arrangement you pass into the read/write functions. The way they are written you want your functions to start at address 0

void loop() {
int addr=0; //EEPROM Address 0

i2c_eeprom_schreibe_zahl(0x50, addr, 3999); // write to EEPROM

int b = i2c_eeprom_lese_zahl(0x50, addr); // read eeprom

Thank you,

i have changed some things, it runs only the adding the two readings of the two bytes does not match.
Writing and reading high and low byte is good.

now it runs, here the code.

#include <Wire.h> //Include the Arduino I2C Library</pre>


 
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, int data ) {
int rdata = data;

int rdata_low = lowByte(rdata); 
int rdata_high = highByte(rdata);
Serial.println(rdata_low);
Serial.println(rdata_high);
Wire.beginTransmission(deviceaddress);


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

delay(10);

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


} 
 
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
    byte rdata = 0xFF;
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddress >> 8)); // MSB
    Wire.write((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();
    Wire.requestFrom(deviceaddress,1);
    if (Wire.available()) rdata = Wire.read();
    return rdata;
}
 /*
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
byte rdata = 0xFF;
 
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
 
if (Wire.available()) rdata = Wire.read();
 
return rdata;
}*/
 
void setup() {
char somedata[] = "dayzlab.wordpress.com"; //Data

Wire.begin(); //Start I2C connections
Serial.begin(115200);

}
 
void loop() {
int addr=0; //EEPROM Address 0
int wert = -32000; // Zahl zwischen - und +32000
i2c_eeprom_write_byte(0x50, addr, wert ); // write to EEPROM
 
delay(10); //add a small delay
 
Serial.println("Memory written");


// zwei Byte lesen und wieder zusammensetzen.
byte b = i2c_eeprom_read_byte(0x50, addr); // access the first address from the memory
 

Serial.println(b); //print content to serial port
byte a = i2c_eeprom_read_byte(0x50, addr+1); // access the first address from the memory
 

Serial.println(a); //print content to serial port

int c = ((b << 0) & 0xFF) + ((a << 8) & 0xFF00);

//byte c = word(a, b); 
Serial.println(c); //print content of the two rows
 
Serial.println(" ");
delay(2000);
}

What is your serial output?

0 131 Memory written 0 131 -32000

this is the number that i done as input (-32000) is an int number

it runs only the adding the two readings of the two bytes does not match.

The output looks correct.

10000011 00000000 is the signed 16 bit binary of -32000

10000011 is 131 decimal in the high byte

Do a Google search on "two's complement"