I2C アドレススキャン

ソース表示てすと(頂いて来たものの表示を少し変更)

/* this program is to detect IC2 device connected to Arduino
   project tutorial see http://osoyoo.com/?p=72

   Arduino 日本語リファレンス
   http://www.musashinodenpa.com/arduino/ref/
*/
#include <Wire.h>

char adrString[8];
void SerialPrintHex(byte iAdr,char *fmt = " %.02X") {
  sprintf(adrString, fmt, iAdr);
  Serial.print(adrString);
}

void setup() {
  Wire.begin();

  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  Serial.println("\nI2C Address ");
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() {
  byte error, address;
  int nDevices = 0;

  Serial.print("Scanning...\n   ");
  for(address = 0; address < 16; address++ ) SerialPrintHex( address,"%3x" );
  for(address = 0; address <= 0x78; address++ ) {
    if(! (address % 16))  SerialPrintHex( address, "\n%.02X:" );
    if(address &lt; 8) { Serial.print("   "); continue; }
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error) Serial.print((error==4)?" ??":"  -");
    else {
      SerialPrintHex( address, " %.02X" );
      nDevices++;
    }
  }
  if (nDevices) Serial.println("\ndone\n");
  else          Serial.println("\nNo I2C devices found\n");

  delay(5000); // wait 5 seconds for next scan
}


Serial Console

I2C Address 
Scanning...
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                          -  -  -  -  -  -  -  -
10:  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
20:  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
30:  -  -  -  -  -  -  -  -  -  -  -  - 3C  -  - 3F
40:  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
50:  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
60:  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
70:  -  -  -  -  -  -  -  -  -
done

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください