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.
adxl345.h
00001 #ifndef ADXL435_H 00002 #define ADXL435_H 00003 00004 #include "mbed.h" 00005 00006 00007 /*************************** REGISTER MAP ***************************/ 00008 #define ADXL345_DEVID 0x00 // Device ID 00009 #define ADXL345_RESERVED1 0x01 // Reserved. Do Not Access. 00010 #define ADXL345_THRESH_TAP 0x1D // Tap Threshold. 00011 #define ADXL345_OFSX 0x1E // X-Axis Offset. 00012 #define ADXL345_OFSY 0x1F // Y-Axis Offset. 00013 #define ADXL345_OFSZ 0x20 // Z- Axis Offset. 00014 #define ADXL345_DUR 0x21 // Tap Duration. 00015 #define ADXL345_LATENT 0x22 // Tap Latency. 00016 #define ADXL345_WINDOW 0x23 // Tap Window. 00017 #define ADXL345_THRESH_ACT 0x24 // Activity Threshold 00018 #define ADXL345_THRESH_INACT 0x25 // Inactivity Threshold 00019 #define ADXL345_TIME_INACT 0x26 // Inactivity Time 00020 #define ADXL345_ACT_INACT_CTL 0x27 // Axis Enable Control for Activity and Inactivity Detection 00021 #define ADXL345_THRESH_FF 0x28 // Free-Fall Threshold. 00022 #define ADXL345_TIME_FF 0x29 // Free-Fall Time. 00023 #define ADXL345_TAP_AXES 0x2A // Axis Control for Tap/Double Tap. 00024 #define ADXL345_ACT_TAP_STATUS 0x2B // Source of Tap/Double Tap 00025 #define ADXL345_BW_RATE 0x2C // Data Rate and Power mode Control 00026 #define ADXL345_POWER_CTL 0x2D // Power-Saving Features Control 00027 #define ADXL345_INT_ENABLE 0x2E // Interrupt Enable Control 00028 #define ADXL345_INT_MAP 0x2F // Interrupt Mapping Control 00029 #define ADXL345_INT_SOURCE 0x30 // Source of Interrupts 00030 #define ADXL345_DATA_FORMAT 0x31 // Data Format Control 00031 #define ADXL345_DATAX0 0x32 // X-Axis Data 0 00032 #define ADXL345_DATAX1 0x33 // X-Axis Data 1 00033 #define ADXL345_DATAY0 0x34 // Y-Axis Data 0 00034 #define ADXL345_DATAY1 0x35 // Y-Axis Data 1 00035 #define ADXL345_DATAZ0 0x36 // Z-Axis Data 0 00036 #define ADXL345_DATAZ1 0x37 // Z-Axis Data 1 00037 #define ADXL345_FIFO_CTL 0x38 // FIFO Control 00038 #define ADXL345_FIFO_STATUS 0x39 // FIFO Status 00039 00040 #define ADXL345_BW_1600 0xF // 1111 IDD = 40uA 00041 #define ADXL345_BW_800 0xE // 1110 IDD = 90uA 00042 #define ADXL345_BW_400 0xD // 1101 IDD = 140uA 00043 #define ADXL345_BW_200 0xC // 1100 IDD = 140uA 00044 #define ADXL345_BW_100 0xB // 1011 IDD = 140uA 00045 #define ADXL345_BW_50 0xA // 1010 IDD = 140uA 00046 #define ADXL345_BW_25 0x9 // 1001 IDD = 90uA 00047 #define ADXL345_BW_12_5 0x8 // 1000 IDD = 60uA 00048 #define ADXL345_BW_6_25 0x7 // 0111 IDD = 50uA 00049 #define ADXL345_BW_3_13 0x6 // 0110 IDD = 45uA 00050 #define ADXL345_BW_1_56 0x5 // 0101 IDD = 40uA 00051 #define ADXL345_BW_0_78 0x4 // 0100 IDD = 34uA 00052 #define ADXL345_BW_0_39 0x3 // 0011 IDD = 23uA 00053 #define ADXL345_BW_0_20 0x2 // 0010 IDD = 23uA 00054 #define ADXL345_BW_0_10 0x1 // 0001 IDD = 23uA 00055 #define ADXL345_BW_0_05 0x0 // 0000 IDD = 23uA 00056 00057 00058 /************************** INTERRUPT PINS **************************/ 00059 #define ADXL345_INT1_PIN 0x00 //INT1: 0 00060 #define ADXL345_INT2_PIN 0x01 //INT2: 1 00061 00062 00063 /********************** INTERRUPT BIT POSITION **********************/ 00064 #define ADXL345_INT_DATA_READY_BIT 0x07 00065 #define ADXL345_INT_SINGLE_TAP_BIT 0x06 00066 #define ADXL345_INT_DOUBLE_TAP_BIT 0x05 00067 #define ADXL345_INT_ACTIVITY_BIT 0x04 00068 #define ADXL345_INT_INACTIVITY_BIT 0x03 00069 #define ADXL345_INT_FREE_FALL_BIT 0x02 00070 #define ADXL345_INT_WATERMARK_BIT 0x01 00071 #define ADXL345_INT_OVERRUNY_BIT 0x00 00072 00073 #define ADXL345_DATA_READY 0x07 00074 #define ADXL345_SINGLE_TAP 0x06 00075 #define ADXL345_DOUBLE_TAP 0x05 00076 #define ADXL345_ACTIVITY 0x04 00077 #define ADXL345_INACTIVITY 0x03 00078 #define ADXL345_FREE_FALL 0x02 00079 #define ADXL345_WATERMARK 0x01 00080 #define ADXL345_OVERRUNY 0x00 00081 00082 00083 /****************************** ERRORS ******************************/ 00084 #define ADXL345_OK 1 // No Error 00085 #define ADXL345_ERROR 0 // Error Exists 00086 00087 #define ADXL345_NO_ERROR 0 // Initial State 00088 #define ADXL345_READ_ERROR 1 // Accelerometer Reading Error 00089 #define ADXL345_BAD_ARG 2 // Bad Argument 00090 00091 00092 class ADXL345 00093 { 00094 public: 00095 bool status; // Set When Error Exists 00096 00097 char error_code; // Initial State 00098 double gains[3]; // Counts to Gs 00099 00100 00101 00102 ADXL345(); 00103 00104 void powerOn(); 00105 void readAccel(int* xyx); 00106 void readAccel(int* x, int* y, int* z); 00107 void get_Gxyz(double *xyz); 00108 00109 void setTapThreshold(int tapThreshold); 00110 int getTapThreshold(); 00111 void setAxisGains(double *_gains); 00112 void getAxisGains(double *_gains); 00113 void setAxisOffset(int x, int y, int z); 00114 void getAxisOffset(int* x, int* y, int*z); 00115 void setTapDuration(int tapDuration); 00116 int getTapDuration(); 00117 void setDoubleTapLatency(int doubleTapLatency); 00118 int getDoubleTapLatency(); 00119 void setDoubleTapWindow(int doubleTapWindow); 00120 int getDoubleTapWindow(); 00121 void setActivityThreshold(int activityThreshold); 00122 int getActivityThreshold(); 00123 void setInactivityThreshold(int inactivityThreshold); 00124 int getInactivityThreshold(); 00125 void setTimeInactivity(int timeInactivity); 00126 int getTimeInactivity(); 00127 void setFreeFallThreshold(int freeFallthreshold); 00128 int getFreeFallThreshold(); 00129 void setFreeFallDuration(int freeFallDuration); 00130 int getFreeFallDuration(); 00131 00132 bool isActivityXEnabled(); 00133 bool isActivityYEnabled(); 00134 bool isActivityZEnabled(); 00135 bool isInactivityXEnabled(); 00136 bool isInactivityYEnabled(); 00137 bool isInactivityZEnabled(); 00138 bool isActivityAc(); 00139 bool isInactivityAc(); 00140 void setActivityAc(bool state); 00141 void setInactivityAc(bool state); 00142 00143 bool getSuppressBit(); 00144 void setSuppressBit(bool state); 00145 bool isTapDetectionOnX(); 00146 void setTapDetectionOnX(bool state); 00147 bool isTapDetectionOnY(); 00148 void setTapDetectionOnY(bool state); 00149 bool isTapDetectionOnZ(); 00150 void setTapDetectionOnZ(bool state); 00151 void setTapDetectionOnXYZ(bool stateX, bool stateY, bool stateZ); 00152 00153 void setActivityX(bool state); 00154 void setActivityY(bool state); 00155 void setActivityZ(bool state); 00156 void setActivityXYZ(bool stateX, bool stateY, bool stateZ); 00157 void setInactivityX(bool state); 00158 void setInactivityY(bool state); 00159 void setInactivityZ(bool state); 00160 void setInactivityXYZ(bool stateX, bool stateY, bool stateZ); 00161 00162 bool isActivitySourceOnX(); 00163 bool isActivitySourceOnY(); 00164 bool isActivitySourceOnZ(); 00165 bool isTapSourceOnX(); 00166 bool isTapSourceOnY(); 00167 bool isTapSourceOnZ(); 00168 bool isAsleep(); 00169 00170 bool isLowPower(); 00171 void setLowPower(bool state); 00172 00173 00174 bool triggered(char interrupts, int mask); 00175 00176 char getInterruptSource(); 00177 bool getInterruptSource(char interruptBit); 00178 bool getInterruptMapping(char interruptBit); 00179 void setInterruptMapping(char interruptBit, bool interruptPin); 00180 bool isInterruptEnabled(char interruptBit); 00181 void setInterrupt(char interruptBit, bool state); 00182 void setImportantInterruptMapping(int single_tap, int double_tap, int free_fall, int activity, int inactivity); 00183 void InactivityINT(bool status); 00184 void ActivityINT(bool status); 00185 void FreeFallINT(bool status); 00186 void doubleTapINT(bool status); 00187 void singleTapINT(bool status); 00188 00189 void getRangeSetting(char* rangeSetting); 00190 void setRangeSetting(int val); 00191 bool getSelfTestBit(); 00192 void setSelfTestBit(bool selfTestBit); 00193 bool getInterruptLevelBit(); 00194 void setInterruptLevelBit(bool interruptLevelBit); 00195 bool getFullResBit(); 00196 void setFullResBit(bool fullResBit); 00197 bool getJustifyBit(); 00198 void setJustifyBit(bool justifyBit); 00199 void printAllRegister(); 00200 00201 private: 00202 void writeToI2C(char address, char val); 00203 void readFromI2C(char address, int num, char buff[]); 00204 void setRegisterBit(char regAdress, int bitPos, bool state); 00205 bool getRegisterBit(char regAdress, int bitPos); 00206 char _buff[6] ; // 6 chars Buffer 00207 }; 00208 void print_char(char val); 00209 00210 #endif
Generated on Wed Jul 13 2022 12:29:35 by
1.7.2