![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Program to scan I2C bus for device addresses
main.cpp@0:545d57fc4eca, 2017-01-26 (annotated)
- Committer:
- michaelVisimid
- Date:
- Thu Jan 26 19:28:55 2017 +0000
- Revision:
- 0:545d57fc4eca
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaelVisimid | 0:545d57fc4eca | 1 | // Original program found here: http://playground.arduino.cc/Main/I2cScanner |
michaelVisimid | 0:545d57fc4eca | 2 | // Modified by Domen Ornik, 4.5.2015 |
michaelVisimid | 0:545d57fc4eca | 3 | |
michaelVisimid | 0:545d57fc4eca | 4 | #include "mbed.h" |
michaelVisimid | 0:545d57fc4eca | 5 | |
michaelVisimid | 0:545d57fc4eca | 6 | I2C i2c(I2C_SDA , I2C_SCL ); |
michaelVisimid | 0:545d57fc4eca | 7 | |
michaelVisimid | 0:545d57fc4eca | 8 | int returnCode, address, numberOfDevices = 0; |
michaelVisimid | 0:545d57fc4eca | 9 | char outByte = 0xAA; |
michaelVisimid | 0:545d57fc4eca | 10 | |
michaelVisimid | 0:545d57fc4eca | 11 | int main() { |
michaelVisimid | 0:545d57fc4eca | 12 | printf("\r\nI2C Bus Scanner\r\n"); |
michaelVisimid | 0:545d57fc4eca | 13 | numberOfDevices = 0; |
michaelVisimid | 0:545d57fc4eca | 14 | for(address = 1; address < 127; address++ ){ |
michaelVisimid | 0:545d57fc4eca | 15 | returnCode = i2c.write((address << 1),&outByte,1,0); |
michaelVisimid | 0:545d57fc4eca | 16 | if (returnCode == 0) |
michaelVisimid | 0:545d57fc4eca | 17 | { |
michaelVisimid | 0:545d57fc4eca | 18 | printf("I2C device found at address 0x%X\r\n", address); |
michaelVisimid | 0:545d57fc4eca | 19 | numberOfDevices++; |
michaelVisimid | 0:545d57fc4eca | 20 | } |
michaelVisimid | 0:545d57fc4eca | 21 | |
michaelVisimid | 0:545d57fc4eca | 22 | } |
michaelVisimid | 0:545d57fc4eca | 23 | if (numberOfDevices == 0){ |
michaelVisimid | 0:545d57fc4eca | 24 | printf("No I2C devices found\r\n"); |
michaelVisimid | 0:545d57fc4eca | 25 | } |
michaelVisimid | 0:545d57fc4eca | 26 | else{ |
michaelVisimid | 0:545d57fc4eca | 27 | printf("Number of devices: %d\r\n",numberOfDevices); |
michaelVisimid | 0:545d57fc4eca | 28 | } |
michaelVisimid | 0:545d57fc4eca | 29 | |
michaelVisimid | 0:545d57fc4eca | 30 | } |