accelerometer example for Xadow M0 on-board ADXL345
Dependencies: ADXL345_I2C USBDevice mbed
main.cpp@1:769899f50706, 2014-08-20 (annotated)
- Committer:
- yihui
- Date:
- Wed Aug 20 09:13:25 2014 +0000
- Revision:
- 1:769899f50706
- Parent:
- 0:b017c907d53d
fix USBDevice compile error, use a custom USBDevice lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
seeed | 0:b017c907d53d | 1 | #include "ADXL345_I2C.h" |
seeed | 0:b017c907d53d | 2 | |
seeed | 0:b017c907d53d | 3 | #define DEBUG |
seeed | 0:b017c907d53d | 4 | |
seeed | 0:b017c907d53d | 5 | #ifdef DEBUG |
seeed | 0:b017c907d53d | 6 | #include "USBSerial.h" // To use USB virtual serial, a driver is needed, check http://mbed.org/handbook/USBSerial |
seeed | 0:b017c907d53d | 7 | #define LOG(args...) pc.printf(args) |
seeed | 0:b017c907d53d | 8 | USBSerial pc; |
seeed | 0:b017c907d53d | 9 | #else |
seeed | 0:b017c907d53d | 10 | #define LOG(args...) |
seeed | 0:b017c907d53d | 11 | #endif |
seeed | 0:b017c907d53d | 12 | |
seeed | 0:b017c907d53d | 13 | ADXL345_I2C accelerometer(P0_5, P0_4); |
seeed | 0:b017c907d53d | 14 | |
seeed | 0:b017c907d53d | 15 | int main() |
seeed | 0:b017c907d53d | 16 | { |
seeed | 0:b017c907d53d | 17 | int readings[3] = {0, 0, 0}; |
seeed | 0:b017c907d53d | 18 | |
seeed | 0:b017c907d53d | 19 | LOG("Starting ADXL345 test...\n"); |
seeed | 0:b017c907d53d | 20 | LOG("Device ID is: 0x%02x\n", accelerometer.getDeviceID()); |
seeed | 0:b017c907d53d | 21 | |
seeed | 0:b017c907d53d | 22 | //Go into standby mode to configure the device. |
seeed | 0:b017c907d53d | 23 | accelerometer.setPowerControl(0x00); |
seeed | 0:b017c907d53d | 24 | |
seeed | 0:b017c907d53d | 25 | //Full resolution, +/-16g, 4mg/LSB. |
seeed | 0:b017c907d53d | 26 | accelerometer.setDataFormatControl(0x0B); |
seeed | 0:b017c907d53d | 27 | |
seeed | 0:b017c907d53d | 28 | //3.2kHz data rate. |
seeed | 0:b017c907d53d | 29 | accelerometer.setDataRate(ADXL345_3200HZ); |
seeed | 0:b017c907d53d | 30 | |
seeed | 0:b017c907d53d | 31 | //Measurement mode. |
seeed | 0:b017c907d53d | 32 | accelerometer.setPowerControl(0x08); |
seeed | 0:b017c907d53d | 33 | |
seeed | 0:b017c907d53d | 34 | while (1) { |
seeed | 0:b017c907d53d | 35 | accelerometer.getOutput(readings); |
seeed | 0:b017c907d53d | 36 | LOG("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]); |
seeed | 0:b017c907d53d | 37 | |
seeed | 0:b017c907d53d | 38 | wait(1); |
seeed | 0:b017c907d53d | 39 | } |
seeed | 0:b017c907d53d | 40 | |
seeed | 0:b017c907d53d | 41 | } |