Arduino に接続したI2Cモジュールを検索するサンプルソースです。
Wireライブラリしか使っていないのでソースがシンプルです。:-)
// --------------------------------------------------
// Global Versatile Controler
// --------------------------------------------------
// --------------------------------------------------
// Memo
// --------------------------------------------------
// ------------------------------
// BASE
// ------------------------------
// I2C Module Scan by Wire Liblary, for Arduino.
//
// New BSD License. Copyright (c) 2011-2012, Future Versatile Group
// All rights reserved.
//
// 2012.02.08 T.Kabu http://www.gvc-on.net/
//
// http://todbot.com/blog/2009/11/29/i2cscanner-pde-arduino-as-i2c-bus-scanner/ ... I2C bus scanner for Arduino
#include "Wire.h"
// ------------------------------
// GVC Module Scan
// ------------------------------
void gvc_module_scan(byte from_id, byte to_id)
{
char module_id; // Target I2C ID
byte data; // Dummy data
byte result; // Result of TX
// Open I2C device
Wire.begin();
// I2C Module Scan, from_id ... to_id
for (module_id = from_id; module_id <= to_id; module_id ++)
{
Wire.beginTransmission(module_id);
Wire.write(&data, 0);
result = Wire.endTransmission();
// I2C Module found out!
if (result == 0)
{
Serial.print("I2C Module Found! ID = ");
Serial.println(module_id, DEC);
}
}
}
void setup()
{
// Serial Start!
Serial.begin(9600);
Serial.print("Wire(I2C) ID Scan.\n");
// I2C Module Scan
gvc_module_scan(1, 100);
Serial.print("\nEND\n");
}
void loop()
{
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
}
ピンバック: I2Cデバイスを調べる方法 | 続・バイク好き!セロリ嫌い!