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