ADXL345 Library as part of the 9DOF stick from Sparkfun.com
Dependents: 9DOF-Stick 10dof aigamozu_program_ver2 aigamozu_program_ver2_yokokawa ... more
ADXL345.h
00001 /** 00002 * @author Uwe Gartmann 00003 * @author Used ITG3200 library developed Peter Swanson as template 00004 * A special thanks to Ewout van Bekkum for all his patient help in developing this library! 00005 * 00006 * @section LICENSE 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, I2C interface, accelerometer. 00029 * 00030 * Datasheet: 00031 * 00032 * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf 00033 */ 00034 00035 00036 00037 #ifndef ADXL345_H 00038 #define ADXL345_H 00039 00040 /** 00041 * Includes 00042 */ 00043 #include "mbed.h" 00044 00045 /** 00046 * Defines 00047 */ 00048 //Registers. 00049 #define ADXL345_DEVID_REG 0x00 00050 #define ADXL345_THRESH_TAP_REG 0x1D 00051 #define ADXL345_OFSX_REG 0x1E 00052 #define ADXL345_OFSY_REG 0x1F 00053 #define ADXL345_OFSZ_REG 0x20 00054 #define ADXL345_DUR_REG 0x21 00055 #define ADXL345_LATENT_REG 0x22 00056 #define ADXL345_WINDOW_REG 0x23 00057 #define ADXL345_THRESH_ACT_REG 0x24 00058 #define ADXL345_THRESH_INACT_REG 0x25 00059 #define ADXL345_TIME_INACT_REG 0x26 00060 #define ADXL345_ACT_INACT_CTL_REG 0x27 00061 #define ADXL345_THRESH_FF_REG 0x28 00062 #define ADXL345_TIME_FF_REG 0x29 00063 #define ADXL345_TAP_AXES_REG 0x2A 00064 #define ADXL345_ACT_TAP_STATUS_REG 0x2B 00065 #define ADXL345_BW_RATE_REG 0x2C 00066 #define ADXL345_POWER_CTL_REG 0x2D 00067 #define ADXL345_INT_ENABLE_REG 0x2E 00068 #define ADXL345_INT_MAP_REG 0x2F 00069 #define ADXL345_INT_SOURCE_REG 0x30 00070 #define ADXL345_DATA_FORMAT_REG 0x31 00071 #define ADXL345_DATAX0_REG 0x32 00072 #define ADXL345_DATAX1_REG 0x33 00073 #define ADXL345_DATAY0_REG 0x34 00074 #define ADXL345_DATAY1_REG 0x35 00075 #define ADXL345_DATAZ0_REG 0x36 00076 #define ADXL345_DATAZ1_REG 0x37 00077 #define ADXL345_FIFO_CTL 0x38 00078 #define ADXL345_FIFO_STATUS 0x39 00079 00080 //Data rate codes. 00081 #define ADXL345_3200HZ 0x0F 00082 #define ADXL345_1600HZ 0x0E 00083 #define ADXL345_800HZ 0x0D 00084 #define ADXL345_400HZ 0x0C 00085 #define ADXL345_200HZ 0x0B 00086 #define ADXL345_100HZ 0x0A 00087 #define ADXL345_50HZ 0x09 00088 #define ADXL345_25HZ 0x08 00089 #define ADXL345_12HZ5 0x07 00090 #define ADXL345_6HZ25 0x06 00091 00092 // read or write bytes 00093 #define ADXL345_READ 0xA7 00094 #define ADXL345_WRITE 0xA6 00095 #define ADXL345_ADDRESS 0x53 //the ADXL345 7-bit address is 0x53 when ALT ADDRESS is low as it is on the sparkfun chip: when ALT ADDRESS is high the address is 0x1D 00096 00097 /////////////when ALT ADDRESS pin is high: 00098 //#define ADXL345_READ 0x3B 00099 //#define ADXL345_WRITE 0x3A 00100 //#define ADXL345_ADDRESS 0x1D 00101 00102 #define ADXL345_X 0x00 00103 #define ADXL345_Y 0x01 00104 #define ADXL345_Z 0x02 00105 00106 00107 00108 // modes 00109 #define MeasurementMode 0x08 00110 00111 00112 00113 00114 00115 00116 00117 class ADXL345 { 00118 00119 public: 00120 00121 /** 00122 * Constructor. 00123 * 00124 * @param mosi mbed pin to use for SDA line of I2C interface. 00125 * @param sck mbed pin to use for SCL line of I2C interface. 00126 */ 00127 ADXL345(PinName sda, PinName scl); 00128 00129 /** 00130 * Get the output of all three axes. 00131 * 00132 * @param Pointer to a buffer to hold the accelerometer value for the 00133 * x-axis, y-axis and z-axis [in that order]. 00134 */ 00135 void getOutput(int* readings); 00136 00137 /** 00138 * Read the device ID register on the device. 00139 * 00140 * @return The device ID code [0xE5] 00141 */ 00142 char getDeviceID(void); 00143 00144 /** 00145 * Set the power mode. 00146 * 00147 * @param mode 0 -> Normal operation. 00148 * 1 -> Reduced power operation. 00149 */ 00150 int setPowerMode(char mode); 00151 00152 /** 00153 * Set the power control settings. 00154 * 00155 * See datasheet for details. 00156 * 00157 * @param The control byte to write to the POWER_CTL register. 00158 */ 00159 int setPowerControl(char settings); 00160 /** 00161 * Get the power control settings. 00162 * 00163 * See datasheet for details. 00164 * 00165 * @return The contents of the POWER_CTL register. 00166 */ 00167 char getPowerControl(void); 00168 00169 00170 /** 00171 * Get the data format settings. 00172 * 00173 * @return The contents of the DATA_FORMAT register. 00174 */ 00175 00176 char getDataFormatControl(void); 00177 00178 /** 00179 * Set the data format settings. 00180 * 00181 * @param settings The control byte to write to the DATA_FORMAT register. 00182 */ 00183 int setDataFormatControl(char settings); 00184 00185 /** 00186 * Set the data rate. 00187 * 00188 * @param rate The rate code (see #defines or datasheet). 00189 */ 00190 int setDataRate(char rate); 00191 00192 00193 /** 00194 * Get the current offset for a particular axis. 00195 * 00196 * @param axis 0x00 -> X-axis 00197 * 0x01 -> Y-axis 00198 * 0x02 -> Z-axis 00199 * @return The current offset as an 8-bit 2's complement number with scale 00200 * factor 15.6mg/LSB. 00201 */ 00202 00203 char getOffset(char axis); 00204 00205 /** 00206 * Set the offset for a particular axis. 00207 * 00208 * @param axis 0x00 -> X-axis 00209 * 0x01 -> Y-axis 00210 * 0x02 -> Z-axis 00211 * @param offset The offset as an 8-bit 2's complement number with scale 00212 * factor 15.6mg/LSB. 00213 */ 00214 int setOffset(char axis, char offset); 00215 00216 /** 00217 * Get the FIFO control settings. 00218 * 00219 * @return The contents of the FIFO_CTL register. 00220 */ 00221 char getFifoControl(void); 00222 00223 /** 00224 * Set the FIFO control settings. 00225 * 00226 * @param The control byte to write to the FIFO_CTL register. 00227 */ 00228 int setFifoControl(char settings); 00229 00230 /** 00231 * Get FIFO status. 00232 * 00233 * @return The contents of the FIFO_STATUS register. 00234 */ 00235 char getFifoStatus(void); 00236 00237 /** 00238 * Read the tap threshold on the device. 00239 * 00240 * @return The tap threshold as an 8-bit number with a scale factor of 00241 * 62.5mg/LSB. 00242 */ 00243 char getTapThreshold(void); 00244 00245 /** 00246 * Set the tap threshold. 00247 * 00248 * @param The tap threshold as an 8-bit number with a scale factor of 00249 * 62.5mg/LSB. 00250 */ 00251 int setTapThreshold(char threshold); 00252 00253 /** 00254 * Get the tap duration required to trigger an event. 00255 * 00256 * @return The max time that an event must be above the tap threshold to 00257 * qualify as a tap event, in microseconds. 00258 */ 00259 float getTapDuration(void); 00260 00261 /** 00262 * Set the tap duration required to trigger an event. 00263 * 00264 * @param duration_us The max time that an event must be above the tap 00265 * threshold to qualify as a tap event, in microseconds. 00266 * Time will be normalized by the scale factor which is 00267 * 625us/LSB. A value of 0 disables the single/double 00268 * tap functions. 00269 */ 00270 int setTapDuration(short int duration_us); 00271 00272 /** 00273 * Get the tap latency between the detection of a tap and the time window. 00274 * 00275 * @return The wait time from the detection of a tap event to the start of 00276 * the time window during which a possible second tap event can be 00277 * detected in milliseconds. 00278 */ 00279 float getTapLatency(void); 00280 00281 /** 00282 * Set the tap latency between the detection of a tap and the time window. 00283 * 00284 * @param latency_ms The wait time from the detection of a tap event to the 00285 * start of the time window during which a possible 00286 * second tap event can be detected in milliseconds. 00287 * A value of 0 disables the double tap function. 00288 */ 00289 int setTapLatency(short int latency_ms); 00290 00291 /** 00292 * Get the time of window between tap latency and a double tap. 00293 * 00294 * @return The amount of time after the expiration of the latency time 00295 * during which a second valid tap can begin, in milliseconds. 00296 */ 00297 float getWindowTime(void); 00298 00299 /** 00300 * Set the time of the window between tap latency and a double tap. 00301 * 00302 * @param window_ms The amount of time after the expiration of the latency 00303 * time during which a second valid tap can begin, 00304 * in milliseconds. 00305 */ 00306 int setWindowTime(short int window_ms); 00307 00308 /** 00309 * Get the threshold value for detecting activity. 00310 * 00311 * @return The threshold value for detecting activity as an 8-bit number. 00312 * Scale factor is 62.5mg/LSB. 00313 */ 00314 char getActivityThreshold(void); 00315 00316 /** 00317 * Set the threshold value for detecting activity. 00318 * 00319 * @param threshold The threshold value for detecting activity as an 8-bit 00320 * number. Scale factor is 62.5mg/LSB. A value of 0 may 00321 * result in undesirable behavior if the activity 00322 * interrupt is enabled. 00323 */ 00324 int setActivityThreshold(char threshold); 00325 00326 /** 00327 * Get the threshold value for detecting inactivity. 00328 * 00329 * @return The threshold value for detecting inactivity as an 8-bit number. 00330 * Scale factor is 62.5mg/LSB. 00331 */ 00332 char getInactivityThreshold(void); 00333 00334 /** 00335 * Set the threshold value for detecting inactivity. 00336 * 00337 * @param threshold The threshold value for detecting inactivity as an 00338 * 8-bit number. Scale factor is 62.5mg/LSB. 00339 */ 00340 int setInactivityThreshold(char threshold); 00341 00342 /** 00343 * Get the time required for inactivity to be declared. 00344 * 00345 * @return The amount of time that acceleration must be less than the 00346 * inactivity threshold for inactivity to be declared, in 00347 * seconds. 00348 */ 00349 char getTimeInactivity(void); 00350 00351 /** 00352 * Set the time required for inactivity to be declared. 00353 * 00354 * @param inactivity The amount of time that acceleration must be less than 00355 * the inactivity threshold for inactivity to be 00356 * declared, in seconds. A value of 0 results in an 00357 * interrupt when the output data is less than the 00358 * threshold inactivity. 00359 */ 00360 int setTimeInactivity(char timeInactivity); 00361 00362 /** 00363 * Get the activity/inactivity control settings. 00364 * 00365 * D7 D6 D5 D4 00366 * +-----------+--------------+--------------+--------------+ 00367 * | ACT ac/dc | ACT_X enable | ACT_Y enable | ACT_Z enable | 00368 * +-----------+--------------+--------------+--------------+ 00369 * 00370 * D3 D2 D1 D0 00371 * +-------------+----------------+----------------+----------------+ 00372 * | INACT ac/dc | INACT_X enable | INACT_Y enable | INACT_Z enable | 00373 * +-------------+----------------+----------------+----------------+ 00374 * 00375 * See datasheet for details. 00376 * 00377 * @return The contents of the ACT_INACT_CTL register. 00378 */ 00379 char getActivityInactivityControl(void); 00380 00381 /** 00382 * Set the activity/inactivity control settings. 00383 * 00384 * D7 D6 D5 D4 00385 * +-----------+--------------+--------------+--------------+ 00386 * | ACT ac/dc | ACT_X enable | ACT_Y enable | ACT_Z enable | 00387 * +-----------+--------------+--------------+--------------+ 00388 * 00389 * D3 D2 D1 D0 00390 * +-------------+----------------+----------------+----------------+ 00391 * | INACT ac/dc | INACT_X enable | INACT_Y enable | INACT_Z enable | 00392 * +-------------+----------------+----------------+----------------+ 00393 * 00394 * See datasheet for details. 00395 * 00396 * @param settings The control byte to write to the ACT_INACT_CTL register. 00397 */ 00398 int setActivityInactivityControl(char settings); 00399 00400 /** 00401 * Get the threshold for free fall detection. 00402 * 00403 * @return The threshold value for free-fall detection, as an 8-bit number, 00404 * with scale factor 62.5mg/LSB. 00405 */ 00406 char getFreefallThreshold(void); 00407 00408 /** 00409 * Set the threshold for free fall detection. 00410 * 00411 * @return The threshold value for free-fall detection, as an 8-bit number, 00412 * with scale factor 62.5mg/LSB. A value of 0 may result in 00413 * undesirable behavior if the free-fall interrupt is enabled. 00414 * Values between 300 mg and 600 mg (0x05 to 0x09) are recommended. 00415 */ 00416 int setFreefallThreshold(char threshold); 00417 00418 /** 00419 * Get the time required to generate a free fall interrupt. 00420 * 00421 * @return The minimum time that the value of all axes must be less than 00422 * the freefall threshold to generate a free-fall interrupt, in 00423 * milliseconds. 00424 */ 00425 char getFreefallTime(void); 00426 00427 /** 00428 * Set the time required to generate a free fall interrupt. 00429 * 00430 * @return The minimum time that the value of all axes must be less than 00431 * the freefall threshold to generate a free-fall interrupt, in 00432 * milliseconds. A value of 0 may result in undesirable behavior 00433 * if the free-fall interrupt is enabled. Values between 100 ms 00434 * and 350 ms (0x14 to 0x46) are recommended. 00435 */ 00436 int setFreefallTime(short int freefallTime_ms); 00437 00438 /** 00439 * Get the axis tap settings. 00440 * 00441 * D3 D2 D1 D0 00442 * +----------+--------------+--------------+--------------+ 00443 * | Suppress | TAP_X enable | TAP_Y enable | TAP_Z enable | 00444 * +----------+--------------+--------------+--------------+ 00445 * 00446 * (D7-D4 are 0s). 00447 * 00448 * See datasheet for more details. 00449 * 00450 * @return The contents of the TAP_AXES register. 00451 */ 00452 char getTapAxisControl(void); 00453 00454 /** 00455 * Set the axis tap settings. 00456 * 00457 * D3 D2 D1 D0 00458 * +----------+--------------+--------------+--------------+ 00459 * | Suppress | TAP_X enable | TAP_Y enable | TAP_Z enable | 00460 * +----------+--------------+--------------+--------------+ 00461 * 00462 * (D7-D4 are 0s). 00463 * 00464 * See datasheet for more details. 00465 * 00466 * @param The control byte to write to the TAP_AXES register. 00467 */ 00468 int setTapAxisControl(char settings); 00469 00470 /** 00471 * Get the source of a tap. 00472 * 00473 * @return The contents of the ACT_TAP_STATUS register. 00474 */ 00475 char getTapSource(void); 00476 00477 /** 00478 * Get the interrupt enable settings. 00479 * 00480 * @return The contents of the INT_ENABLE register. 00481 */ 00482 00483 char getInterruptEnableControl(void); 00484 00485 /** 00486 * Set the interrupt enable settings. 00487 * 00488 * @param settings The control byte to write to the INT_ENABLE register. 00489 */ 00490 int setInterruptEnableControl(char settings); 00491 00492 /** 00493 * Get the interrupt mapping settings. 00494 * 00495 * @return The contents of the INT_MAP register. 00496 */ 00497 char getInterruptMappingControl(void); 00498 00499 /** 00500 * Set the interrupt mapping settings. 00501 * 00502 * @param settings The control byte to write to the INT_MAP register. 00503 */ 00504 int setInterruptMappingControl(char settings); 00505 00506 /** 00507 * Get the interrupt source. 00508 * 00509 * @return The contents of the INT_SOURCE register. 00510 */ 00511 char getInterruptSource(void); 00512 00513 00514 private: 00515 00516 I2C i2c_; 00517 00518 00519 /** 00520 * Read one byte from a register on the device. 00521 * 00522 * @param: - the address to be read from 00523 * 00524 * @return: the value of the data read 00525 */ 00526 char SingleByteRead(char address); 00527 00528 /** 00529 * Write one byte to a register on the device. 00530 * 00531 * @param: 00532 - address of the register to write to. 00533 - the value of the data to store 00534 */ 00535 00536 00537 int SingleByteWrite(char address, char data); 00538 00539 /** 00540 * Read several consecutive bytes on the device and store them in a given location. 00541 * 00542 * @param startAddress: The address of the first register to read from. 00543 * @param ptr_output: a pointer to the location to store the data being read 00544 * @param size: The number of bytes to read. 00545 */ 00546 void multiByteRead(char startAddress, char* ptr_output, int size); 00547 00548 /** 00549 * Write several consecutive bytes on the device. 00550 * 00551 * @param startAddress: The address of the first register to write to. 00552 * @param ptr_data: Pointer to a location which contains the data to write. 00553 * @param size: The number of bytes to write. 00554 */ 00555 int multiByteWrite(char startAddress, char* ptr_data, int size); 00556 00557 }; 00558 00559 #endif /* ADXL345_H */
Generated on Sun Jul 17 2022 00:40:20 by
![doxygen](doxygen.png)