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