availableI2C()
[Inter Integrated Circuit]
bool availableI2C();Description
Checks if the I2C bus buffer contains any data.
Parameters
None.
Returns
A boolean value that indicates if the I2C bus buffer contains any data.
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.
Finally, the code checks if any data is available and 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
}
}