Un petit programme qui scanne le bus à la recherche des éléments connectés.
// Wire Scanner
//
//
// Demonstrates the use of the Wire library
// Ping all valid adresses on the I²C bus
// and get the answer to see if there is a slave
// Print an 8 x 16 array with the results
// Created by fdufnews 2012 december 14th
// This example code is in the public domain.
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
Serial.println(); // Print a banner
Serial.println();
Serial.println("I2C slave scanner");
Serial.println(" reserved adress");
Serial.println(". no slave detected");
Serial.println("X slave detected");
Serial.println();
Serial.println();
int result;
Serial.println(" 0 1 2 3 4 5 6 7 8 9 A B C D E F");
// Scan only valid addresses (8 to 0x7B)
// 0-7 and 7C-7F are reserved ones
unsigned char devices=0; // holds how many devices found
for (unsigned char ad=0; ad<0x7C;ad++){
if(ad%16==0){ // If at start of a row
Serial.print(ad>>4,HEX); // Display high order bit address
Serial.print(" ");
}
if (ad>7){ // skip address from 0 to 7
Wire.beginTransmission(ad); // start transmission
result = Wire.endTransmission(); // end transmission and store answer
if (!result) devices++; // add a device to the count if ACK
Serial.print(" ");
Serial.print(result==0?"X ":". "); // If ACK there is a guy at this address
}
else{
Serial.print(" "); // Sorry nobody here
}
if(ad%16==15) Serial.println(); // end of the row add a new line
}
Serial.println();
Serial.println();
Serial.println();
Serial.print(devices, DEC);
Serial.print(" device");
Serial.print(devices>1?"s":"");
Serial.println(" found on the bus");
}
void loop()
{
}