Last active
June 4, 2020 12:55
-
-
Save SweiLz/a97d290aa0f1ac40dffd5d7ef7faa3d7 to your computer and use it in GitHub Desktop.
I2C address scanner for mbed os
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mbed.h> | |
I2C i2c(PB_11, PB_10); // SDA pin, SCL pin | |
Serial pc(PB_6, PB_7); // TX pin, RX pin | |
int main() | |
{ | |
pc.baud(115200); | |
wait_ms(2000); | |
pc.printf("Hello World!\n"); | |
int count = 0; | |
for (int address = 0; address < 127; address++) | |
{ | |
if (!i2c.write(address << 1, NULL, 0)) // 0 returned is ok | |
{ | |
pc.printf("I2C device found at address 0x%02X (0x%02X in 8-bit)\n", address, address << 1); | |
count++; | |
} | |
wait_ms(20); | |
} | |
if (count) | |
pc.printf("%d", count); | |
else | |
pc.printf("No"); | |
pc.printf(" device found\n\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment