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.

419 lines
11 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /**
  2. * @author Peter Swanson
  3. * A personal note from me: Jesus Christ has changed my life so much it blows my mind. I say this because
  4. * today, religion is thought of as something that you do or believe and has about as
  5. * little impact on a person as their political stance. But for me, God gives me daily
  6. * strength and has filled my life with the satisfaction that I could never find in any
  7. * of the other things that I once looked for it in.
  8. * If your interested, heres verse that changed my life:
  9. * Rom 8:1-3: "Therefore, there is now no condemnation for those who are in Christ Jesus,
  10. * because through Christ Jesus, the law of the Spirit who gives life has set
  11. * me free from the law of sin (which brings...) and death. For what the law
  12. * was powerless to do in that it was weakened by the flesh, God did by sending
  13. * His own Son in the likeness of sinful flesh to be a sin offering. And so He
  14. * condemned sin in the flesh in order that the righteous requirements of the
  15. * (God's) law might be fully met in us, who live not according to the flesh
  16. * but according to the Spirit."
  17. *
  18. * A special thanks to Ewout van Bekkum for all his patient help in developing this library!
  19. *
  20. * @section LICENSE
  21. *
  22. * Permission is hereby granted, free of charge, to any person obtaining a copy
  23. * of this software and associated documentation files (the "Software"), to deal
  24. * in the Software without restriction, including without limitation the rights
  25. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  26. * copies of the Software, and to permit persons to whom the Software is
  27. * furnished to do so, subject to the following conditions:
  28. *
  29. * The above copyright notice and this permission notice shall be included in
  30. * all copies or substantial portions of the Software.
  31. *
  32. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  33. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  34. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  35. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  36. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  37. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  38. * THE SOFTWARE.
  39. *
  40. * @section DESCRIPTION
  41. *
  42. * ADXL345, triple axis, I2C interface, accelerometer.
  43. *
  44. * Datasheet:
  45. *
  46. * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
  47. */
  48. /**
  49. * Includes
  50. */
  51. #include "ADXL345_I2C.h"
  52. //#include "mbed.h"
  53. ADXL345_I2C::ADXL345_I2C(PinName sda, PinName scl) : i2c_(sda, scl) {
  54. //400kHz, allowing us to use the fastest data rates.
  55. i2c_.frequency(400000);
  56. // initialize the BW data rate
  57. char tx[2];
  58. tx[0] = ADXL345_BW_RATE_REG;
  59. tx[1] = ADXL345_1600HZ; //value greater than or equal to 0x0A is written into the rate bits (Bit D3 through Bit D0) in the BW_RATE register
  60. i2c_.write( ADXL345_I2C_WRITE , tx, 2);
  61. //Data format (for +-16g) - This is done by setting Bit D3 of the DATA_FORMAT register (Address 0x31) and writing a value of 0x03 to the range bits (Bit D1 and Bit D0) of the DATA_FORMAT register (Address 0x31).
  62. char rx[2];
  63. rx[0] = ADXL345_DATA_FORMAT_REG;
  64. rx[1] = 0x0B;
  65. // full res and +_16g
  66. i2c_.write( ADXL345_I2C_WRITE , rx, 2);
  67. // Set Offset - programmed into the OFSX, OFSY, and OFXZ registers, respectively, as 0xFD, 0x03 and 0xFE.
  68. char x[2];
  69. x[0] = ADXL345_OFSX_REG ;
  70. x[1] = 0xFD;
  71. i2c_.write( ADXL345_I2C_WRITE , x, 2);
  72. char y[2];
  73. y[0] = ADXL345_OFSY_REG ;
  74. y[1] = 0x03;
  75. i2c_.write( ADXL345_I2C_WRITE , y, 2);
  76. char z[2];
  77. z[0] = ADXL345_OFSZ_REG ;
  78. z[1] = 0xFE;
  79. i2c_.write( ADXL345_I2C_WRITE , z, 2);
  80. }
  81. char ADXL345_I2C::SingleByteRead(char address){
  82. char tx = address;
  83. char output;
  84. i2c_.write( ADXL345_I2C_WRITE , &tx, 1); //tell it what you want to read
  85. i2c_.read( ADXL345_I2C_READ , &output, 1); //tell it where to store the data
  86. return output;
  87. }
  88. /*
  89. ***info on the i2c_.write***
  90. address 8-bit I2C slave address [ addr | 0 ]
  91. data Pointer to the byte-array data to send
  92. length Number of bytes to send
  93. repeated Repeated start, true - do not send stop at end
  94. returns 0 on success (ack), or non-0 on failure (nack)
  95. */
  96. int ADXL345_I2C::SingleByteWrite(char address, char data){
  97. int ack = 0;
  98. char tx[2];
  99. tx[0] = address;
  100. tx[1] = data;
  101. return ack | i2c_.write( ADXL345_I2C_WRITE , tx, 2);
  102. }
  103. void ADXL345_I2C::multiByteRead(char address, char* output, int size) {
  104. i2c_.write( ADXL345_I2C_WRITE, &address, 1); //tell it where to read from
  105. i2c_.read( ADXL345_I2C_READ , output, size); //tell it where to store the data read
  106. }
  107. int ADXL345_I2C::multiByteWrite(char address, char* ptr_data, int size) {
  108. int ack;
  109. ack = i2c_.write( ADXL345_I2C_WRITE, &address, 1); //tell it where to write to
  110. return ack | i2c_.write( ADXL345_I2C_READ, ptr_data, size); //tell it what data to write
  111. }
  112. void ADXL345_I2C::getOutput(int* readings){
  113. char buffer[6];
  114. multiByteRead(ADXL345_DATAX0_REG, buffer, 6);
  115. readings[0] = (int)buffer[1] << 8 | (int)buffer[0];
  116. readings[1] = (int)buffer[3] << 8 | (int)buffer[2];
  117. readings[2] = (int)buffer[5] << 8 | (int)buffer[4];
  118. }
  119. char ADXL345_I2C::getDeviceID() {
  120. return SingleByteRead(ADXL345_DEVID_REG);
  121. }
  122. //
  123. int ADXL345_I2C::setPowerMode(char mode) {
  124. //Get the current register contents, so we don't clobber the rate value.
  125. char registerContents = (mode << 4) | SingleByteRead(ADXL345_BW_RATE_REG);
  126. return SingleByteWrite(ADXL345_BW_RATE_REG, registerContents);
  127. }
  128. char ADXL345_I2C::getPowerControl() {
  129. return SingleByteRead(ADXL345_POWER_CTL_REG);
  130. }
  131. int ADXL345_I2C::setPowerControl(char settings) {
  132. return SingleByteWrite(ADXL345_POWER_CTL_REG, settings);
  133. }
  134. char ADXL345_I2C::getDataFormatControl(void){
  135. return SingleByteRead(ADXL345_DATA_FORMAT_REG);
  136. }
  137. int ADXL345_I2C::setDataFormatControl(char settings){
  138. return SingleByteWrite(ADXL345_DATA_FORMAT_REG, settings);
  139. }
  140. int ADXL345_I2C::setDataRate(char rate) {
  141. //Get the current register contents, so we don't clobber the power bit.
  142. char registerContents = SingleByteRead(ADXL345_BW_RATE_REG);
  143. registerContents &= 0x10;
  144. registerContents |= rate;
  145. return SingleByteWrite(ADXL345_BW_RATE_REG, registerContents);
  146. }
  147. char ADXL345_I2C::getOffset(char axis) {
  148. char address = 0;
  149. if (axis == ADXL345_X) {
  150. address = ADXL345_OFSX_REG;
  151. } else if (axis == ADXL345_Y) {
  152. address = ADXL345_OFSY_REG;
  153. } else if (axis == ADXL345_Z) {
  154. address = ADXL345_OFSZ_REG;
  155. }
  156. return SingleByteRead(address);
  157. }
  158. int ADXL345_I2C::setOffset(char axis, char offset) {
  159. char address = 0;
  160. if (axis == ADXL345_X) {
  161. address = ADXL345_OFSX_REG;
  162. } else if (axis == ADXL345_Y) {
  163. address = ADXL345_OFSY_REG;
  164. } else if (axis == ADXL345_Z) {
  165. address = ADXL345_OFSZ_REG;
  166. }
  167. return SingleByteWrite(address, offset);
  168. }
  169. char ADXL345_I2C::getFifoControl(void){
  170. return SingleByteRead(ADXL345_FIFO_CTL);
  171. }
  172. int ADXL345_I2C::setFifoControl(char settings){
  173. return SingleByteWrite(ADXL345_FIFO_STATUS, settings);
  174. }
  175. char ADXL345_I2C::getFifoStatus(void){
  176. return SingleByteRead(ADXL345_FIFO_STATUS);
  177. }
  178. char ADXL345_I2C::getTapThreshold(void) {
  179. return SingleByteRead(ADXL345_THRESH_TAP_REG);
  180. }
  181. int ADXL345_I2C::setTapThreshold(char threshold) {
  182. return SingleByteWrite(ADXL345_THRESH_TAP_REG, threshold);
  183. }
  184. float ADXL345_I2C::getTapDuration(void) {
  185. return (float)SingleByteRead(ADXL345_DUR_REG)*625;
  186. }
  187. int ADXL345_I2C::setTapDuration(short int duration_us) {
  188. short int tapDuration = duration_us / 625;
  189. char tapChar[2];
  190. tapChar[0] = (tapDuration & 0x00FF);
  191. tapChar[1] = (tapDuration >> 8) & 0x00FF;
  192. return multiByteWrite(ADXL345_DUR_REG, tapChar, 2);
  193. }
  194. float ADXL345_I2C::getTapLatency(void) {
  195. return (float)SingleByteRead(ADXL345_LATENT_REG)*1.25;
  196. }
  197. int ADXL345_I2C::setTapLatency(short int latency_ms) {
  198. latency_ms = latency_ms / 1.25;
  199. char latChar[2];
  200. latChar[0] = (latency_ms & 0x00FF);
  201. latChar[1] = (latency_ms << 8) & 0xFF00;
  202. return multiByteWrite(ADXL345_LATENT_REG, latChar, 2);
  203. }
  204. float ADXL345_I2C::getWindowTime(void) {
  205. return (float)SingleByteRead(ADXL345_WINDOW_REG)*1.25;
  206. }
  207. int ADXL345_I2C::setWindowTime(short int window_ms) {
  208. window_ms = window_ms / 1.25;
  209. char windowChar[2];
  210. windowChar[0] = (window_ms & 0x00FF);
  211. windowChar[1] = ((window_ms << 8) & 0xFF00);
  212. return multiByteWrite(ADXL345_WINDOW_REG, windowChar, 2);
  213. }
  214. char ADXL345_I2C::getActivityThreshold(void) {
  215. return SingleByteRead(ADXL345_THRESH_ACT_REG);
  216. }
  217. int ADXL345_I2C::setActivityThreshold(char threshold) {
  218. return SingleByteWrite(ADXL345_THRESH_ACT_REG, threshold);
  219. }
  220. char ADXL345_I2C::getInactivityThreshold(void) {
  221. return SingleByteRead(ADXL345_THRESH_INACT_REG);
  222. }
  223. //int FUNCTION(short int * ptr_Output)
  224. //short int FUNCTION ()
  225. int ADXL345_I2C::setInactivityThreshold(char threshold) {
  226. return SingleByteWrite(ADXL345_THRESH_INACT_REG, threshold);
  227. }
  228. char ADXL345_I2C::getTimeInactivity(void) {
  229. return SingleByteRead(ADXL345_TIME_INACT_REG);
  230. }
  231. int ADXL345_I2C::setTimeInactivity(char timeInactivity) {
  232. return SingleByteWrite(ADXL345_TIME_INACT_REG, timeInactivity);
  233. }
  234. char ADXL345_I2C::getActivityInactivityControl(void) {
  235. return SingleByteRead(ADXL345_ACT_INACT_CTL_REG);
  236. }
  237. int ADXL345_I2C::setActivityInactivityControl(char settings) {
  238. return SingleByteWrite(ADXL345_ACT_INACT_CTL_REG, settings);
  239. }
  240. char ADXL345_I2C::getFreefallThreshold(void) {
  241. return SingleByteRead(ADXL345_THRESH_FF_REG);
  242. }
  243. int ADXL345_I2C::setFreefallThreshold(char threshold) {
  244. return SingleByteWrite(ADXL345_THRESH_FF_REG, threshold);
  245. }
  246. char ADXL345_I2C::getFreefallTime(void) {
  247. return SingleByteRead(ADXL345_TIME_FF_REG)*5;
  248. }
  249. int ADXL345_I2C::setFreefallTime(short int freefallTime_ms) {
  250. freefallTime_ms = freefallTime_ms / 5;
  251. char fallChar[2];
  252. fallChar[0] = (freefallTime_ms & 0x00FF);
  253. fallChar[1] = (freefallTime_ms << 8) & 0xFF00;
  254. return multiByteWrite(ADXL345_TIME_FF_REG, fallChar, 2);
  255. }
  256. char ADXL345_I2C::getTapAxisControl(void) {
  257. return SingleByteRead(ADXL345_TAP_AXES_REG);
  258. }
  259. int ADXL345_I2C::setTapAxisControl(char settings) {
  260. return SingleByteWrite(ADXL345_TAP_AXES_REG, settings);
  261. }
  262. char ADXL345_I2C::getTapSource(void) {
  263. return SingleByteRead(ADXL345_ACT_TAP_STATUS_REG);
  264. }
  265. char ADXL345_I2C::getInterruptEnableControl(void) {
  266. return SingleByteRead(ADXL345_INT_ENABLE_REG);
  267. }
  268. int ADXL345_I2C::setInterruptEnableControl(char settings) {
  269. return SingleByteWrite(ADXL345_INT_ENABLE_REG, settings);
  270. }
  271. char ADXL345_I2C::getInterruptMappingControl(void) {
  272. return SingleByteRead(ADXL345_INT_MAP_REG);
  273. }
  274. int ADXL345_I2C::setInterruptMappingControl(char settings) {
  275. return SingleByteWrite(ADXL345_INT_MAP_REG, settings);
  276. }
  277. char ADXL345_I2C::getInterruptSource(void){
  278. return SingleByteRead(ADXL345_INT_SOURCE_REG);
  279. }