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