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