Frederick Huang
/
ADXL345Test
ADXL345 Evaluation
Diff: main.cpp
- Revision:
- 3:e4783c57bcc0
- Parent:
- 2:0bc5af6c7ebf
- Child:
- 4:f5a78245f2d0
diff -r 0bc5af6c7ebf -r e4783c57bcc0 main.cpp --- a/main.cpp Wed Aug 17 09:58:13 2016 +0000 +++ b/main.cpp Mon Aug 21 02:40:56 2017 +0000 @@ -1,17 +1,68 @@ -#include "mbed.h" +#include "ADXL345_I2C.h" -DigitalOut red(LED1); -DigitalOut blue(LED2); -DigitalOut green(LED3); -int i; + ADXL345_I2C accelerometer(I2C_SDA, I2C_SCL); + Serial pc(USBTX, USBRX); -int main() { - while(1) { - for (i=1; i<7; i++) { - red = i & 1; - blue = i & 2; - green = i & 4; - wait(0.2); - } - } -} + int main() { + pc.baud(115200); + int readings[3] = {0, 0, 0}; + + pc.printf("Starting ADXL345 test...\r\n"); + wait(.001); + pc.printf("Device ID is: 0x%02x\r\n", accelerometer.getDeviceID()); + wait(.001); + + restart: + + // These are here to test whether any of the initialization fails. It will print the failure + if (accelerometer.setPowerControl(0x00)){ + pc.printf("didn't intitialize power control\r\n"); + return 0; } + wait(.001); + + //Full resolution, +/-16g, 4mg/LSB. + if(accelerometer.setDataFormatControl(0x0B)){ + //Full resolution, +/-2g, 4mg/LSB. + //if(accelerometer.setDataFormatControl(0x08)){ + pc.printf("didn't set data format\r\n"); + return 0; } + wait(.001); + + //3.2kHz data rate. + if(accelerometer.setDataRate(ADXL345_3200HZ)){ + pc.printf("didn't set data rate\r\n"); + return 0; } + wait(.001); + + //Measurement mode. + + if(accelerometer.setPowerControl(MeasurementMode)) { + pc.printf("didn't set the power control to measurement\r\n"); + return 0; } + + pc.printf("x-axis, y-axis, z-axis\r\n"); + + while (1) { + int error_count=0; + + wait(0.01); + + accelerometer.getOutput(readings); + if (( 17601 == readings[0] ) || ( 17601 == readings[1] ) || ( 17601 == readings[2] )) + { + error_count++; + if (error_count>10) + { + accelerometer.setPowerControl(0); + pc.printf("Sensor Halt!\r\n"); + goto restart; + } + } + else + { + error_count = 0; + pc.printf("%i, %i, %i\r\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]); + } + } + + }