readI2C()
[Inter Integrated Circuit]
unsigned char readI2C();Description
Reads data from the follower chip contained in the buffer.
Parameters
None.
Returns
The next byte received from the follower chip removing it from the buffer.
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 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
}
}Notes and Warnings
- If the function
readI2C()is called when no data is available, the function will return 0. - The function
readI2C()will delete the data from the buffer after reading it, if you want to keep the data, you should store it in a variable, or call the functionpeekI2C()instead. - The return value of the function
readI2C()is an integer between 0 and 255 or anunsigned char.
