initial code for i2c communication with accelerometers
Dependencies: BLE_API mbed-dev nRF51822
Fork of capstone_i2c by
Diff: main.cpp
- Revision:
- 5:a52a03b6d13b
- Parent:
- 3:3d08e2045bde
--- a/main.cpp Tue Apr 11 00:52:40 2017 +0000 +++ b/main.cpp Tue Apr 11 20:39:41 2017 +0000 @@ -198,22 +198,37 @@ wait(5); //Wire.begin(); Wire.begin(SCL, SDA, TWI_FREQUENCY_100K); - pc.printf("IIC Demo Start \r\n"); - - int nDevices = 0; - for (int i = 0; i < 128; i++) { - uint8_t i2caddr = (i << 1); - Wire.beginTransmission(i2caddr); - int error = Wire.endTransmission(); - if(error == 0) { - pc.printf("I2C device found at 0x%x\n", i); - nDevices++; + uint8_t address; + int error; + int nDevices; + + pc.printf("Scanning...\n"); + + nDevices = 0; + for(address = 1; address < 127; address++ ) + { + pc.printf("address: %x\n", address); + // The i2c_scanner uses the return value of + // the Write.endTransmisstion to see if + // a device did acknowledge to the address. + Wire.beginTransmission(address << 1); + error = Wire.endTransmission(); + pc.printf("Error Code: %d\n", error); + if (error == 0) + { + pc.printf("I2C device found at address 0x%x\n", address); + nDevices++; } + else if (error==4) + { + pc.printf("Unknown error at address 0x%x\n", address); + } } - if (nDevices == 0){ + if (nDevices == 0) pc.printf("No I2C devices found\n"); - } else - pc.printf("done\n"); + else { + pc.printf("%d I2C devices found\n", nDevices); + } uint8_t* whoami = new uint8_t[1]; AT24C512_ReadBytes(REG_WHOAMI, whoami, 1, ADDR_ONE);