This library enables users to communicate with the ADXL345 accelerometer through the I2C bus on the mbed. The API names are similar and work nearly the same way as those made in the SPI libraries for the ADXL345.

Dependencies:   mbed

Fork of ADXL345_I2C by Peter Swanson

Files at this revision

API Documentation at this revision

Comitter:
ledonger
Date:
Mon Oct 02 08:28:52 2017 +0000
Parent:
1:d9412b56f98a
Commit message:
ADXL345_data_recovery

Changed in this revision

ADXL345_I2C.cpp Show annotated file Show diff for this revision Revisions of this file
ADXL345_I2C.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r d9412b56f98a -r c2067856a052 ADXL345_I2C.cpp
--- a/ADXL345_I2C.cpp	Thu May 12 01:19:36 2011 +0000
+++ b/ADXL345_I2C.cpp	Mon Oct 02 08:28:52 2017 +0000
@@ -131,14 +131,14 @@
 }
 
 
-void ADXL345_I2C::getOutput(int* readings){
+void ADXL345_I2C::getOutput(int16_t* readings){
     char buffer[6];    
     multiByteRead(ADXL345_DATAX0_REG, buffer, 6);
     
-    readings[0] = (int)buffer[1] << 8 | (int)buffer[0];
-    readings[1] = (int)buffer[3] << 8 | (int)buffer[2];
-    readings[2] = (int)buffer[5] << 8 | (int)buffer[4];
-
+    readings[0] = (int16_t)buffer[1] << 8 | (int16_t)buffer[0];
+    readings[1] = (int16_t)buffer[3] << 8 | (int16_t)buffer[2];
+    readings[2] = (int16_t)buffer[5] << 8 | (int16_t)buffer[4];
+    //printf("raw data : %d\t%d\t%d\t%d\t%d\t%d\n",buffer[0],buffer[1],buffer[2],buffer[3],buffer[4],buffer[5]);
 }
 
 
diff -r d9412b56f98a -r c2067856a052 ADXL345_I2C.h
--- a/ADXL345_I2C.h	Thu May 12 01:19:36 2011 +0000
+++ b/ADXL345_I2C.h	Mon Oct 02 08:28:52 2017 +0000
@@ -146,7 +146,7 @@
      * @param Pointer to a buffer to hold the accelerometer value for the
      *        x-axis, y-axis and z-axis [in that order].
      */
-    void getOutput(int* readings);
+    void getOutput(int16_t* readings);
 
     /**
      * Read the device ID register on the device.
diff -r d9412b56f98a -r c2067856a052 main.cpp
--- a/main.cpp	Thu May 12 01:19:36 2011 +0000
+++ b/main.cpp	Mon Oct 02 08:28:52 2017 +0000
@@ -1,11 +1,13 @@
 #include "ADXL345_I2C.h"
 
- ADXL345_I2C accelerometer(p28, p27);
+ ADXL345_I2C accelerometer(D14, D15);
  Serial pc(USBTX, USBRX);
+ 
+ DigitalIn button(USER_BUTTON);
 
  int main() {
        pc.baud(115200);
-     int readings[3] = {0, 0, 0};
+     int16_t readings[3] = {0, 0, 0};
      
      pc.printf("Starting ADXL345 test...\n");
      wait(.001);
@@ -35,15 +37,22 @@
      if(accelerometer.setPowerControl(MeasurementMode)) {
         pc.printf("didn't set the power control to measurement\n"); 
         return 0;   } 
- 
+    int nbPoints = 0;
      while (1) {
-     
-         wait(0.1);
-         
-         accelerometer.getOutput(readings);
-         
-
-        pc.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
+        
+        
+        if(button != 0){
+            while(nbPoints < 3*10){
+                wait(0.1);
+                accelerometer.getOutput(readings);
+                pc.printf("%d,%d,%d,\n",readings[0],readings[1],readings[2]);
+                //pc.printf("%f,%f,%f,", (float)((int16_t)readings[0]+1000)/2000.0, (float)((int16_t)readings[1]+1000)/2000.0, (float)((int16_t)readings[2]+1000)/2000.0);
+                
+                nbPoints += 3;
+            }
+            nbPoints = 0;
+            printf("\n");
+        }
      }
  
  }
diff -r d9412b56f98a -r c2067856a052 mbed.bld
--- a/mbed.bld	Thu May 12 01:19:36 2011 +0000
+++ b/mbed.bld	Mon Oct 02 08:28:52 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912
+https://mbed.org/users/mbed_official/code/mbed/builds/675da3299148
\ No newline at end of file