MMA7660.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. MMA7760.h
  3. Library for accelerometer_MMA7760
  4. Copyright (c) 2013 seeed technology inc.
  5. Author : FrankieChu
  6. Create Time : Jan 2013
  7. Change Log :
  8. The MIT License (MIT)
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. THE SOFTWARE.
  24. */
  25. #ifndef __MMC7660_H__
  26. #define __MMC7660_H__
  27. #define MMA7660_ADDR 0x4c
  28. #define MMA7660_X 0x00
  29. #define MMA7660_Y 0x01
  30. #define MMA7660_Z 0x02
  31. #define MMA7660_TILT 0x03
  32. #define MMA7660_SRST 0x04
  33. #define MMA7660_SPCNT 0x05
  34. #define MMA7660_INTSU 0x06
  35. #define MMA7660_SHINTX 0x80
  36. #define MMA7660_SHINTY 0x40
  37. #define MMA7660_SHINTZ 0x20
  38. #define MMA7660_GINT 0x10
  39. #define MMA7660_ASINT 0x08
  40. #define MMA7660_PDINT 0x04
  41. #define MMA7660_PLINT 0x02
  42. #define MMA7660_FBINT 0x01
  43. #define MMA7660_MODE 0x07
  44. #define MMA7660_STAND_BY 0x00
  45. #define MMA7660_ACTIVE 0x01
  46. #define MMA7660_SR 0x08 //sample rate register
  47. #define AUTO_SLEEP_120 0X00//120 sample per second
  48. #define AUTO_SLEEP_64 0X01
  49. #define AUTO_SLEEP_32 0X02
  50. #define AUTO_SLEEP_16 0X03
  51. #define AUTO_SLEEP_8 0X04
  52. #define AUTO_SLEEP_4 0X05
  53. #define AUTO_SLEEP_2 0X06
  54. #define AUTO_SLEEP_1 0X07
  55. #define MMA7660_PDET 0x09
  56. #define MMA7660_PD 0x0A
  57. struct MMA7660_DATA {
  58. uint8_t X;
  59. uint8_t Y;
  60. uint8_t Z;
  61. uint8_t TILT;
  62. uint8_t SRST;
  63. uint8_t SPCNT;
  64. uint8_t INTSU;
  65. uint8_t MODE;
  66. uint8_t SR;
  67. uint8_t PDET;
  68. uint8_t PD;
  69. };
  70. struct MMA7660_LOOKUP {
  71. float g;
  72. float xyAngle;
  73. float zAngle;
  74. };
  75. struct MMA7660_ACC_DATA {
  76. MMA7660_LOOKUP x;
  77. MMA7660_LOOKUP y;
  78. MMA7660_LOOKUP z;
  79. };
  80. class MMA7660 {
  81. private:
  82. void write(uint8_t _register, uint8_t _data);
  83. uint8_t read(uint8_t _register);
  84. // populate lookup table based on the MMA7660 datasheet at
  85. // https://files.seeedstudio.com/wiki/Grove-3-Axis_Digital_Accelerometer-1.5g/res/MMA7660FC.pdf
  86. static constexpr MMA7660_LOOKUP accLookup[] = {
  87. { 0.000f, 0.00f, 90.00f }, // 0
  88. { 0.047f, 2.69f, 87.31f }, // 1
  89. { 0.094f, 5.38f, 84.62f }, // 2
  90. { 0.141f, 8.08f, 81.92f }, // 3
  91. { 0.188f, 10.81f, 79.19f }, // 4
  92. { 0.234f, 13.55f, 76.45f }, // 5
  93. { 0.281f, 16.33f, 73.67f }, // 6
  94. { 0.328f, 19.16f, 70.84f }, // 7
  95. { 0.375f, 22.02f, 67.98f }, // 8
  96. { 0.422f, 24.95f, 65.05f }, // 9
  97. { 0.469f, 27.95f, 62.05f }, // 10
  98. { 0.516f, 31.04f, 58.96f }, // 11
  99. { 0.563f, 34.23f, 55.77f }, // 12
  100. { 0.609f, 37.54f, 52.46f }, // 13
  101. { 0.656f, 41.01f, 48.99f }, // 14
  102. { 0.703f, 44.68f, 45.32f }, // 15
  103. { 0.750f, 48.59f, 41.41f }, // 16
  104. { 0.797f, 52.83f, 37.17f }, // 17
  105. { 0.844f, 57.54f, 32.46f }, // 18
  106. { 0.891f, 62.95f, 27.05f }, // 19
  107. { 0.938f, 69.64f, 20.36f }, // 20
  108. { 0.984f, 79.86f, 10.14f }, // 21
  109. { 1.031f, 0.00f, 0.00f }, // 22 (Not in Datasheet)
  110. { 1.078f, 0.00f, 0.00f }, // 23 (Not in Datasheet)
  111. { 1.125f, 0.00f, 0.00f }, // 24 (Not in Datasheet)
  112. { 1.172f, 0.00f, 0.00f }, // 25 (Not in Datasheet)
  113. { 1.219f, 0.00f, 0.00f }, // 26 (Not in Datasheet)
  114. { 1.266f, 0.00f, 0.00f }, // 27 (Not in Datasheet)
  115. { 1.313f, 0.00f, 0.00f }, // 28 (Not in Datasheet)
  116. { 1.359f, 0.00f, 0.00f }, // 29 (Not in Datasheet)
  117. { 1.406f, 0.00f, 0.00f }, // 30 (Not in Datasheet)
  118. { 1.453f, 0.00f, 0.00f }, // 31 (Not in Datasheet)
  119. { -0.047f, -2.69f, -87.31f }, // -1
  120. { -0.094f, -5.38f, -84.62f }, // -2
  121. { -0.141f, -8.08f, -81.92f }, // -3
  122. { -0.188f, -10.81f, -79.19f }, // -4
  123. { -0.234f, -13.55f, -76.45f }, // -5
  124. { -0.281f, -16.33f, -73.67f }, // -6
  125. { -0.328f, -19.16f, -70.84f }, // -7
  126. { -0.375f, -22.02f, -67.98f }, // -8
  127. { -0.422f, -24.95f, -65.05f }, // -9
  128. { -0.469f, -27.95f, -62.05f }, // -10
  129. { -0.516f, -31.04f, -58.96f }, // -11
  130. { -0.563f, -34.23f, -55.77f }, // -12
  131. { -0.609f, -37.54f, -52.46f }, // -13
  132. { -0.656f, -41.01f, -48.99f }, // -14
  133. { -0.703f, -44.68f, -45.32f }, // -15
  134. { -0.750f, -48.59f, -41.41f }, // -16
  135. { -0.797f, -52.83f, -37.17f }, // -17
  136. { -0.844f, -57.54f, -32.46f }, // -18
  137. { -0.891f, -62.95f, -27.05f }, // -19
  138. { -0.938f, -69.64f, -20.36f }, // -20
  139. { -0.984f, -79.86f, -10.14f }, // -21
  140. { -1.031f, 0.00f, 0.00f }, // -22 (Wert fehlt im Datenblatt)
  141. { -1.078f, 0.00f, 0.00f }, // -23 (Wert fehlt im Datenblatt)
  142. { -1.125f, 0.00f, 0.00f }, // -24 (Wert fehlt im Datenblatt)
  143. { -1.172f, 0.00f, 0.00f }, // -25 (Wert fehlt im Datenblatt)
  144. { -1.219f, 0.00f, 0.00f }, // -26 (Wert fehlt im Datenblatt)
  145. { -1.266f, 0.00f, 0.00f }, // -27 (Wert fehlt im Datenblatt)
  146. { -1.313f, 0.00f, 0.00f }, // -28 (Wert fehlt im Datenblatt)
  147. { -1.359f, 0.00f, 0.00f }, // -29 (Wert fehlt im Datenblatt)
  148. { -1.406f, 0.00f, 0.00f }, // -30 (Wert fehlt im Datenblatt)
  149. { -1.453f, 0.00f, 0.00f }, // -31 (Wert fehlt im Datenblatt)
  150. { -1.500f, 0.00f, 0.00f } // -32 (Wert fehlt im Datenblatt)
  151. };
  152. public:
  153. void init();
  154. void init(uint8_t interrupts);
  155. void setMode(uint8_t mode);
  156. void setSampleRate(uint8_t rate);
  157. // get the signed value of x,y,z register
  158. bool getXYZ(int8_t* x, int8_t* y, int8_t* z);
  159. // calculate the acceleration from the signed value of x,y,z register
  160. bool getAcceleration(float* ax, float* ay, float* az);
  161. // lookup the acceleration from the lookup table from this chip's datasheet
  162. bool getAcceleration(MMA7660_ACC_DATA* data);
  163. // get all the register value
  164. bool getAllData(MMA7660_DATA* data);
  165. };
  166. #endif