I2Cリーダー

ライターができれば、リーダーだ。
スニファを作ってみた。


#include

int i2cAddress = (ここでスニッフするスレーブデバイスのアドレスを指定);
byte Buffer;

void onI2CReceived(int NumOfReceivedData)
{
while(Wire.available() > 0)
{
Buffer = Wire.read();
Serial.println(Buffer);
}
}

void setup()
{
Serial.begin(9600);
Wire.begin(i2cAddress);
Wire.onReceive(onI2CReceived);
}

void loop()
{
}

これで指定アドレスへのライトリクエストがある度に、その内容をシリアルに1バイト/行で出力をする。
ArduinoIDEにはシリアルコンソールがついているので、こういったプログラムの動作確認には便利だ。
Arduino Uno Rev3

Arduino Uno Rev3