peekI2C()

[Inter Integrated Circuit]
unsigned char peekI2C();

Description

Peeks at the next byte received from the follower chip without removing it from the buffer.

Parameters

None.

Returns

The next byte received from the follower chip without 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 requests 4 bytes of data from the follower chip with the address 0x20, and sends a stop condition on the I2C bus.

The code checks if any data is available and peeks at the data from the follower chip.

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
}

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 = peekI2C();        // peeks at 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
        readI2C();                      // reads the data from the follower chip and removes it from the buffer
    }
}

Notes and Warnings

  • If the function peekI2C() is called when no data is available, the function will return 0.
  • The return value of the function peekI2C() is an integer between 0 and 255 or an unsigned char.