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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "ADXL345_I2C.h"
00002 
00003  ADXL345_I2C accelerometer(D14, D15);
00004  Serial pc(USBTX, USBRX);
00005  
00006  DigitalIn button(USER_BUTTON);
00007 
00008  int main() {
00009        pc.baud(115200);
00010      int16_t readings[3] = {0, 0, 0};
00011      
00012      pc.printf("Starting ADXL345 test...\n");
00013      wait(.001);
00014      pc.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
00015     wait(.001);
00016     
00017      // These are here to test whether any of the initialization fails. It will print the failure
00018     if (accelerometer.setPowerControl(0x00)){
00019          pc.printf("didn't intitialize power control\n"); 
00020          return 0;  }
00021      //Full resolution, +/-16g, 4mg/LSB.
00022      wait(.001);
00023      
00024      if(accelerometer.setDataFormatControl(0x0B)){
00025         pc.printf("didn't set data format\n");
00026         return 0;  }
00027      wait(.001);
00028      
00029      //3.2kHz data rate.
00030      if(accelerometer.setDataRate(ADXL345_3200HZ)){
00031         pc.printf("didn't set data rate\n");
00032         return 0;    }
00033      wait(.001);
00034      
00035      //Measurement mode.
00036      
00037      if(accelerometer.setPowerControl(MeasurementMode)) {
00038         pc.printf("didn't set the power control to measurement\n"); 
00039         return 0;   } 
00040     int nbPoints = 0;
00041      while (1) {
00042         
00043         
00044         if(button != 0){
00045             while(nbPoints < 3*10){
00046                 wait(0.1);
00047                 accelerometer.getOutput(readings);
00048                 pc.printf("%d,%d,%d,\n",readings[0],readings[1],readings[2]);
00049                 //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);
00050                 
00051                 nbPoints += 3;
00052             }
00053             nbPoints = 0;
00054             printf("\n");
00055         }
00056      }
00057  
00058  }