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