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