Mateusz Grzywacz / Mbed OS I2C_scanner
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 Serial pc(USBTX, USBRX, 115200);
00004 
00005 
00006 I2C i2c(PC_1, PC_0); // SDA, SCL    L476 all on the left side
00007 //I2C i2c(PB_9, PB_8); // SDA, SCL    arduino pins
00008 //I2C i2c(PB_14, PB_13); // SDA, SCL
00009 DigitalOut led(D13);
00010 
00011 int main()
00012 {
00013     i2c.frequency(100000);
00014     while (1) {
00015         pc.printf("Searching for I2C devices...\n\r");
00016 
00017         int count = 0;
00018         for (int address = 0; address < 255; address +=2) { // check only for device's read addres
00019             if (!i2c.write(address, NULL, 0)) { // 0 returned is ok
00020                 pc.printf("I2C device found at address 0x%02X (0x%02X in 8-bit)\n\r", address >> 1, address);  // the address is without LSB, which is R/W flag. shoft it right once
00021                 count++;
00022                 led = 1;
00023             }
00024             wait(0.02);
00025         }
00026         if (count)
00027             pc.printf("%d", count);
00028         else
00029             pc.printf("No");
00030         pc.printf(" device%c found\n\r\n", count == 1?'\0':'s');
00031         led = 0;
00032         wait (1.8);
00033     }
00034 }