initial code for i2c communication with accelerometers

Dependencies:   BLE_API mbed-dev nRF51822

Fork of capstone_i2c by Zachary Newman

Files at this revision

API Documentation at this revision

Comitter:
cpadua
Date:
Tue Apr 11 20:39:41 2017 +0000
Parent:
4:5985157ed67d
Commit message:
fixed i2c-api.c

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-dev.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 5985157ed67d -r a52a03b6d13b main.cpp
--- 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);
diff -r 5985157ed67d -r a52a03b6d13b mbed-dev.lib
--- a/mbed-dev.lib	Tue Apr 11 00:52:40 2017 +0000
+++ b/mbed-dev.lib	Tue Apr 11 20:39:41 2017 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/cpadua/code/mbed-dev/#0ad47fa279aa
+https://developer.mbed.org/users/cpadua/code/mbed-dev/#1d4c9d0af1e9