library for ADXL345 Accelerometer
Fork of ADXL345_I2C by
main.cpp@1:d9412b56f98a, 2011-05-12 (annotated)
- Committer:
- peterswanson87
- Date:
- Thu May 12 01:19:36 2011 +0000
- Revision:
- 1:d9412b56f98a
- Parent:
- 0:d0adb548714f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
peterswanson87 | 0:d0adb548714f | 1 | #include "ADXL345_I2C.h" |
peterswanson87 | 0:d0adb548714f | 2 | |
peterswanson87 | 0:d0adb548714f | 3 | ADXL345_I2C accelerometer(p28, p27); |
peterswanson87 | 0:d0adb548714f | 4 | Serial pc(USBTX, USBRX); |
peterswanson87 | 0:d0adb548714f | 5 | |
peterswanson87 | 0:d0adb548714f | 6 | int main() { |
peterswanson87 | 0:d0adb548714f | 7 | pc.baud(115200); |
peterswanson87 | 0:d0adb548714f | 8 | int readings[3] = {0, 0, 0}; |
peterswanson87 | 0:d0adb548714f | 9 | |
peterswanson87 | 0:d0adb548714f | 10 | pc.printf("Starting ADXL345 test...\n"); |
peterswanson87 | 0:d0adb548714f | 11 | wait(.001); |
peterswanson87 | 0:d0adb548714f | 12 | pc.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID()); |
peterswanson87 | 0:d0adb548714f | 13 | wait(.001); |
peterswanson87 | 0:d0adb548714f | 14 | |
peterswanson87 | 0:d0adb548714f | 15 | // These are here to test whether any of the initialization fails. It will print the failure |
peterswanson87 | 0:d0adb548714f | 16 | if (accelerometer.setPowerControl(0x00)){ |
peterswanson87 | 0:d0adb548714f | 17 | pc.printf("didn't intitialize power control\n"); |
peterswanson87 | 0:d0adb548714f | 18 | return 0; } |
peterswanson87 | 0:d0adb548714f | 19 | //Full resolution, +/-16g, 4mg/LSB. |
peterswanson87 | 0:d0adb548714f | 20 | wait(.001); |
peterswanson87 | 0:d0adb548714f | 21 | |
peterswanson87 | 0:d0adb548714f | 22 | if(accelerometer.setDataFormatControl(0x0B)){ |
peterswanson87 | 0:d0adb548714f | 23 | pc.printf("didn't set data format\n"); |
peterswanson87 | 0:d0adb548714f | 24 | return 0; } |
peterswanson87 | 0:d0adb548714f | 25 | wait(.001); |
peterswanson87 | 0:d0adb548714f | 26 | |
peterswanson87 | 0:d0adb548714f | 27 | //3.2kHz data rate. |
peterswanson87 | 0:d0adb548714f | 28 | if(accelerometer.setDataRate(ADXL345_3200HZ)){ |
peterswanson87 | 0:d0adb548714f | 29 | pc.printf("didn't set data rate\n"); |
peterswanson87 | 0:d0adb548714f | 30 | return 0; } |
peterswanson87 | 0:d0adb548714f | 31 | wait(.001); |
peterswanson87 | 0:d0adb548714f | 32 | |
peterswanson87 | 0:d0adb548714f | 33 | //Measurement mode. |
peterswanson87 | 0:d0adb548714f | 34 | |
peterswanson87 | 0:d0adb548714f | 35 | if(accelerometer.setPowerControl(MeasurementMode)) { |
peterswanson87 | 0:d0adb548714f | 36 | pc.printf("didn't set the power control to measurement\n"); |
peterswanson87 | 0:d0adb548714f | 37 | return 0; } |
peterswanson87 | 0:d0adb548714f | 38 | |
peterswanson87 | 0:d0adb548714f | 39 | while (1) { |
peterswanson87 | 0:d0adb548714f | 40 | |
peterswanson87 | 0:d0adb548714f | 41 | wait(0.1); |
peterswanson87 | 0:d0adb548714f | 42 | |
peterswanson87 | 0:d0adb548714f | 43 | accelerometer.getOutput(readings); |
peterswanson87 | 0:d0adb548714f | 44 | |
peterswanson87 | 0:d0adb548714f | 45 | |
peterswanson87 | 0:d0adb548714f | 46 | pc.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]); |
peterswanson87 | 0:d0adb548714f | 47 | } |
peterswanson87 | 0:d0adb548714f | 48 | |
peterswanson87 | 0:d0adb548714f | 49 | } |