i2c question: will a fast device over-write a slow device's response?

Status
Not open for further replies.

jasper

Active member
Consider the following basic code snippet taken from the i2c wire example here :


Code:
 // read 1 byte, from address 0
[B]  Wire.requestFrom(80, 1);[/B]
  while(Wire.available()) {
    num = Wire.receive();
  }
  Serial.print("num0 = ");
  Serial.println(num, DEC);


 // read 1 byte, from address 1
 [B] Wire.requestFrom(81, 1);[/B]
  while(Wire.available()) {
    num = Wire.receive();
  }
  Serial.print("num1 = ");
  Serial.println(num, DEC);


My question is this: assume device 0 is a little slow to respond and immediately after sending the "requestFrom" it hasnt responded with its data, wire.available will probably return 0.

A short while later i ask device 1 for its data and in the meantime device 0 has responded and put its data on the bus, will wire.receive now return device 0's data?
:confused::confused:
 
Wire.requestFrom() waits for all the data to arrive. The I2C stop condition has already occurred before it returns, so no more data is actually arriving. Wire.available() simply reports the amount of stored response you haven't yet read.
 
ok thanks for the clarification, i had better watch out that my slave responds quickly and does not block at that point.

That explains why some devices , like ultrasonic sensors , you send it a "range" command, then when you come back later the requestFrom() is ready with the data.
 
Status
Not open for further replies.
Back
Top