Preliminary main mbed library for nexpaq development
libraries/tests/mbed/spi_ADXL345/main.cpp@1:d96dbedaebdb, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:54:50 2016 +0000
- Revision:
- 1:d96dbedaebdb
- Parent:
- 0:6c56fb4bc5f0
Removed extra directories for other platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | #include "test_env.h" |
nexpaq | 0:6c56fb4bc5f0 | 2 | #include "ADXL345.h" |
nexpaq | 0:6c56fb4bc5f0 | 3 | |
nexpaq | 0:6c56fb4bc5f0 | 4 | #if defined(TARGET_LPC812) |
nexpaq | 0:6c56fb4bc5f0 | 5 | ADXL345 accelerometer(D10, D11, D12, D13); |
nexpaq | 0:6c56fb4bc5f0 | 6 | |
nexpaq | 0:6c56fb4bc5f0 | 7 | #else |
nexpaq | 0:6c56fb4bc5f0 | 8 | ADXL345 accelerometer(p5, p6, p7, p8); |
nexpaq | 0:6c56fb4bc5f0 | 9 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 10 | |
nexpaq | 0:6c56fb4bc5f0 | 11 | // Assume test configuration on a plane (small x and y, z ~ g) |
nexpaq | 0:6c56fb4bc5f0 | 12 | #define MAX_X_Y (50) |
nexpaq | 0:6c56fb4bc5f0 | 13 | #define MIN_Z (200) |
nexpaq | 0:6c56fb4bc5f0 | 14 | #define MAX_Z (300) |
nexpaq | 0:6c56fb4bc5f0 | 15 | |
nexpaq | 0:6c56fb4bc5f0 | 16 | void check_X_Y(int v) { |
nexpaq | 0:6c56fb4bc5f0 | 17 | int16_t a = (int16_t)v; |
nexpaq | 0:6c56fb4bc5f0 | 18 | if (abs(a) > MAX_X_Y) { |
nexpaq | 0:6c56fb4bc5f0 | 19 | printf("X/Y acceleration is too big: %d\n", a); |
nexpaq | 0:6c56fb4bc5f0 | 20 | notify_completion(false); |
nexpaq | 0:6c56fb4bc5f0 | 21 | } |
nexpaq | 0:6c56fb4bc5f0 | 22 | } |
nexpaq | 0:6c56fb4bc5f0 | 23 | |
nexpaq | 0:6c56fb4bc5f0 | 24 | |
nexpaq | 0:6c56fb4bc5f0 | 25 | int main() { |
nexpaq | 0:6c56fb4bc5f0 | 26 | int readings[3] = {0, 0, 0}; |
nexpaq | 0:6c56fb4bc5f0 | 27 | |
nexpaq | 0:6c56fb4bc5f0 | 28 | printf("Starting ADXL345 test...\n"); |
nexpaq | 0:6c56fb4bc5f0 | 29 | printf("Device ID is: 0x%02x\n", accelerometer.getDevId()); |
nexpaq | 0:6c56fb4bc5f0 | 30 | |
nexpaq | 0:6c56fb4bc5f0 | 31 | //Go into standby mode to configure the device. |
nexpaq | 0:6c56fb4bc5f0 | 32 | accelerometer.setPowerControl(0x00); |
nexpaq | 0:6c56fb4bc5f0 | 33 | |
nexpaq | 0:6c56fb4bc5f0 | 34 | //Full resolution, +/-16g, 4mg/LSB. |
nexpaq | 0:6c56fb4bc5f0 | 35 | accelerometer.setDataFormatControl(0x0B); |
nexpaq | 0:6c56fb4bc5f0 | 36 | |
nexpaq | 0:6c56fb4bc5f0 | 37 | //3.2kHz data rate. |
nexpaq | 0:6c56fb4bc5f0 | 38 | accelerometer.setDataRate(ADXL345_3200HZ); |
nexpaq | 0:6c56fb4bc5f0 | 39 | |
nexpaq | 0:6c56fb4bc5f0 | 40 | //Measurement mode. |
nexpaq | 0:6c56fb4bc5f0 | 41 | accelerometer.setPowerControl(0x08); |
nexpaq | 0:6c56fb4bc5f0 | 42 | |
nexpaq | 0:6c56fb4bc5f0 | 43 | for (int i=0; i<3; i++) { |
nexpaq | 0:6c56fb4bc5f0 | 44 | wait(0.1); |
nexpaq | 0:6c56fb4bc5f0 | 45 | |
nexpaq | 0:6c56fb4bc5f0 | 46 | //13-bit, sign extended values. |
nexpaq | 0:6c56fb4bc5f0 | 47 | accelerometer.getOutput(readings); |
nexpaq | 0:6c56fb4bc5f0 | 48 | |
nexpaq | 0:6c56fb4bc5f0 | 49 | // X and Y |
nexpaq | 0:6c56fb4bc5f0 | 50 | check_X_Y(readings[0]); |
nexpaq | 0:6c56fb4bc5f0 | 51 | check_X_Y(readings[1]); |
nexpaq | 0:6c56fb4bc5f0 | 52 | |
nexpaq | 0:6c56fb4bc5f0 | 53 | // Z |
nexpaq | 0:6c56fb4bc5f0 | 54 | int16_t z = (int16_t)readings[2]; |
nexpaq | 0:6c56fb4bc5f0 | 55 | if ((z < MIN_Z) || (z > MAX_Z)) { |
nexpaq | 0:6c56fb4bc5f0 | 56 | printf("Z acceleration not within expected range\n", z); |
nexpaq | 0:6c56fb4bc5f0 | 57 | notify_completion(false); |
nexpaq | 0:6c56fb4bc5f0 | 58 | } |
nexpaq | 0:6c56fb4bc5f0 | 59 | } |
nexpaq | 0:6c56fb4bc5f0 | 60 | |
nexpaq | 0:6c56fb4bc5f0 | 61 | notify_completion(true); |
nexpaq | 0:6c56fb4bc5f0 | 62 | } |