You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.3 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "ADXL345_I2C.h"
  2. ADXL345_I2C accelerometer( p28, p27 );
  3. Serial pc(USBTX, USBRX);
  4. int main() {
  5. pc.baud(115200);
  6. int readings[3] = {0, 0, 0};
  7. pc.printf("Starting ADXL345 test...\n");
  8. wait(.001);
  9. pc.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
  10. wait(.001);
  11. // These are here to test whether any of the initialization fails. It will print the failure
  12. if (accelerometer.setPowerControl(0x00)){
  13. pc.printf("didn't intitialize power control\n");
  14. return 0; }
  15. //Full resolution, +/-16g, 4mg/LSB.
  16. wait(.001);
  17. if(accelerometer.setDataFormatControl(0x0B)){
  18. pc.printf("didn't set data format\n");
  19. return 0; }
  20. wait(.001);
  21. //3.2kHz data rate.
  22. if(accelerometer.setDataRate(ADXL345_3200HZ)){
  23. pc.printf("didn't set data rate\n");
  24. return 0; }
  25. wait(.001);
  26. //Measurement mode.
  27. if(accelerometer.setPowerControl(MeasurementMode)) {
  28. pc.printf("didn't set the power control to measurement\n");
  29. return 0; }
  30. while (1) {
  31. wait(0.1);
  32. accelerometer.getOutput(readings);
  33. pc.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
  34. }
  35. }