#include <Wire.h>
int addr = 0;
void setup()
{
Wire.begin(); // initialise the connection
Serial.begin(9600);
while (!Serial)
{
}
delay(100);
}
void loop() {
byte deviceAddress = 0x51;
byte data = readData(addr, deviceAddress);
Serial.print(data, HEX);
Serial.print(" ");
addr++;
if(addr%16 == 0) {
Serial.print('\n');
}
// check for 1Kbits first
if (addr%128 == 0) {
Serial.println("round complete");
Serial.println();
addr = 0;
}
delay(100);
}
byte readData(int address, int deviceAddress) {
// sending device address
Wire.beginTransmission(deviceAddress);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom((short int)deviceAddress, 1);
if(Wire.available())
{
byte data = Wire.read();
return data;
}
return 0xAA; // random data
}
The problem I am facing is, I get back the address (from which I want to read the data) as the data itself (For e.g. read(0) returns 0, read(1) returns 1 and so on). I even tried to debug the I2C communication using the logic analyzer (Saleae logic in this case). TheA screenshot attached is shown below.
Can you help me by identifying what possibly I am doing wrong here?
Thanks.
