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.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.h" 00039 00040 ADXL345::ADXL345(PinName mosi, 00041 PinName miso, 00042 PinName sck, 00043 PinName cs) : spi_(mosi, miso, sck), nCS_(cs) { 00044 00045 //2MHz, allowing us to use the fastest data rates. 00046 spi_.frequency(2000000); 00047 spi_.format(8,3); 00048 00049 nCS_ = 1; 00050 00051 wait_us(500); 00052 00053 } 00054 00055 int ADXL345::getDevId(void) { 00056 00057 return oneByteRead(ADXL345_DEVID_REG); 00058 00059 } 00060 00061 int ADXL345::getTapThreshold(void) { 00062 00063 return oneByteRead(ADXL345_THRESH_TAP_REG); 00064 00065 } 00066 00067 void ADXL345::setTapThreshold(int threshold) { 00068 00069 oneByteWrite(ADXL345_THRESH_TAP_REG, threshold); 00070 00071 } 00072 00073 int ADXL345::getOffset(int axis) { 00074 00075 int address = 0; 00076 00077 if (axis == ADXL345_X) { 00078 address = ADXL345_OFSX_REG; 00079 } else if (axis == ADXL345_Y) { 00080 address = ADXL345_OFSY_REG; 00081 } else if (axis == ADXL345_Z) { 00082 address = ADXL345_OFSZ_REG; 00083 } 00084 00085 return oneByteRead(address); 00086 00087 } 00088 00089 void ADXL345::setOffset(int axis, char offset) { 00090 00091 int address = 0; 00092 00093 if (axis == ADXL345_X) { 00094 address = ADXL345_OFSX_REG; 00095 } else if (axis == ADXL345_Y) { 00096 address = ADXL345_OFSY_REG; 00097 } else if (axis == ADXL345_Z) { 00098 address = ADXL345_OFSZ_REG; 00099 } 00100 00101 return oneByteWrite(address, offset); 00102 00103 } 00104 00105 int ADXL345::getTapDuration(void) { 00106 00107 return oneByteRead(ADXL345_DUR_REG)*625; 00108 00109 } 00110 00111 void ADXL345::setTapDuration(int duration_us) { 00112 00113 int tapDuration = duration_us / 625; 00114 00115 oneByteWrite(ADXL345_DUR_REG, tapDuration); 00116 00117 } 00118 00119 float ADXL345::getTapLatency(void) { 00120 00121 return oneByteRead(ADXL345_LATENT_REG)*1.25; 00122 00123 } 00124 00125 void ADXL345::setTapLatency(int latency_ms) { 00126 00127 int tapLatency = latency_ms / 1.25; 00128 00129 oneByteWrite(ADXL345_LATENT_REG, tapLatency); 00130 00131 } 00132 00133 float ADXL345::getWindowTime(void) { 00134 00135 return oneByteRead(ADXL345_WINDOW_REG)*1.25; 00136 00137 } 00138 00139 void ADXL345::setWindowTime(int window_ms) { 00140 00141 int windowTime = window_ms / 1.25; 00142 00143 oneByteWrite(ADXL345_WINDOW_REG, windowTime); 00144 00145 } 00146 00147 int ADXL345::getActivityThreshold(void) { 00148 00149 return oneByteRead(ADXL345_THRESH_ACT_REG); 00150 00151 } 00152 00153 void ADXL345::setActivityThreshold(int threshold) { 00154 00155 oneByteWrite(ADXL345_THRESH_ACT_REG, threshold); 00156 00157 } 00158 00159 int ADXL345::getInactivityThreshold(void) { 00160 00161 return oneByteRead(ADXL345_THRESH_INACT_REG); 00162 00163 } 00164 00165 void ADXL345::setInactivityThreshold(int threshold) { 00166 00167 return oneByteWrite(ADXL345_THRESH_INACT_REG, threshold); 00168 00169 } 00170 00171 int ADXL345::getTimeInactivity(void) { 00172 00173 return oneByteRead(ADXL345_TIME_INACT_REG); 00174 00175 } 00176 00177 void ADXL345::setTimeInactivity(int timeInactivity) { 00178 00179 oneByteWrite(ADXL345_TIME_INACT_REG, timeInactivity); 00180 00181 } 00182 00183 int ADXL345::getActivityInactivityControl(void) { 00184 00185 return oneByteRead(ADXL345_ACT_INACT_CTL_REG); 00186 00187 } 00188 00189 void ADXL345::setActivityInactivityControl(int settings) { 00190 00191 oneByteWrite(ADXL345_ACT_INACT_CTL_REG, settings); 00192 00193 } 00194 00195 int ADXL345::getFreefallThreshold(void) { 00196 00197 return oneByteRead(ADXL345_THRESH_FF_REG); 00198 00199 } 00200 00201 void ADXL345::setFreefallThreshold(int threshold) { 00202 00203 oneByteWrite(ADXL345_THRESH_FF_REG, threshold); 00204 00205 } 00206 00207 int ADXL345::getFreefallTime(void) { 00208 00209 return oneByteRead(ADXL345_TIME_FF_REG)*5; 00210 00211 } 00212 00213 void ADXL345::setFreefallTime(int freefallTime_ms) { 00214 00215 int freefallTime = freefallTime_ms / 5; 00216 00217 oneByteWrite(ADXL345_TIME_FF_REG, freefallTime); 00218 00219 } 00220 00221 int ADXL345::getTapAxisControl(void) { 00222 00223 return oneByteRead(ADXL345_TAP_AXES_REG); 00224 00225 } 00226 00227 void ADXL345::setTapAxisControl(int settings) { 00228 00229 oneByteWrite(ADXL345_TAP_AXES_REG, settings); 00230 00231 } 00232 00233 int ADXL345::getTapSource(void) { 00234 00235 return oneByteRead(ADXL345_ACT_TAP_STATUS_REG); 00236 00237 } 00238 00239 void ADXL345::setPowerMode(char mode) { 00240 00241 //Get the current register contents, so we don't clobber the rate value. 00242 char registerContents = oneByteRead(ADXL345_BW_RATE_REG); 00243 00244 registerContents = (mode << 4) | registerContents; 00245 00246 oneByteWrite(ADXL345_BW_RATE_REG, registerContents); 00247 00248 } 00249 00250 int ADXL345::getPowerControl(void) { 00251 00252 return oneByteRead(ADXL345_POWER_CTL_REG); 00253 00254 } 00255 00256 void ADXL345::setPowerControl(int settings) { 00257 00258 oneByteWrite(ADXL345_POWER_CTL_REG, settings); 00259 00260 } 00261 00262 int ADXL345::getInterruptEnableControl(void) { 00263 00264 return oneByteRead(ADXL345_INT_ENABLE_REG); 00265 00266 } 00267 00268 void ADXL345::setInterruptEnableControl(int settings) { 00269 00270 oneByteWrite(ADXL345_INT_ENABLE_REG, settings); 00271 00272 } 00273 00274 int ADXL345::getInterruptMappingControl(void) { 00275 00276 return oneByteRead(ADXL345_INT_MAP_REG); 00277 00278 } 00279 00280 void ADXL345::setInterruptMappingControl(int settings) { 00281 00282 oneByteWrite(ADXL345_INT_MAP_REG, settings); 00283 00284 } 00285 00286 int ADXL345::getInterruptSource(void){ 00287 00288 return oneByteRead(ADXL345_INT_SOURCE_REG); 00289 00290 } 00291 00292 int ADXL345::getDataFormatControl(void){ 00293 00294 return oneByteRead(ADXL345_DATA_FORMAT_REG); 00295 00296 } 00297 00298 void ADXL345::setDataFormatControl(int settings){ 00299 00300 oneByteWrite(ADXL345_DATA_FORMAT_REG, settings); 00301 00302 } 00303 00304 void ADXL345::setDataRate(int rate) { 00305 00306 //Get the current register contents, so we don't clobber the power bit. 00307 char registerContents = oneByteRead(ADXL345_BW_RATE_REG); 00308 00309 registerContents &= 0x10; 00310 registerContents |= rate; 00311 00312 oneByteWrite(ADXL345_BW_RATE_REG, registerContents); 00313 00314 } 00315 00316 void ADXL345::getOutput(int* readings){ 00317 00318 char buffer[6]; 00319 00320 multiByteRead(ADXL345_DATAX0_REG, buffer, 6); 00321 00322 readings[0] = (int)buffer[1] << 8 | (int)buffer[0]; 00323 readings[1] = (int)buffer[3] << 8 | (int)buffer[2]; 00324 readings[2] = (int)buffer[5] << 8 | (int)buffer[4]; 00325 00326 } 00327 00328 int ADXL345::getFifoControl(void){ 00329 00330 return oneByteRead(ADXL345_FIFO_CTL); 00331 00332 } 00333 00334 void ADXL345::setFifoControl(int settings){ 00335 00336 oneByteWrite(ADXL345_FIFO_STATUS, settings); 00337 00338 } 00339 00340 int ADXL345::getFifoStatus(void){ 00341 00342 return oneByteRead(ADXL345_FIFO_STATUS); 00343 00344 } 00345 00346 int ADXL345::oneByteRead(int address) { 00347 00348 int tx = (ADXL345_SPI_READ | (address & 0x3F)); 00349 int rx = 0; 00350 00351 nCS_ = 0; 00352 //Send address to read from. 00353 spi_.write(tx); 00354 //Read back contents of address. 00355 rx = spi_.write(0x00); 00356 nCS_ = 1; 00357 00358 return rx; 00359 00360 } 00361 00362 void ADXL345::oneByteWrite(int address, char data) { 00363 00364 int tx = (ADXL345_SPI_WRITE | (address & 0x3F)); 00365 00366 nCS_ = 0; 00367 //Send address to write to. 00368 spi_.write(tx); 00369 //Send data to be written. 00370 spi_.write(data); 00371 nCS_ = 1; 00372 00373 } 00374 00375 void ADXL345::multiByteRead(int startAddress, char* buffer, int size) { 00376 00377 int tx = (ADXL345_SPI_READ | 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 } 00390 00391 void ADXL345::multiByteWrite(int startAddress, char* buffer, int size) { 00392 00393 int tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F)); 00394 00395 nCS_ = 0; 00396 //Send address to start reading from. 00397 spi_.write(tx); 00398 00399 for (int i = 0; i < size; i++) { 00400 buffer[i] = spi_.write(0x00); 00401 } 00402 00403 nCS_ = 1; 00404 00405 }
Generated on Tue Jul 12 2022 12:21:34 by
