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

Dependents:   sensors-example

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