Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

UserFunctions.cpp 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "UserTypes.h"
  2. // User data functions. Modify these functions for your data items.
  3. // Start time for data
  4. static uint32_t startMicros;
  5. const uint8_t ADXL345_CS = 9;
  6. const uint8_t POWER_CTL = 0x2D; //Power Control Register
  7. const uint8_t DATA_FORMAT = 0x31;
  8. const uint8_t DATAX0 = 0x32; //X-Axis Data 0
  9. const uint8_t DATAX1 = 0x33; //X-Axis Data 1
  10. const uint8_t DATAY0 = 0x34; //Y-Axis Data 0
  11. const uint8_t DATAY1 = 0x35; //Y-Axis Data 1
  12. const uint8_t DATAZ0 = 0x36; //Z-Axis Data 0
  13. const uint8_t DATAZ1 = 0x37; //Z-Axis Data 1
  14. void writeADXL345Register(const uint8_t registerAddress, const uint8_t value) {
  15. // Max SPI clock frequency is 5 MHz with CPOL = 1 and CPHA = 1.
  16. SPI.beginTransaction(SPISettings(5000000, MSBFIRST, SPI_MODE3));
  17. digitalWrite(ADXL345_CS, LOW);
  18. SPI.transfer(registerAddress);
  19. SPI.transfer(value);
  20. digitalWrite(ADXL345_CS, HIGH);
  21. SPI.endTransaction();
  22. }
  23. void userSetup() {
  24. SPI.begin();
  25. pinMode(ADXL345_CS, OUTPUT);
  26. digitalWrite(ADXL345_CS, HIGH);
  27. //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
  28. writeADXL345Register(DATA_FORMAT, 0x01);
  29. //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
  30. writeADXL345Register(POWER_CTL, 0x08); //Measurement mode
  31. }
  32. // Acquire a data record.
  33. void acquireData(data_t* data) {
  34. // Max SPI clock frequency is 5 MHz with CPOL = 1 and CPHA = 1.
  35. SPI.beginTransaction(SPISettings(5000000, MSBFIRST, SPI_MODE3));
  36. data->time = micros();
  37. digitalWrite(ADXL345_CS, LOW);
  38. // Read multiple bytes so or 0XC0 with address.
  39. SPI.transfer(DATAX0 | 0XC0);
  40. data->accel[0] = SPI.transfer(0) | (SPI.transfer(0) << 8);
  41. data->accel[1] = SPI.transfer(0) | (SPI.transfer(0) << 8);
  42. data->accel[2] = SPI.transfer(0) | (SPI.transfer(0) << 8);
  43. digitalWrite(ADXL345_CS, HIGH);
  44. SPI.endTransaction();
  45. }
  46. // Print a data record.
  47. void printData(Print* pr, data_t* data) {
  48. if (startMicros == 0) {
  49. startMicros = data->time;
  50. }
  51. pr->print(data->time - startMicros);
  52. for (int i = 0; i < ACCEL_DIM; i++) {
  53. pr->write(',');
  54. pr->print(data->accel[i]);
  55. }
  56. pr->println();
  57. }
  58. // Print data header.
  59. void printHeader(Print* pr) {
  60. startMicros = 0;
  61. pr->println(F("micros,ax,ay,az"));
  62. }