MMA7660.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #include <Arduino.h>
  26. #include <Wire.h>
  27. #include "MMA7660.h"
  28. #define MMA7660TIMEOUT 500 // us
  29. /*Function: Write a byte to the register of the MMA7660*/
  30. void MMA7660::write(uint8_t _register, uint8_t _data) {
  31. Wire.begin();
  32. Wire.beginTransmission(MMA7660_ADDR);
  33. Wire.write(_register);
  34. Wire.write(_data);
  35. Wire.endTransmission();
  36. }
  37. /*Function: Read a byte from the regitster of the MMA7660*/
  38. uint8_t MMA7660::read(uint8_t _register) {
  39. uint8_t data_read;
  40. Wire.begin();
  41. Wire.beginTransmission(MMA7660_ADDR);
  42. Wire.write(_register);
  43. Wire.endTransmission();
  44. Wire.beginTransmission(MMA7660_ADDR);
  45. Wire.requestFrom(MMA7660_ADDR, 1);
  46. while (Wire.available()) {
  47. data_read = Wire.read();
  48. }
  49. Wire.endTransmission();
  50. return data_read;
  51. }
  52. void MMA7660::init() {
  53. setMode(MMA7660_STAND_BY);
  54. setSampleRate(AUTO_SLEEP_32);
  55. setMode(MMA7660_ACTIVE);
  56. }
  57. void MMA7660::init(uint8_t interrupts) {
  58. setMode(MMA7660_STAND_BY);
  59. setSampleRate(AUTO_SLEEP_32);
  60. write(MMA7660_INTSU, interrupts);
  61. setMode(MMA7660_ACTIVE);
  62. }
  63. void MMA7660::setMode(uint8_t mode) {
  64. write(MMA7660_MODE, mode);
  65. }
  66. void MMA7660::setSampleRate(uint8_t rate) {
  67. write(MMA7660_SR, rate);
  68. }
  69. /*Function: Get the contents of the registers in the MMA7660*/
  70. /* so as to calculate the acceleration. */
  71. bool MMA7660::getXYZ(int8_t* x, int8_t* y, int8_t* z) {
  72. START:
  73. unsigned char val[3];
  74. int count = 0;
  75. val[0] = val[1] = val[2] = 64;
  76. while (Wire.available() > 0) {
  77. Wire.read();
  78. }
  79. Wire.requestFrom(MMA7660_ADDR, 3);
  80. unsigned long timer_s = micros();
  81. while (Wire.available()) {
  82. if (count < 3) {
  83. while (val[count] > 63) { // reload the damn thing it is bad
  84. val[count] = Wire.read();
  85. if (micros() - timer_s > MMA7660TIMEOUT) {
  86. goto START;
  87. }
  88. }
  89. }
  90. count++;
  91. }
  92. *x = ((int8_t)(val[0] << 2)) / 4;
  93. *y = ((int8_t)(val[1] << 2)) / 4;
  94. *z = ((int8_t)(val[2] << 2)) / 4;
  95. return 1;
  96. }
  97. bool MMA7660::getAcceleration(float* ax, float* ay, float* az) {
  98. int8_t x, y, z;
  99. if (!getXYZ(&x, &y, &z)) {
  100. return 0;
  101. }
  102. *ax = x / 21.00;
  103. *ay = y / 21.00;
  104. *az = z / 21.00;
  105. return 1;
  106. }
  107. bool MMA7660::getAcceleration(MMA7660_ACC_DATA* data) {
  108. unsigned char val[3];
  109. int count;
  110. bool error;
  111. unsigned long timer_s = micros();
  112. do {
  113. error = false;
  114. count = 0;
  115. while (Wire.available() > 0) {
  116. Wire.read();
  117. }
  118. Wire.requestFrom(MMA7660_ADDR, 3);
  119. while (Wire.available()) {
  120. if (count < 3) {
  121. val[count] = Wire.read();
  122. if (0x40 & val[count] == 0x40) { // alert bit is set, data is garbage and we have to start over.
  123. error = true;
  124. break;
  125. }
  126. }
  127. count++;
  128. }
  129. if (micros() - timer_s > MMA7660TIMEOUT) {
  130. return 0;
  131. }
  132. } while (error);
  133. (*data).x = accLookup[val[0]];
  134. (*data).y = accLookup[val[1]];
  135. (*data).z = accLookup[val[2]];
  136. return 1;
  137. }
  138. bool MMA7660::getAllData(MMA7660_DATA* data) {
  139. int count = 0;
  140. uint8_t val[11] = {0};
  141. while (Wire.available() > 0) {
  142. Wire.read();
  143. }
  144. Wire.requestFrom(MMA7660_ADDR, 11);
  145. while (Wire.available()) {
  146. if (count < 11) {
  147. val[count] = Wire.read();
  148. }
  149. count++;
  150. }
  151. data->X = val[0];
  152. data->Y = val[1];
  153. data->Z = val[2];
  154. data->TILT = val[3];
  155. data->SRST = val[4];
  156. data->SPCNT = val[5];
  157. data->INTSU = val[6];
  158. data->MODE = val[7];
  159. data->SR = val[8];
  160. data->PDET = val[9];
  161. data->PD = val[10];
  162. return 1;
  163. }