I use <Wire.h> library to communicate with my I2C device and use the I2C protocol analyzer to observe data format.
PS: My I2C slave device address is 0x50, I want to write data (0x09) to EEPROM address (0xE0) and then read it for check.
But I use the I2C protocol analyzer to observe data format, I found a strange thing.
The 00 data always be send before the slave device address.
Anyone who can tell me why?
Thank you!
#include <Wire.h>
#define chip1 0x50
unsigned int pointer;
byte d=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
}
void wireData(int device, unsigned int address, byte data)
{
Wire.beginTransmission(device);
Wire.write((byte) (address >> 8));
Wire.write((byte) (address & 0xFF));
Wire.write(data);
Wire.endTransmission();
delay(10);
}
byte readData(int device, unsigned int address)
{
byte result;
Wire.beginTransmission(device);
Wire.write((byte) (address >> 8));
Wire.write((byte) (address & 0xFF));
Wire.endTransmission();
Wire.requestFrom(device,1);
result = Wire.read();
return result;
}
void loop(){
wireData(chip1,0xE0,9);
d=readData(chip1,0xe0);
Serial.println(d,DEC);
delay(1000);
}
T_I2C.ino (787 Bytes)
