Frederick Huang
/
ADXL345Test
ADXL345 Evaluation
main.cpp
- Committer:
- Frederick_H
- Date:
- 2017-08-21
- Revision:
- 3:e4783c57bcc0
- Parent:
- 2:0bc5af6c7ebf
- Child:
- 4:f5a78245f2d0
File content as of revision 3:e4783c57bcc0:
#include "ADXL345_I2C.h" ADXL345_I2C accelerometer(I2C_SDA, I2C_SCL); Serial pc(USBTX, USBRX); 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]); } } }