TEST1

Dependencies:   TM1638 mbed

Fork of ADXL345Test by jack zen

Committer:
Frederick_H
Date:
Mon Aug 21 02:40:56 2017 +0000
Revision:
3:e4783c57bcc0
Parent:
2:0bc5af6c7ebf
Child:
4:f5a78245f2d0
Test purpose

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Frederick_H 3:e4783c57bcc0 1 #include "ADXL345_I2C.h"
screamer 0:51be560629b8 2
Frederick_H 3:e4783c57bcc0 3 ADXL345_I2C accelerometer(I2C_SDA, I2C_SCL);
Frederick_H 3:e4783c57bcc0 4 Serial pc(USBTX, USBRX);
screamer 0:51be560629b8 5
Frederick_H 3:e4783c57bcc0 6 int main() {
Frederick_H 3:e4783c57bcc0 7 pc.baud(115200);
Frederick_H 3:e4783c57bcc0 8 int readings[3] = {0, 0, 0};
Frederick_H 3:e4783c57bcc0 9
Frederick_H 3:e4783c57bcc0 10 pc.printf("Starting ADXL345 test...\r\n");
Frederick_H 3:e4783c57bcc0 11 wait(.001);
Frederick_H 3:e4783c57bcc0 12 pc.printf("Device ID is: 0x%02x\r\n", accelerometer.getDeviceID());
Frederick_H 3:e4783c57bcc0 13 wait(.001);
Frederick_H 3:e4783c57bcc0 14
Frederick_H 3:e4783c57bcc0 15 restart:
Frederick_H 3:e4783c57bcc0 16
Frederick_H 3:e4783c57bcc0 17 // These are here to test whether any of the initialization fails. It will print the failure
Frederick_H 3:e4783c57bcc0 18 if (accelerometer.setPowerControl(0x00)){
Frederick_H 3:e4783c57bcc0 19 pc.printf("didn't intitialize power control\r\n");
Frederick_H 3:e4783c57bcc0 20 return 0; }
Frederick_H 3:e4783c57bcc0 21 wait(.001);
Frederick_H 3:e4783c57bcc0 22
Frederick_H 3:e4783c57bcc0 23 //Full resolution, +/-16g, 4mg/LSB.
Frederick_H 3:e4783c57bcc0 24 if(accelerometer.setDataFormatControl(0x0B)){
Frederick_H 3:e4783c57bcc0 25 //Full resolution, +/-2g, 4mg/LSB.
Frederick_H 3:e4783c57bcc0 26 //if(accelerometer.setDataFormatControl(0x08)){
Frederick_H 3:e4783c57bcc0 27 pc.printf("didn't set data format\r\n");
Frederick_H 3:e4783c57bcc0 28 return 0; }
Frederick_H 3:e4783c57bcc0 29 wait(.001);
Frederick_H 3:e4783c57bcc0 30
Frederick_H 3:e4783c57bcc0 31 //3.2kHz data rate.
Frederick_H 3:e4783c57bcc0 32 if(accelerometer.setDataRate(ADXL345_3200HZ)){
Frederick_H 3:e4783c57bcc0 33 pc.printf("didn't set data rate\r\n");
Frederick_H 3:e4783c57bcc0 34 return 0; }
Frederick_H 3:e4783c57bcc0 35 wait(.001);
Frederick_H 3:e4783c57bcc0 36
Frederick_H 3:e4783c57bcc0 37 //Measurement mode.
Frederick_H 3:e4783c57bcc0 38
Frederick_H 3:e4783c57bcc0 39 if(accelerometer.setPowerControl(MeasurementMode)) {
Frederick_H 3:e4783c57bcc0 40 pc.printf("didn't set the power control to measurement\r\n");
Frederick_H 3:e4783c57bcc0 41 return 0; }
Frederick_H 3:e4783c57bcc0 42
Frederick_H 3:e4783c57bcc0 43 pc.printf("x-axis, y-axis, z-axis\r\n");
Frederick_H 3:e4783c57bcc0 44
Frederick_H 3:e4783c57bcc0 45 while (1) {
Frederick_H 3:e4783c57bcc0 46 int error_count=0;
Frederick_H 3:e4783c57bcc0 47
Frederick_H 3:e4783c57bcc0 48 wait(0.01);
Frederick_H 3:e4783c57bcc0 49
Frederick_H 3:e4783c57bcc0 50 accelerometer.getOutput(readings);
Frederick_H 3:e4783c57bcc0 51 if (( 17601 == readings[0] ) || ( 17601 == readings[1] ) || ( 17601 == readings[2] ))
Frederick_H 3:e4783c57bcc0 52 {
Frederick_H 3:e4783c57bcc0 53 error_count++;
Frederick_H 3:e4783c57bcc0 54 if (error_count>10)
Frederick_H 3:e4783c57bcc0 55 {
Frederick_H 3:e4783c57bcc0 56 accelerometer.setPowerControl(0);
Frederick_H 3:e4783c57bcc0 57 pc.printf("Sensor Halt!\r\n");
Frederick_H 3:e4783c57bcc0 58 goto restart;
Frederick_H 3:e4783c57bcc0 59 }
Frederick_H 3:e4783c57bcc0 60 }
Frederick_H 3:e4783c57bcc0 61 else
Frederick_H 3:e4783c57bcc0 62 {
Frederick_H 3:e4783c57bcc0 63 error_count = 0;
Frederick_H 3:e4783c57bcc0 64 pc.printf("%i, %i, %i\r\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
Frederick_H 3:e4783c57bcc0 65 }
Frederick_H 3:e4783c57bcc0 66 }
Frederick_H 3:e4783c57bcc0 67
Frederick_H 3:e4783c57bcc0 68 }