Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of I2C_scanner by
main.cpp
- Committer:
- smartsystemdesign
- Date:
- 2016-11-09
- Revision:
- 2:39ba9beabf3c
- Parent:
- 1:1cdd12a4efb2
- Child:
- 3:42120705a9bd
File content as of revision 2:39ba9beabf3c:
// I2CU - Search for devices on an I2C bus
// Copyright (c) 2009, sford
// Released under the MIT License: http://mbed.org/license/mit
//
// Goes through each device address, seeing if it gets a response
// - for every device if finds, it prints the 8-bit address
// - if the program hangs, the bus isn't working (check pull-ups etc)
// - if it doesn't find your device, check power/bus wiring etc
#include "mbed.h"
I2C i2c(I2C_SDA, I2C_SCL); // sda, scl
Ticker repeatTicker;
void repeat()
{
printf("I2CU! Searching for I2C devices...\n");
int count = 0;
for (int address=0; address<256; address+=2) {
bool temp = i2c.write(address, NULL, 0); // 0 if successful, otherwise non-zero
if (!temp && (address >> 1) != 0) { // not interested in address 0
printf(" - I2C device found at address 0x%02X\n", address >> 1);
count++;
}
}
printf("%d device(s) found\n", count);
}
int main()
{
repeatTicker.attach(&repeat, 2.0);
}
