requestI2CFrom()
[Inter Integrated Circuit]
unsigned char requestI2CFrom(unsigned char address, unsigned char quantity, bool send_stop = false);Description
Requests data from a follower device on the I2C bus.
Parameters
address: the address of the follower device.
quantity: the number of bytes to request from the
follower device.
send_stop: a boolean value that determines whether to
send a stop condition on the I2C bus.
Returns
The error status of the transmission, 0 if successful,
1 if the transmission failed.
Example Code
The code initializes the I2C communication with the SDA pin connected to pin 4, and the SCL pin connected to pin 5.
Then begins an I2C transmission to the follower chip with the address 0x20 to request 4 bytes of data, and sends a stop condition on the I2C bus.
The code reads the data from the follower chip, and place it in the pins 1, 2, 3, and 4.
Ends the I2C transmission and sends a stop condition on the I2C bus.
void setup() {
pinLabel(4, "SDA"); // sets the label of pin 4 to "SDA"
pinLabel(5, "SCL"); // sets the label of pin 5 to "SCL"
beginI2C(4, 5); // initializes the I2C communication with the SDA pin connected to pin 4, the SCL pin connected to pin 5
pinLabel(1, "O0"); // sets the label of pin 1 to "O0"
pinLabel(2, "O1"); // sets the label of pin 2 to "O1"
pinLabel(3, "O2"); // sets the label of pin 3 to "O2"
pinLabel(4, "O3"); // sets the label of pin 4 to "O3"
}
void loop() {
requestI2CFrom(0x20, 4, true); // requests 4 bytes of data from the follower chip with the address 0x20
while (availableI2C()) { // waits until the data is available
int val = readI2C(); // reads the data from the follower chip
digitalWrite(1, val & 0x01); // places the least significant bit of the data in pin 1
digitalWrite(2, val & 0x02); // places the second least significant bit of the data in pin 2
digitalWrite(3, val & 0x04); // places the third least significant bit of the data in pin 3
digitalWrite(4, val & 0x08); // places the most significant bit of the data in pin 4
}
}Notes and Warnings
- If the
send_stopparameter is set totrue, the I2C bus will not be released. - The maximum number of bytes that can be requested is 32.
