ADXL345 triple axis, digital accelerometer example.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "ADXL345.h"
00002 
00003 ADXL345 accelerometer(p5, p6, p7, p8);
00004 Serial pc(USBTX, USBRX);
00005 
00006 int main() {
00007 
00008     int readings[3] = {0, 0, 0};
00009     
00010     pc.printf("Starting ADXL345 test...\n");
00011     pc.printf("Device ID is: 0x%02x\n", accelerometer.getDevId());
00012 
00013     //Go into standby mode to configure the device.
00014     accelerometer.setPowerControl(0x00);
00015 
00016     //Full resolution, +/-16g, 4mg/LSB.
00017     accelerometer.setDataFormatControl(0x0B);
00018     
00019     //3.2kHz data rate.
00020     accelerometer.setDataRate(ADXL345_3200HZ);
00021 
00022     //Measurement mode.
00023     accelerometer.setPowerControl(0x08);
00024 
00025     while (1) {
00026     
00027         wait(0.1);
00028         
00029         accelerometer.getOutput(readings);
00030         
00031         //13-bit, sign extended values.
00032         pc.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
00033 
00034     }
00035 
00036 }