WORKS

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "test_env.h"
00002 #include "ADXL345.h"
00003 
00004 #if defined(TARGET_LPC812)
00005 ADXL345 accelerometer(D10, D11, D12, D13);
00006 
00007 #else
00008 ADXL345 accelerometer(p5, p6, p7, p8);
00009 #endif
00010 
00011 // Assume test configuration on a plane (small x and y, z ~ g)
00012 #define MAX_X_Y     (50)
00013 #define MIN_Z       (200)
00014 #define MAX_Z       (300)
00015 
00016 void check_X_Y(int v) {
00017     int16_t a = (int16_t)v;
00018     if (abs(a) > MAX_X_Y) {
00019         printf("X/Y acceleration is too big: %d\n", a);
00020         notify_completion(false);
00021     }
00022 }
00023 
00024 
00025 int main() {
00026     int readings[3] = {0, 0, 0};
00027 
00028     printf("Starting ADXL345 test...\n");
00029     printf("Device ID is: 0x%02x\n", accelerometer.getDevId());
00030 
00031     //Go into standby mode to configure the device.
00032     accelerometer.setPowerControl(0x00);
00033 
00034     //Full resolution, +/-16g, 4mg/LSB.
00035     accelerometer.setDataFormatControl(0x0B);
00036 
00037     //3.2kHz data rate.
00038     accelerometer.setDataRate(ADXL345_3200HZ);
00039 
00040     //Measurement mode.
00041     accelerometer.setPowerControl(0x08);
00042 
00043     for (int i=0; i<3; i++) {
00044         wait(0.1);
00045 
00046         //13-bit, sign extended values.
00047         accelerometer.getOutput(readings);
00048 
00049         // X and Y
00050         check_X_Y(readings[0]);
00051         check_X_Y(readings[1]);
00052 
00053         // Z
00054         int16_t z = (int16_t)readings[2];
00055         if ((z < MIN_Z) || (z > MAX_Z)) {
00056             printf("Z acceleration not within expected range\n", z);
00057             notify_completion(false);
00058         }
00059     }
00060 
00061     notify_completion(true);
00062 }