Scans the I2C bus and outputs the addresses that were found
main.cpp
- Committer:
- ElectronicsSanta
- Date:
- 2017-06-27
- Revision:
- 0:c801d9233d33
- Child:
- 1:1d28de04ff26
File content as of revision 0:c801d9233d33:
#include "mbed.h" I2C i2c(I2C_SDA, I2C_SCL); Serial pc(SERIAL_TX, SERIAL_RX); int main() { pc.baud(115200); pc.printf("I2C scanner starting\n"); i2c.frequency(100e3); // default freq (100kHz) const char *data = "1"; int ack; unsigned char address; for (address = 1; address < 127; address++) { ack = i2c.write(address << 1, data, 1); // address is shifted to the left to leave room for the last R/W bit if (ack == 0) { pc.printf("Sent 0x%X; 0x%X ACK \r\n", address << 1, address); // Send command string } wait_ms(2); } pc.printf("I2C scanner done\n"); }