ADXL345 Library as part of the 9DOF stick from Sparkfun.com
Dependents: 9DOF-Stick 10dof aigamozu_program_ver2 aigamozu_program_ver2_yokokawa ... more
ADXL345.cpp
00001 /** 00002 * @author Peter Swanson 00003 * A personal note from me: Jesus Christ has changed my life so much it blows my mind. I say this because 00004 * today, religion is thought of as something that you do or believe and has about as 00005 * little impact on a person as their political stance. But for me, God gives me daily 00006 * strength and has filled my life with the satisfaction that I could never find in any 00007 * of the other things that I once looked for it in. 00008 * If your interested, heres verse that changed my life: 00009 * Rom 8:1-3: "Therefore, there is now no condemnation for those who are in Christ Jesus, 00010 * because through Christ Jesus, the law of the Spirit who gives life has set 00011 * me free from the law of sin (which brings...) and death. For what the law 00012 * was powerless to do in that it was weakened by the flesh, God did by sending 00013 * His own Son in the likeness of sinful flesh to be a sin offering. And so He 00014 * condemned sin in the flesh in order that the righteous requirements of the 00015 * (God's) law might be fully met in us, who live not according to the flesh 00016 * but according to the Spirit." 00017 * 00018 * A special thanks to Ewout van Bekkum for all his patient help in developing this library! 00019 * 00020 * @section LICENSE 00021 * 00022 * Permission is hereby granted, free of charge, to any person obtaining a copy 00023 * of this software and associated documentation files (the "Software"), to deal 00024 * in the Software without restriction, including without limitation the rights 00025 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00026 * copies of the Software, and to permit persons to whom the Software is 00027 * furnished to do so, subject to the following conditions: 00028 * 00029 * The above copyright notice and this permission notice shall be included in 00030 * all copies or substantial portions of the Software. 00031 * 00032 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00033 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00034 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00035 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00036 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00037 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00038 * THE SOFTWARE. 00039 * 00040 * @section DESCRIPTION 00041 * 00042 * ADXL345, triple axis, I2C interface, accelerometer. 00043 * 00044 * Datasheet: 00045 * 00046 * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf 00047 */ 00048 00049 /** 00050 * Includes 00051 */ 00052 #include "ADXL345.h" 00053 00054 //#include "mbed.h" 00055 00056 ADXL345::ADXL345(PinName sda, PinName scl) : i2c_(sda, scl) { 00057 00058 //400kHz, allowing us to use the fastest data rates. 00059 //there are other chips on board, sorry 00060 i2c_.frequency(100000); 00061 // initialize the BW data rate 00062 char tx[2]; 00063 tx[0] = ADXL345_BW_RATE_REG; 00064 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 00065 i2c_.write( ADXL345_WRITE , tx, 2); 00066 00067 //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). 00068 00069 char rx[2]; 00070 rx[0] = ADXL345_DATA_FORMAT_REG; 00071 rx[1] = 0x0B; 00072 // full res and +_16g 00073 i2c_.write( ADXL345_WRITE , rx, 2); 00074 00075 // Set Offset - programmed into the OFSX, OFSY, and OFXZ registers, respectively, as 0xFD, 0x03 and 0xFE. 00076 char x[2]; 00077 x[0] = ADXL345_OFSX_REG ; 00078 x[1] = 0xFD; 00079 i2c_.write( ADXL345_WRITE , x, 2); 00080 char y[2]; 00081 y[0] = ADXL345_OFSY_REG ; 00082 y[1] = 0x03; 00083 i2c_.write( ADXL345_WRITE , y, 2); 00084 char z[2]; 00085 z[0] = ADXL345_OFSZ_REG ; 00086 z[1] = 0xFE; 00087 i2c_.write( ADXL345_WRITE , z, 2); 00088 } 00089 00090 00091 char ADXL345::SingleByteRead(char address){ 00092 char tx = address; 00093 char output; 00094 i2c_.write( ADXL345_WRITE , &tx, 1); //tell it what you want to read 00095 i2c_.read( ADXL345_READ , &output, 1); //tell it where to store the data 00096 return output; 00097 00098 } 00099 00100 00101 /* 00102 ***info on the i2c_.write*** 00103 address 8-bit I2C slave address [ addr | 0 ] 00104 data Pointer to the byte-array data to send 00105 length Number of bytes to send 00106 repeated Repeated start, true - do not send stop at end 00107 returns 0 on success (ack), or non-0 on failure (nack) 00108 */ 00109 00110 int ADXL345::SingleByteWrite(char address, char data){ 00111 int ack = 0; 00112 char tx[2]; 00113 tx[0] = address; 00114 tx[1] = data; 00115 return ack | i2c_.write( ADXL345_WRITE , tx, 2); 00116 } 00117 00118 00119 00120 void ADXL345::multiByteRead(char address, char* output, int size) { 00121 i2c_.write( ADXL345_WRITE, &address, 1); //tell it where to read from 00122 i2c_.read( ADXL345_READ , output, size); //tell it where to store the data read 00123 } 00124 00125 00126 int ADXL345::multiByteWrite(char address, char* ptr_data, int size) { 00127 int ack; 00128 00129 ack = i2c_.write( ADXL345_WRITE, &address, 1); //tell it where to write to 00130 return ack | i2c_.write( ADXL345_READ, ptr_data, size); //tell it what data to write 00131 00132 } 00133 00134 00135 void ADXL345::getOutput(int* readings){ 00136 char buffer[6]; 00137 multiByteRead(ADXL345_DATAX0_REG, buffer, 6); 00138 00139 readings[0] = (int)buffer[1] << 8 | (int)buffer[0]; 00140 readings[1] = (int)buffer[3] << 8 | (int)buffer[2]; 00141 readings[2] = (int)buffer[5] << 8 | (int)buffer[4]; 00142 00143 } 00144 00145 00146 00147 char ADXL345::getDeviceID() { 00148 return SingleByteRead(ADXL345_DEVID_REG); 00149 } 00150 // 00151 int ADXL345::setPowerMode(char mode) { 00152 00153 //Get the current register contents, so we don't clobber the rate value. 00154 char registerContents = (mode << 4) | SingleByteRead(ADXL345_BW_RATE_REG); 00155 00156 return SingleByteWrite(ADXL345_BW_RATE_REG, registerContents); 00157 00158 } 00159 00160 char ADXL345::getPowerControl() { 00161 return SingleByteRead(ADXL345_POWER_CTL_REG); 00162 } 00163 00164 int ADXL345::setPowerControl(char settings) { 00165 return SingleByteWrite(ADXL345_POWER_CTL_REG, settings); 00166 00167 } 00168 00169 00170 00171 char ADXL345::getDataFormatControl(void){ 00172 00173 return SingleByteRead(ADXL345_DATA_FORMAT_REG); 00174 } 00175 00176 int ADXL345::setDataFormatControl(char settings){ 00177 00178 return SingleByteWrite(ADXL345_DATA_FORMAT_REG, settings); 00179 00180 } 00181 00182 int ADXL345::setDataRate(char rate) { 00183 00184 //Get the current register contents, so we don't clobber the power bit. 00185 char registerContents = SingleByteRead(ADXL345_BW_RATE_REG); 00186 00187 registerContents &= 0x10; 00188 registerContents |= rate; 00189 00190 return SingleByteWrite(ADXL345_BW_RATE_REG, registerContents); 00191 00192 } 00193 00194 00195 char ADXL345::getOffset(char axis) { 00196 00197 char address = 0; 00198 00199 if (axis == ADXL345_X) { 00200 address = ADXL345_OFSX_REG; 00201 } else if (axis == ADXL345_Y) { 00202 address = ADXL345_OFSY_REG; 00203 } else if (axis == ADXL345_Z) { 00204 address = ADXL345_OFSZ_REG; 00205 } 00206 00207 return SingleByteRead(address); 00208 } 00209 00210 int ADXL345::setOffset(char axis, char offset) { 00211 00212 char address = 0; 00213 00214 if (axis == ADXL345_X) { 00215 address = ADXL345_OFSX_REG; 00216 } else if (axis == ADXL345_Y) { 00217 address = ADXL345_OFSY_REG; 00218 } else if (axis == ADXL345_Z) { 00219 address = ADXL345_OFSZ_REG; 00220 } 00221 00222 return SingleByteWrite(address, offset); 00223 00224 } 00225 00226 00227 char ADXL345::getFifoControl(void){ 00228 00229 return SingleByteRead(ADXL345_FIFO_CTL); 00230 00231 } 00232 00233 int ADXL345::setFifoControl(char settings){ 00234 return SingleByteWrite(ADXL345_FIFO_STATUS, settings); 00235 00236 } 00237 00238 char ADXL345::getFifoStatus(void){ 00239 00240 return SingleByteRead(ADXL345_FIFO_STATUS); 00241 00242 } 00243 00244 00245 00246 char ADXL345::getTapThreshold(void) { 00247 00248 return SingleByteRead(ADXL345_THRESH_TAP_REG); 00249 } 00250 00251 int ADXL345::setTapThreshold(char threshold) { 00252 00253 return SingleByteWrite(ADXL345_THRESH_TAP_REG, threshold); 00254 00255 } 00256 00257 00258 float ADXL345::getTapDuration(void) { 00259 00260 return (float)SingleByteRead(ADXL345_DUR_REG)*625; 00261 } 00262 00263 int ADXL345::setTapDuration(short int duration_us) { 00264 00265 short int tapDuration = duration_us / 625; 00266 char tapChar[2]; 00267 tapChar[0] = (tapDuration & 0x00FF); 00268 tapChar[1] = (tapDuration >> 8) & 0x00FF; 00269 return multiByteWrite(ADXL345_DUR_REG, tapChar, 2); 00270 00271 } 00272 00273 float ADXL345::getTapLatency(void) { 00274 00275 return (float)SingleByteRead(ADXL345_LATENT_REG)*1.25; 00276 } 00277 00278 int ADXL345::setTapLatency(short int latency_ms) { 00279 00280 latency_ms = latency_ms / 1.25; 00281 char latChar[2]; 00282 latChar[0] = (latency_ms & 0x00FF); 00283 latChar[1] = (latency_ms << 8) & 0xFF00; 00284 return multiByteWrite(ADXL345_LATENT_REG, latChar, 2); 00285 00286 } 00287 00288 float ADXL345::getWindowTime(void) { 00289 00290 return (float)SingleByteRead(ADXL345_WINDOW_REG)*1.25; 00291 } 00292 00293 int ADXL345::setWindowTime(short int window_ms) { 00294 00295 window_ms = window_ms / 1.25; 00296 char windowChar[2]; 00297 windowChar[0] = (window_ms & 0x00FF); 00298 windowChar[1] = ((window_ms << 8) & 0xFF00); 00299 return multiByteWrite(ADXL345_WINDOW_REG, windowChar, 2); 00300 00301 } 00302 00303 char ADXL345::getActivityThreshold(void) { 00304 00305 return SingleByteRead(ADXL345_THRESH_ACT_REG); 00306 } 00307 00308 int ADXL345::setActivityThreshold(char threshold) { 00309 return SingleByteWrite(ADXL345_THRESH_ACT_REG, threshold); 00310 00311 } 00312 00313 char ADXL345::getInactivityThreshold(void) { 00314 return SingleByteRead(ADXL345_THRESH_INACT_REG); 00315 00316 } 00317 00318 //int FUNCTION(short int * ptr_Output) 00319 //short int FUNCTION () 00320 00321 int ADXL345::setInactivityThreshold(char threshold) { 00322 return SingleByteWrite(ADXL345_THRESH_INACT_REG, threshold); 00323 00324 } 00325 00326 char ADXL345::getTimeInactivity(void) { 00327 00328 return SingleByteRead(ADXL345_TIME_INACT_REG); 00329 00330 } 00331 00332 int ADXL345::setTimeInactivity(char timeInactivity) { 00333 return SingleByteWrite(ADXL345_TIME_INACT_REG, timeInactivity); 00334 00335 } 00336 00337 char ADXL345::getActivityInactivityControl(void) { 00338 00339 return SingleByteRead(ADXL345_ACT_INACT_CTL_REG); 00340 00341 } 00342 00343 int ADXL345::setActivityInactivityControl(char settings) { 00344 return SingleByteWrite(ADXL345_ACT_INACT_CTL_REG, settings); 00345 00346 } 00347 00348 char ADXL345::getFreefallThreshold(void) { 00349 00350 return SingleByteRead(ADXL345_THRESH_FF_REG); 00351 00352 } 00353 00354 int ADXL345::setFreefallThreshold(char threshold) { 00355 return SingleByteWrite(ADXL345_THRESH_FF_REG, threshold); 00356 00357 } 00358 00359 char ADXL345::getFreefallTime(void) { 00360 00361 return SingleByteRead(ADXL345_TIME_FF_REG)*5; 00362 00363 } 00364 00365 int ADXL345::setFreefallTime(short int freefallTime_ms) { 00366 freefallTime_ms = freefallTime_ms / 5; 00367 char fallChar[2]; 00368 fallChar[0] = (freefallTime_ms & 0x00FF); 00369 fallChar[1] = (freefallTime_ms << 8) & 0xFF00; 00370 00371 return multiByteWrite(ADXL345_TIME_FF_REG, fallChar, 2); 00372 00373 } 00374 00375 char ADXL345::getTapAxisControl(void) { 00376 00377 return SingleByteRead(ADXL345_TAP_AXES_REG); 00378 00379 } 00380 00381 int ADXL345::setTapAxisControl(char settings) { 00382 return SingleByteWrite(ADXL345_TAP_AXES_REG, settings); 00383 00384 } 00385 00386 char ADXL345::getTapSource(void) { 00387 00388 return SingleByteRead(ADXL345_ACT_TAP_STATUS_REG); 00389 00390 } 00391 00392 00393 00394 char ADXL345::getInterruptEnableControl(void) { 00395 00396 return SingleByteRead(ADXL345_INT_ENABLE_REG); 00397 00398 } 00399 00400 int ADXL345::setInterruptEnableControl(char settings) { 00401 return SingleByteWrite(ADXL345_INT_ENABLE_REG, settings); 00402 00403 } 00404 00405 char ADXL345::getInterruptMappingControl(void) { 00406 00407 return SingleByteRead(ADXL345_INT_MAP_REG); 00408 00409 } 00410 00411 int ADXL345::setInterruptMappingControl(char settings) { 00412 return SingleByteWrite(ADXL345_INT_MAP_REG, settings); 00413 00414 } 00415 00416 char ADXL345::getInterruptSource(void){ 00417 00418 return SingleByteRead(ADXL345_INT_SOURCE_REG); 00419 00420 } 00421 00422 00423 00424
Generated on Sun Jul 17 2022 00:40:20 by 1.7.2