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.
ADXL345_I2C.cpp
00001 /** 00002 * @author Aaron Berk 00003 * 00004 * @section LICENSE 00005 * 00006 * Copyright (c) 2010 ARM Limited 00007 * 00008 * Permission is hereby granted, free of charge, to any person obtaining a copy 00009 * of this software and associated documentation files (the "Software"), to deal 00010 * in the Software without restriction, including without limitation the rights 00011 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00012 * copies of the Software, and to permit persons to whom the Software is 00013 * furnished to do so, subject to the following conditions: 00014 * 00015 * The above copyright notice and this permission notice shall be included in 00016 * all copies or substantial portions of the Software. 00017 * 00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00023 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00024 * THE SOFTWARE. 00025 * 00026 * @section DESCRIPTION 00027 * 00028 * ADXL345, triple axis, digital interface, accelerometer. 00029 * 00030 * Datasheet: 00031 * 00032 * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf 00033 */ 00034 00035 /** 00036 * Includes 00037 */ 00038 #include "ADXL345_I2C.h" 00039 00040 ADXL345::ADXL345(PinName sda, PinName scl) { 00041 00042 i2c_ = new I2C(sda, scl); 00043 //100KHz, as specified by the datasheet. 00044 i2c_->frequency(100000); 00045 //2MHz, allowing us to use the fastest data rates. 00046 00047 wait_us(500); 00048 00049 } 00050 00051 int ADXL345::getDevId(void) { 00052 00053 return oneByteRead(ADXL345_DEVID_REG); 00054 00055 } 00056 00057 int ADXL345::getTapThreshold(void) { 00058 00059 return oneByteRead(ADXL345_THRESH_TAP_REG); 00060 00061 } 00062 00063 void ADXL345::setTapThreshold(int threshold) { 00064 00065 oneByteWrite(ADXL345_THRESH_TAP_REG, threshold); 00066 00067 } 00068 00069 int ADXL345::getOffset(int axis) { 00070 00071 int address = 0; 00072 00073 if (axis == ADXL345_X) { 00074 address = ADXL345_OFSX_REG; 00075 } else if (axis == ADXL345_Y) { 00076 address = ADXL345_OFSY_REG; 00077 } else if (axis == ADXL345_Z) { 00078 address = ADXL345_OFSZ_REG; 00079 } 00080 00081 return oneByteRead(address); 00082 00083 } 00084 00085 void ADXL345::setOffset(int axis, char offset) { 00086 00087 int address = 0; 00088 00089 if (axis == ADXL345_X) { 00090 address = ADXL345_OFSX_REG; 00091 } else if (axis == ADXL345_Y) { 00092 address = ADXL345_OFSY_REG; 00093 } else if (axis == ADXL345_Z) { 00094 address = ADXL345_OFSZ_REG; 00095 } 00096 00097 return oneByteWrite(address, offset); 00098 00099 } 00100 00101 int ADXL345::getTapDuration(void) { 00102 00103 return oneByteRead(ADXL345_DUR_REG)*625; 00104 00105 } 00106 00107 void ADXL345::setTapDuration(int duration_us) { 00108 00109 int tapDuration = duration_us / 625; 00110 00111 oneByteWrite(ADXL345_DUR_REG, tapDuration); 00112 00113 } 00114 00115 float ADXL345::getTapLatency(void) { 00116 00117 return oneByteRead(ADXL345_LATENT_REG)*1.25; 00118 00119 } 00120 00121 void ADXL345::setTapLatency(int latency_ms) { 00122 00123 int tapLatency = latency_ms / 1.25; 00124 00125 oneByteWrite(ADXL345_LATENT_REG, tapLatency); 00126 00127 } 00128 00129 float ADXL345::getWindowTime(void) { 00130 00131 return oneByteRead(ADXL345_WINDOW_REG)*1.25; 00132 00133 } 00134 00135 void ADXL345::setWindowTime(int window_ms) { 00136 00137 int windowTime = window_ms / 1.25; 00138 00139 oneByteWrite(ADXL345_WINDOW_REG, windowTime); 00140 00141 } 00142 00143 int ADXL345::getActivityThreshold(void) { 00144 00145 return oneByteRead(ADXL345_THRESH_ACT_REG); 00146 00147 } 00148 00149 void ADXL345::setActivityThreshold(int threshold) { 00150 00151 oneByteWrite(ADXL345_THRESH_ACT_REG, threshold); 00152 00153 } 00154 00155 int ADXL345::getInactivityThreshold(void) { 00156 00157 return oneByteRead(ADXL345_THRESH_INACT_REG); 00158 00159 } 00160 00161 void ADXL345::setInactivityThreshold(int threshold) { 00162 00163 return oneByteWrite(ADXL345_THRESH_INACT_REG, threshold); 00164 00165 } 00166 00167 int ADXL345::getTimeInactivity(void) { 00168 00169 return oneByteRead(ADXL345_TIME_INACT_REG); 00170 00171 } 00172 00173 void ADXL345::setTimeInactivity(int timeInactivity) { 00174 00175 oneByteWrite(ADXL345_TIME_INACT_REG, timeInactivity); 00176 00177 } 00178 00179 int ADXL345::getActivityInactivityControl(void) { 00180 00181 return oneByteRead(ADXL345_ACT_INACT_CTL_REG); 00182 00183 } 00184 00185 void ADXL345::setActivityInactivityControl(int settings) { 00186 00187 oneByteWrite(ADXL345_ACT_INACT_CTL_REG, settings); 00188 00189 } 00190 00191 int ADXL345::getFreefallThreshold(void) { 00192 00193 return oneByteRead(ADXL345_THRESH_FF_REG); 00194 00195 } 00196 00197 void ADXL345::setFreefallThreshold(int threshold) { 00198 00199 oneByteWrite(ADXL345_THRESH_FF_REG, threshold); 00200 00201 } 00202 00203 int ADXL345::getFreefallTime(void) { 00204 00205 return oneByteRead(ADXL345_TIME_FF_REG)*5; 00206 00207 } 00208 00209 void ADXL345::setFreefallTime(int freefallTime_ms) { 00210 00211 int freefallTime = freefallTime_ms / 5; 00212 00213 oneByteWrite(ADXL345_TIME_FF_REG, freefallTime); 00214 00215 } 00216 00217 int ADXL345::getTapAxisControl(void) { 00218 00219 return oneByteRead(ADXL345_TAP_AXES_REG); 00220 00221 } 00222 00223 void ADXL345::setTapAxisControl(int settings) { 00224 00225 oneByteWrite(ADXL345_TAP_AXES_REG, settings); 00226 00227 } 00228 00229 int ADXL345::getTapSource(void) { 00230 00231 return oneByteRead(ADXL345_ACT_TAP_STATUS_REG); 00232 00233 } 00234 00235 void ADXL345::setPowerMode(char mode) { 00236 00237 //Get the current register contents, so we don't clobber the rate value. 00238 char registerContents = oneByteRead(ADXL345_BW_RATE_REG); 00239 00240 registerContents = (mode << 4) | registerContents; 00241 00242 oneByteWrite(ADXL345_BW_RATE_REG, registerContents); 00243 00244 } 00245 00246 int ADXL345::getPowerControl(void) { 00247 00248 return oneByteRead(ADXL345_POWER_CTL_REG); 00249 00250 } 00251 00252 void ADXL345::setPowerControl(int settings) { 00253 00254 oneByteWrite(ADXL345_POWER_CTL_REG, settings); 00255 00256 } 00257 00258 int ADXL345::getInterruptEnableControl(void) { 00259 00260 return oneByteRead(ADXL345_INT_ENABLE_REG); 00261 00262 } 00263 00264 void ADXL345::setInterruptEnableControl(int settings) { 00265 00266 oneByteWrite(ADXL345_INT_ENABLE_REG, settings); 00267 00268 } 00269 00270 int ADXL345::getInterruptMappingControl(void) { 00271 00272 return oneByteRead(ADXL345_INT_MAP_REG); 00273 00274 } 00275 00276 void ADXL345::setInterruptMappingControl(int settings) { 00277 00278 oneByteWrite(ADXL345_INT_MAP_REG, settings); 00279 00280 } 00281 00282 int ADXL345::getInterruptSource(void){ 00283 00284 return oneByteRead(ADXL345_INT_SOURCE_REG); 00285 00286 } 00287 00288 int ADXL345::getDataFormatControl(void){ 00289 00290 return oneByteRead(ADXL345_DATA_FORMAT_REG); 00291 00292 } 00293 00294 void ADXL345::setDataFormatControl(int settings){ 00295 00296 oneByteWrite(ADXL345_DATA_FORMAT_REG, settings); 00297 00298 } 00299 00300 void ADXL345::setDataRate(int rate) { 00301 00302 //Get the current register contents, so we don't clobber the power bit. 00303 char registerContents = oneByteRead(ADXL345_BW_RATE_REG); 00304 00305 registerContents &= 0x10; 00306 registerContents |= rate; 00307 00308 oneByteWrite(ADXL345_BW_RATE_REG, registerContents); 00309 00310 } 00311 00312 void ADXL345::getOutput(int* readings){ 00313 00314 char buffer[6]; 00315 00316 multiByteRead(ADXL345_DATAX0_REG, buffer, 6); 00317 00318 readings[0] = (int)buffer[1] << 8 | (int)buffer[0]; 00319 readings[1] = (int)buffer[3] << 8 | (int)buffer[2]; 00320 readings[2] = (int)buffer[5] << 8 | (int)buffer[4]; 00321 00322 } 00323 00324 int ADXL345::getFifoControl(void){ 00325 00326 return oneByteRead(ADXL345_FIFO_CTL); 00327 00328 } 00329 00330 void ADXL345::setFifoControl(int settings){ 00331 00332 oneByteWrite(ADXL345_FIFO_STATUS, settings); 00333 00334 } 00335 00336 int ADXL345::getFifoStatus(void){ 00337 00338 return oneByteRead(ADXL345_FIFO_STATUS); 00339 00340 } 00341 00342 00343 int ADXL345::oneByteRead(int address) { 00344 00345 char rx[1]; 00346 rx[0] = address; 00347 i2c_ -> write((ADXL345_I2C_ADDRESS << 1) & 0xFE, rx, 1); 00348 wait_ms(1); 00349 i2c_ -> read((ADXL345_I2C_ADDRESS << 1) | 0x01, rx, 1); 00350 wait_ms(1); 00351 return rx[0]; 00352 } 00353 00354 void ADXL345::oneByteWrite(int address, char data) { 00355 00356 char tx[2]; 00357 tx[0] = address; 00358 tx[1] = data; 00359 i2c_ -> write((ADXL345_I2C_ADDRESS << 1) & 0xFE, tx, 2); 00360 wait_ms(1); 00361 00362 } 00363 00364 void ADXL345::multiByteRead(int address, char* data, int bytes) { 00365 00366 char cmd[6]; 00367 cmd[0] = address; 00368 i2c_ -> write((ADXL345_I2C_ADDRESS << 1) & 0xFE, cmd, 1); 00369 wait_ms(1); 00370 i2c_ -> read((ADXL345_I2C_ADDRESS << 1) | 0x01, data, bytes ); 00371 wait_ms(1); 00372 00373 } 00374 00375 /*void ADXL345::multiByteWrite(int startAddress, char* buffer, int size) { 00376 00377 int tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F)); 00378 00379 nCS_ = 0; 00380 //Send address to start reading from. 00381 spi_.write(tx); 00382 00383 for (int i = 0; i < size; i++) { 00384 buffer[i] = spi_.write(0x00); 00385 } 00386 00387 nCS_ = 1; 00388 00389 }*/
Generated on Mon Jul 18 2022 14:44:22 by
1.7.2