I2Cdevlib

I2C device library collection for AVR/Arduino or other C++-based MCUs

ADXL345/ADXL345.h
00001 // I2Cdev library collection - ADXL345 I2C device class header file
00002 // Based on Analog Devices ADXL345 datasheet rev. C, 5/2011
00003 // 7/31/2011 by Jeff Rowberg <jeff@rowberg.net>
00004 // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
00005 //
00006 // Changelog:
00007 //     2011-07-31 - initial release
00008 
00009 /* ============================================
00010 I2Cdev device library code is placed under the MIT license
00011 Copyright (c) 2011 Jeff Rowberg
00012 
00013 Permission is hereby granted, free of charge, to any person obtaining a copy
00014 of this software and associated documentation files (the "Software"), to deal
00015 in the Software without restriction, including without limitation the rights
00016 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00017 copies of the Software, and to permit persons to whom the Software is
00018 furnished to do so, subject to the following conditions:
00019 
00020 The above copyright notice and this permission notice shall be included in
00021 all copies or substantial portions of the Software.
00022 
00023 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00024 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00025 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00026 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00027 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00028 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00029 THE SOFTWARE.
00030 ===============================================
00031 */
00032 
00033 #ifndef _ADXL345_H_
00034 #define _ADXL345_H_
00035 
00036 #include "I2Cdev.h"
00037 
00038 #define ADXL345_ADDRESS_ALT_LOW     0x53 // alt address pin low (GND)
00039 #define ADXL345_ADDRESS_ALT_HIGH    0x1D // alt address pin high (VCC)
00040 #define ADXL345_DEFAULT_ADDRESS     ADXL345_ADDRESS_ALT_LOW
00041 
00042 #define ADXL345_RA_DEVID            0x00
00043 #define ADXL345_RA_RESERVED1        0x01
00044 #define ADXL345_RA_THRESH_TAP       0x1D
00045 #define ADXL345_RA_OFSX             0x1E
00046 #define ADXL345_RA_OFSY             0x1F
00047 #define ADXL345_RA_OFSZ             0x20
00048 #define ADXL345_RA_DUR              0x21
00049 #define ADXL345_RA_LATENT           0x22
00050 #define ADXL345_RA_WINDOW           0x23
00051 #define ADXL345_RA_THRESH_ACT       0x24
00052 #define ADXL345_RA_THRESH_INACT     0x25
00053 #define ADXL345_RA_TIME_INACT       0x26
00054 #define ADXL345_RA_ACT_INACT_CTL    0x27
00055 #define ADXL345_RA_THRESH_FF        0x28
00056 #define ADXL345_RA_TIME_FF          0x29
00057 #define ADXL345_RA_TAP_AXES         0x2A
00058 #define ADXL345_RA_ACT_TAP_STATUS   0x2B
00059 #define ADXL345_RA_BW_RATE          0x2C
00060 #define ADXL345_RA_POWER_CTL        0x2D
00061 #define ADXL345_RA_INT_ENABLE       0x2E
00062 #define ADXL345_RA_INT_MAP          0x2F
00063 #define ADXL345_RA_INT_SOURCE       0x30
00064 #define ADXL345_RA_DATA_FORMAT      0x31
00065 #define ADXL345_RA_DATAX0           0x32
00066 #define ADXL345_RA_DATAX1           0x33
00067 #define ADXL345_RA_DATAY0           0x34
00068 #define ADXL345_RA_DATAY1           0x35
00069 #define ADXL345_RA_DATAZ0           0x36
00070 #define ADXL345_RA_DATAZ1           0x37
00071 #define ADXL345_RA_FIFO_CTL         0x38
00072 #define ADXL345_RA_FIFO_STATUS      0x39
00073 
00074 #define ADXL345_AIC_ACT_AC_BIT      7
00075 #define ADXL345_AIC_ACT_X_BIT       6
00076 #define ADXL345_AIC_ACT_Y_BIT       5
00077 #define ADXL345_AIC_ACT_Z_BIT       4
00078 #define ADXL345_AIC_INACT_AC_BIT    3
00079 #define ADXL345_AIC_INACT_X_BIT     2
00080 #define ADXL345_AIC_INACT_Y_BIT     1
00081 #define ADXL345_AIC_INACT_Z_BIT     0
00082 
00083 #define ADXL345_TAPAXIS_SUP_BIT     3
00084 #define ADXL345_TAPAXIS_X_BIT       2
00085 #define ADXL345_TAPAXIS_Y_BIT       1
00086 #define ADXL345_TAPAXIS_Z_BIT       0
00087 
00088 #define ADXL345_TAPSTAT_ACTX_BIT    6
00089 #define ADXL345_TAPSTAT_ACTY_BIT    5
00090 #define ADXL345_TAPSTAT_ACTZ_BIT    4
00091 #define ADXL345_TAPSTAT_ASLEEP_BIT  3
00092 #define ADXL345_TAPSTAT_TAPX_BIT    2
00093 #define ADXL345_TAPSTAT_TAPY_BIT    1
00094 #define ADXL345_TAPSTAT_TAPZ_BIT    0
00095 
00096 #define ADXL345_BW_LOWPOWER_BIT     4
00097 #define ADXL345_BW_RATE_BIT         3
00098 #define ADXL345_BW_RATE_LENGTH      4
00099 
00100 #define ADXL345_RATE_3200           0b1111
00101 #define ADXL345_RATE_1600           0b1110
00102 #define ADXL345_RATE_800            0b1101
00103 #define ADXL345_RATE_400            0b1100
00104 #define ADXL345_RATE_200            0b1011
00105 #define ADXL345_RATE_100            0b1010
00106 #define ADXL345_RATE_50             0b1001
00107 #define ADXL345_RATE_25             0b1000
00108 #define ADXL345_RATE_12P5           0b0111
00109 #define ADXL345_RATE_6P25           0b0110
00110 #define ADXL345_RATE_3P13           0b0101
00111 #define ADXL345_RATE_1P56           0b0100
00112 #define ADXL345_RATE_0P78           0b0011
00113 #define ADXL345_RATE_0P39           0b0010
00114 #define ADXL345_RATE_0P20           0b0001
00115 #define ADXL345_RATE_0P10           0b0000
00116 
00117 #define ADXL345_PCTL_LINK_BIT       5
00118 #define ADXL345_PCTL_AUTOSLEEP_BIT  4
00119 #define ADXL345_PCTL_MEASURE_BIT    3
00120 #define ADXL345_PCTL_SLEEP_BIT      2
00121 #define ADXL345_PCTL_WAKEUP_BIT     1
00122 #define ADXL345_PCTL_WAKEUP_LENGTH  2
00123 
00124 #define ADXL345_WAKEUP_8HZ          0b00
00125 #define ADXL345_WAKEUP_4HZ          0b01
00126 #define ADXL345_WAKEUP_2HZ          0b10
00127 #define ADXL345_WAKEUP_1HZ          0b11
00128 
00129 #define ADXL345_INT_DATA_READY_BIT  7
00130 #define ADXL345_INT_SINGLE_TAP_BIT  6
00131 #define ADXL345_INT_DOUBLE_TAP_BIT  5
00132 #define ADXL345_INT_ACTIVITY_BIT    4
00133 #define ADXL345_INT_INACTIVITY_BIT  3
00134 #define ADXL345_INT_FREE_FALL_BIT   2
00135 #define ADXL345_INT_WATERMARK_BIT   1
00136 #define ADXL345_INT_OVERRUN_BIT     0
00137 
00138 #define ADXL345_FORMAT_SELFTEST_BIT 7
00139 #define ADXL345_FORMAT_SPIMODE_BIT  6
00140 #define ADXL345_FORMAT_INTMODE_BIT  5
00141 #define ADXL345_FORMAT_FULL_RES_BIT 3
00142 #define ADXL345_FORMAT_JUSTIFY_BIT  2
00143 #define ADXL345_FORMAT_RANGE_BIT    1
00144 #define ADXL345_FORMAT_RANGE_LENGTH 2
00145 
00146 #define ADXL345_RANGE_2G            0b00
00147 #define ADXL345_RANGE_4G            0b01
00148 #define ADXL345_RANGE_8G            0b10
00149 #define ADXL345_RANGE_16G           0b11
00150 
00151 #define ADXL345_FIFO_MODE_BIT       7
00152 #define ADXL345_FIFO_MODE_LENGTH    2
00153 #define ADXL345_FIFO_TRIGGER_BIT    5
00154 #define ADXL345_FIFO_SAMPLES_BIT    4
00155 #define ADXL345_FIFO_SAMPLES_LENGTH 5
00156 
00157 #define ADXL345_FIFO_MODE_BYPASS    0b00
00158 #define ADXL345_FIFO_MODE_FIFO      0b01
00159 #define ADXL345_FIFO_MODE_STREAM    0b10
00160 #define ADXL345_FIFO_MODE_TRIGGER   0b11
00161 
00162 #define ADXL345_FIFOSTAT_TRIGGER_BIT        7
00163 #define ADXL345_FIFOSTAT_LENGTH_BIT         5
00164 #define ADXL345_FIFOSTAT_LENGTH_LENGTH      6
00165 
00166 class ADXL345 {
00167     public:
00168         ADXL345();
00169         ADXL345(uint8_t address);
00170 
00171         void initialize();
00172         bool testConnection();
00173 
00174         // DEVID register
00175         uint8_t getDeviceID();
00176         
00177         // THRESH_TAP register
00178         uint8_t getTapThreshold();
00179         void setTapThreshold(uint8_t threshold);
00180 
00181         // OFS* registers
00182         void getOffset(int8_t* x, int8_t* y, int8_t* z);
00183         void setOffset(int8_t x, int8_t y, int8_t z);
00184         int8_t getOffsetX();
00185         void setOffsetX(int8_t x);
00186         int8_t getOffsetY();
00187         void setOffsetY(int8_t y);
00188         int8_t getOffsetZ();
00189         void setOffsetZ(int8_t z);
00190         
00191         // DUR register
00192         uint8_t getTapDuration();
00193         void setTapDuration(uint8_t duration);
00194         
00195         // LATENT register
00196         uint8_t getDoubleTapLatency();
00197         void setDoubleTapLatency(uint8_t latency);
00198         
00199         // WINDOW register
00200         uint8_t getDoubleTapWindow();
00201         void setDoubleTapWindow(uint8_t window);
00202         
00203         // THRESH_ACT register
00204         uint8_t getActivityThreshold();
00205         void setActivityThreshold(uint8_t threshold);
00206         
00207         // THRESH_INACT register
00208         uint8_t getInactivityThreshold();
00209         void setInactivityThreshold(uint8_t threshold);
00210 
00211         // TIME_INACT register
00212         uint8_t getInactivityTime();
00213         void setInactivityTime(uint8_t time);
00214         
00215         // ACT_INACT_CTL register
00216         bool getActivityAC();
00217         void setActivityAC(bool enabled);
00218         bool getActivityXEnabled();
00219         void setActivityXEnabled(bool enabled);
00220         bool getActivityYEnabled();
00221         void setActivityYEnabled(bool enabled);
00222         bool getActivityZEnabled();
00223         void setActivityZEnabled(bool enabled);
00224         bool getInactivityAC();
00225         void setInactivityAC(bool enabled);
00226         bool getInactivityXEnabled();
00227         void setInactivityXEnabled(bool enabled);
00228         bool getInactivityYEnabled();
00229         void setInactivityYEnabled(bool enabled);
00230         bool getInactivityZEnabled();
00231         void setInactivityZEnabled(bool enabled);
00232         
00233         // THRESH_FF register
00234         uint8_t getFreefallThreshold();
00235         void setFreefallThreshold(uint8_t threshold);
00236         
00237         // TIME_FF register
00238         uint8_t getFreefallTime();
00239         void setFreefallTime(uint8_t time);
00240         
00241         // TAP_AXES register
00242         bool getTapAxisSuppress();
00243         void setTapAxisSuppress(bool enabled);
00244         bool getTapAxisXEnabled();
00245         void setTapAxisXEnabled(bool enabled);
00246         bool getTapAxisYEnabled();
00247         void setTapAxisYEnabled(bool enabled);
00248         bool getTapAxisZEnabled();
00249         void setTapAxisZEnabled(bool enabled);
00250         
00251         // ACT_TAP_STATUS register
00252         bool getActivitySourceX();
00253         bool getActivitySourceY();
00254         bool getActivitySourceZ();
00255         bool getAsleep();
00256         bool getTapSourceX();
00257         bool getTapSourceY();
00258         bool getTapSourceZ();
00259         
00260         // BW_RATE register
00261         bool getLowPowerEnabled();
00262         void setLowPowerEnabled(bool enabled);
00263         uint8_t getRate();
00264         void setRate(uint8_t rate);
00265 
00266         // POWER_CTL register
00267         bool getLinkEnabled();
00268         void setLinkEnabled(bool enabled);
00269         bool getAutoSleepEnabled();
00270         void setAutoSleepEnabled(bool enabled);
00271         bool getMeasureEnabled();
00272         void setMeasureEnabled(bool enabled);
00273         bool getSleepEnabled();
00274         void setSleepEnabled(bool enabled);
00275         uint8_t getWakeupFrequency();
00276         void setWakeupFrequency(uint8_t frequency);
00277         
00278         // INT_ENABLE register
00279         bool getIntDataReadyEnabled();
00280         void setIntDataReadyEnabled(bool enabled);
00281         bool getIntSingleTapEnabled();
00282         void setIntSingleTapEnabled(bool enabled);
00283         bool getIntDoubleTapEnabled();
00284         void setIntDoubleTapEnabled(bool enabled);
00285         bool getIntActivityEnabled();
00286         void setIntActivityEnabled(bool enabled);
00287         bool getIntInactivityEnabled();
00288         void setIntInactivityEnabled(bool enabled);
00289         bool getIntFreefallEnabled();
00290         void setIntFreefallEnabled(bool enabled);
00291         bool getIntWatermarkEnabled();
00292         void setIntWatermarkEnabled(bool enabled);
00293         bool getIntOverrunEnabled();
00294         void setIntOverrunEnabled(bool enabled);
00295         
00296         // INT_MAP register
00297         uint8_t getIntDataReadyPin();
00298         void setIntDataReadyPin(uint8_t pin);
00299         uint8_t getIntSingleTapPin();
00300         void setIntSingleTapPin(uint8_t pin);
00301         uint8_t getIntDoubleTapPin();
00302         void setIntDoubleTapPin(uint8_t pin);
00303         uint8_t getIntActivityPin();
00304         void setIntActivityPin(uint8_t pin);
00305         uint8_t getIntInactivityPin();
00306         void setIntInactivityPin(uint8_t pin);
00307         uint8_t getIntFreefallPin();
00308         void setIntFreefallPin(uint8_t pin);
00309         uint8_t getIntWatermarkPin();
00310         void setIntWatermarkPin(uint8_t pin);
00311         uint8_t getIntOverrunPin();
00312         void setIntOverrunPin(uint8_t pin);
00313 
00314         // INT_SOURCE register
00315         uint8_t getIntDataReadySource();
00316         uint8_t getIntSingleTapSource();
00317         uint8_t getIntDoubleTapSource();
00318         uint8_t getIntActivitySource();
00319         uint8_t getIntInactivitySource();
00320         uint8_t getIntFreefallSource();
00321         uint8_t getIntWatermarkSource();
00322         uint8_t getIntOverrunSource();
00323         
00324         // DATA_FORMAT register
00325         uint8_t getSelfTestEnabled();
00326         void setSelfTestEnabled(uint8_t enabled);
00327         uint8_t getSPIMode();
00328         void setSPIMode(uint8_t mode);
00329         uint8_t getInterruptMode();
00330         void setInterruptMode(uint8_t mode);
00331         uint8_t getFullResolution();
00332         void setFullResolution(uint8_t resolution);
00333         uint8_t getDataJustification();
00334         void setDataJustification(uint8_t justification);
00335         uint8_t getRange();
00336         void setRange(uint8_t range);
00337 
00338         // DATA* registers
00339         void getAcceleration(int16_t* x, int16_t* y, int16_t* z);
00340         int16_t getAccelerationX();
00341         int16_t getAccelerationY();
00342         int16_t getAccelerationZ();
00343 
00344         // FIFO_CTL register
00345         uint8_t getFIFOMode();
00346         void setFIFOMode(uint8_t mode);
00347         uint8_t getFIFOTriggerInterruptPin();
00348         void setFIFOTriggerInterruptPin(uint8_t interrupt);
00349         uint8_t getFIFOSamples();
00350         void setFIFOSamples(uint8_t size);
00351         
00352         // FIFO_STATUS register
00353         bool getFIFOTriggerOccurred();
00354         uint8_t getFIFOLength();
00355 
00356     private:
00357         uint8_t devAddr;
00358         uint8_t buffer[6];
00359 };
00360 
00361 #endif /* _ADXL345_H_ */
 All Data Structures Functions Variables