Problem with Wire library and EEPROM

Hello guys,

I am new to Arduino forum and new to I2C protocols. I have difficulty understanding the following code.

void writeAddress(int address, byte val) {
  Wire.beginTransmission(EEPROM_I2C_ADDRESS);
  
  Wire.write((int)(address >> 8));              //  -----------> (3)
  Wire.write((int)(address & 0xFF));            //  -----------> (4)
  Wire.write(val);                              //  -----------> (5)
  
  Wire.endTransmission();
}

I am little confused in three areas.

a) The bitshift operator. What is advantage of shifting bits?
b) address & 0xFF. Whats this line for ? it didn't make any sense!
c) In line 3, 4 and 5, the wire.write() is used. When this line is used, how the EEPROM distinguish address and data.

I need some serious help guys.I am little confused.
Thank you

Since you can only send one byte at a time on Wire and the addresses on that EEPROM appear to be larger than that they are splitting two byte int into to single bytes to send one at a time. The line with the shift shifts the high byte to the low 8 bits to send that. The next line masks off the low 8 bits so you can send them.

Look in the reference section at the bitwise operators for more on how it works.

Thanks for the quick response.

Delta_G:
The next line masks off the low 8 bits so you can send them.

whats the need for masking the 8 bit data? Please answer my 3rd question too, its very important.

Thank you

It has to be masked because it isn't an 8bit value. It's larger than that. But we can only send 8 bits at once.

It doesn't know the diff between address and value. I suspect that it tells by the order you send them, but you'll have to look at the datasheet for your particular EEPROM to be sure.

Please answer my 3rd question too, its very important.

c) In line 3, 4 and 5, the wire.write() is used. When this line is used, how the EEPROM distinguish address and data.

The eeprom chip is more than just a memory space. It has an internal controller which handles the I/O control logic and memory control logic. It knows how to execute the documented communication protocol.

As DeltaG suggests, take a look at the data sheet for your eeprom.

cattledog:
The eeprom chip is more than just a memory space. It has an internal controller which handles the I/O control logic and memory control logic. It knows how to execute the documented communication protocol.

As DeltaG suggests, take a look at the data sheet for your eeprom.

But what about other i2c devices. The wire.h library don't have any function to send address and date separately. I m still confused. Is it the order by which it is send to salve, identifies it as address and data? For example, after wire.begin(), pass the address, then pass the data to write and finally stop transmission.

The eeprom knows what to do with the address (that's not the EEPROM_I2C_ADDRESS) and data that it receives

If you have a different device (e.g. another Uno as in the master writer / slave receiver example that does not use a second address, the second (receiver) Uno also knows what to do with it (display).

sterretje:
The eeprom knows what to do with the address (that's not the EEPROM_I2C_ADDRESS) and data that it receives

If you have a different device (e.g. another Uno as in the master writer / slave receiver example that does not use a second address, the second (receiver) Uno also knows what to do with it (display).

Correct me if I am wrong....!!

The wire.h library send data or address, one by one. The wire library can't distinguish between data and address. Its the slaves which operates on the received information.

The wire.h library send data or address, one by one.

That's better stated as byte by byte.

The wire library can't distinguish between data and address. Its the slaves which operates on the received information.

Correct.

The Wire library just sends bytes out. It doesn't know or care whether they are addresses or data or what. Well except the I2C address but that isn't what you asked about. It is up to the receiving device to know what is what and it is up to you to read the datasheet for that device and learn what it expects to see and how.