Here's a question for anyone knowledgeable of the ways of I2C.
I'm curious about sending multiple writes to a device and was looking at the code for a MPU9250 module.
There is a nice library that compiled straight off the shelf by Kris Winer here.
In one of his code examples he makes multiple calls to a function called writeByte():
// Wire.h read and write protocols
void writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
{
Wire.beginTransmission(address); // Initialize the Tx buffer
Wire.write(subAddress); // Put slave register address in Tx buffer
Wire.write(data); // Put data in Tx buffer
Wire.endTransmission(); // Send the Tx buffer
}
here is a (truncated) example of the code calling writeByte():
void initMPU9250()
{
writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x00);
delay(100);
writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x01);
delay(200);
writeByte(MPU9250_ADDRESS, CONFIG, 0x03);
writeByte(MPU9250_ADDRESS, SMPLRT_DIV, 0x04);
.
.
.
etc
}
My question is why call Wire.endtransmission() each time your sending I2C to the same device? I can see one reason straight off the bat which is you may need to call delay after your write() - but in cases where you do not need to call delay, wouldnt it be faster to do something like:
Wire.beginTransmission(address);
Wire.write(data1);
Wire.write(data2);
Wire.write(data3);
Wire.write(data4);
Wire.endTransmission();
at least when it's possible?