ADXL345 test on L476

Dependencies:   mbed

Committer:
tifo
Date:
Wed Feb 07 16:05:59 2018 +0000
Revision:
4:d850da8732c1
Parent:
0:a0f7c6807a3a
buzzer added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tifo 0:a0f7c6807a3a 1 #ifndef ADXL435_H
tifo 0:a0f7c6807a3a 2 #define ADXL435_H
tifo 0:a0f7c6807a3a 3
tifo 0:a0f7c6807a3a 4 #include "mbed.h"
tifo 0:a0f7c6807a3a 5
tifo 0:a0f7c6807a3a 6
tifo 0:a0f7c6807a3a 7 /*************************** REGISTER MAP ***************************/
tifo 0:a0f7c6807a3a 8 #define ADXL345_DEVID 0x00 // Device ID
tifo 0:a0f7c6807a3a 9 #define ADXL345_RESERVED1 0x01 // Reserved. Do Not Access.
tifo 0:a0f7c6807a3a 10 #define ADXL345_THRESH_TAP 0x1D // Tap Threshold.
tifo 0:a0f7c6807a3a 11 #define ADXL345_OFSX 0x1E // X-Axis Offset.
tifo 0:a0f7c6807a3a 12 #define ADXL345_OFSY 0x1F // Y-Axis Offset.
tifo 0:a0f7c6807a3a 13 #define ADXL345_OFSZ 0x20 // Z- Axis Offset.
tifo 0:a0f7c6807a3a 14 #define ADXL345_DUR 0x21 // Tap Duration.
tifo 0:a0f7c6807a3a 15 #define ADXL345_LATENT 0x22 // Tap Latency.
tifo 0:a0f7c6807a3a 16 #define ADXL345_WINDOW 0x23 // Tap Window.
tifo 0:a0f7c6807a3a 17 #define ADXL345_THRESH_ACT 0x24 // Activity Threshold
tifo 0:a0f7c6807a3a 18 #define ADXL345_THRESH_INACT 0x25 // Inactivity Threshold
tifo 0:a0f7c6807a3a 19 #define ADXL345_TIME_INACT 0x26 // Inactivity Time
tifo 0:a0f7c6807a3a 20 #define ADXL345_ACT_INACT_CTL 0x27 // Axis Enable Control for Activity and Inactivity Detection
tifo 0:a0f7c6807a3a 21 #define ADXL345_THRESH_FF 0x28 // Free-Fall Threshold.
tifo 0:a0f7c6807a3a 22 #define ADXL345_TIME_FF 0x29 // Free-Fall Time.
tifo 0:a0f7c6807a3a 23 #define ADXL345_TAP_AXES 0x2A // Axis Control for Tap/Double Tap.
tifo 0:a0f7c6807a3a 24 #define ADXL345_ACT_TAP_STATUS 0x2B // Source of Tap/Double Tap
tifo 0:a0f7c6807a3a 25 #define ADXL345_BW_RATE 0x2C // Data Rate and Power mode Control
tifo 0:a0f7c6807a3a 26 #define ADXL345_POWER_CTL 0x2D // Power-Saving Features Control
tifo 0:a0f7c6807a3a 27 #define ADXL345_INT_ENABLE 0x2E // Interrupt Enable Control
tifo 0:a0f7c6807a3a 28 #define ADXL345_INT_MAP 0x2F // Interrupt Mapping Control
tifo 0:a0f7c6807a3a 29 #define ADXL345_INT_SOURCE 0x30 // Source of Interrupts
tifo 0:a0f7c6807a3a 30 #define ADXL345_DATA_FORMAT 0x31 // Data Format Control
tifo 0:a0f7c6807a3a 31 #define ADXL345_DATAX0 0x32 // X-Axis Data 0
tifo 0:a0f7c6807a3a 32 #define ADXL345_DATAX1 0x33 // X-Axis Data 1
tifo 0:a0f7c6807a3a 33 #define ADXL345_DATAY0 0x34 // Y-Axis Data 0
tifo 0:a0f7c6807a3a 34 #define ADXL345_DATAY1 0x35 // Y-Axis Data 1
tifo 0:a0f7c6807a3a 35 #define ADXL345_DATAZ0 0x36 // Z-Axis Data 0
tifo 0:a0f7c6807a3a 36 #define ADXL345_DATAZ1 0x37 // Z-Axis Data 1
tifo 0:a0f7c6807a3a 37 #define ADXL345_FIFO_CTL 0x38 // FIFO Control
tifo 0:a0f7c6807a3a 38 #define ADXL345_FIFO_STATUS 0x39 // FIFO Status
tifo 0:a0f7c6807a3a 39
tifo 0:a0f7c6807a3a 40 #define ADXL345_BW_1600 0xF // 1111 IDD = 40uA
tifo 0:a0f7c6807a3a 41 #define ADXL345_BW_800 0xE // 1110 IDD = 90uA
tifo 0:a0f7c6807a3a 42 #define ADXL345_BW_400 0xD // 1101 IDD = 140uA
tifo 0:a0f7c6807a3a 43 #define ADXL345_BW_200 0xC // 1100 IDD = 140uA
tifo 0:a0f7c6807a3a 44 #define ADXL345_BW_100 0xB // 1011 IDD = 140uA
tifo 0:a0f7c6807a3a 45 #define ADXL345_BW_50 0xA // 1010 IDD = 140uA
tifo 0:a0f7c6807a3a 46 #define ADXL345_BW_25 0x9 // 1001 IDD = 90uA
tifo 0:a0f7c6807a3a 47 #define ADXL345_BW_12_5 0x8 // 1000 IDD = 60uA
tifo 0:a0f7c6807a3a 48 #define ADXL345_BW_6_25 0x7 // 0111 IDD = 50uA
tifo 0:a0f7c6807a3a 49 #define ADXL345_BW_3_13 0x6 // 0110 IDD = 45uA
tifo 0:a0f7c6807a3a 50 #define ADXL345_BW_1_56 0x5 // 0101 IDD = 40uA
tifo 0:a0f7c6807a3a 51 #define ADXL345_BW_0_78 0x4 // 0100 IDD = 34uA
tifo 0:a0f7c6807a3a 52 #define ADXL345_BW_0_39 0x3 // 0011 IDD = 23uA
tifo 0:a0f7c6807a3a 53 #define ADXL345_BW_0_20 0x2 // 0010 IDD = 23uA
tifo 0:a0f7c6807a3a 54 #define ADXL345_BW_0_10 0x1 // 0001 IDD = 23uA
tifo 0:a0f7c6807a3a 55 #define ADXL345_BW_0_05 0x0 // 0000 IDD = 23uA
tifo 0:a0f7c6807a3a 56
tifo 0:a0f7c6807a3a 57
tifo 0:a0f7c6807a3a 58 /************************** INTERRUPT PINS **************************/
tifo 0:a0f7c6807a3a 59 #define ADXL345_INT1_PIN 0x00 //INT1: 0
tifo 0:a0f7c6807a3a 60 #define ADXL345_INT2_PIN 0x01 //INT2: 1
tifo 0:a0f7c6807a3a 61
tifo 0:a0f7c6807a3a 62
tifo 0:a0f7c6807a3a 63 /********************** INTERRUPT BIT POSITION **********************/
tifo 0:a0f7c6807a3a 64 #define ADXL345_INT_DATA_READY_BIT 0x07
tifo 0:a0f7c6807a3a 65 #define ADXL345_INT_SINGLE_TAP_BIT 0x06
tifo 0:a0f7c6807a3a 66 #define ADXL345_INT_DOUBLE_TAP_BIT 0x05
tifo 0:a0f7c6807a3a 67 #define ADXL345_INT_ACTIVITY_BIT 0x04
tifo 0:a0f7c6807a3a 68 #define ADXL345_INT_INACTIVITY_BIT 0x03
tifo 0:a0f7c6807a3a 69 #define ADXL345_INT_FREE_FALL_BIT 0x02
tifo 0:a0f7c6807a3a 70 #define ADXL345_INT_WATERMARK_BIT 0x01
tifo 0:a0f7c6807a3a 71 #define ADXL345_INT_OVERRUNY_BIT 0x00
tifo 0:a0f7c6807a3a 72
tifo 0:a0f7c6807a3a 73 #define ADXL345_DATA_READY 0x07
tifo 0:a0f7c6807a3a 74 #define ADXL345_SINGLE_TAP 0x06
tifo 0:a0f7c6807a3a 75 #define ADXL345_DOUBLE_TAP 0x05
tifo 0:a0f7c6807a3a 76 #define ADXL345_ACTIVITY 0x04
tifo 0:a0f7c6807a3a 77 #define ADXL345_INACTIVITY 0x03
tifo 0:a0f7c6807a3a 78 #define ADXL345_FREE_FALL 0x02
tifo 0:a0f7c6807a3a 79 #define ADXL345_WATERMARK 0x01
tifo 0:a0f7c6807a3a 80 #define ADXL345_OVERRUNY 0x00
tifo 0:a0f7c6807a3a 81
tifo 0:a0f7c6807a3a 82
tifo 0:a0f7c6807a3a 83 /****************************** ERRORS ******************************/
tifo 0:a0f7c6807a3a 84 #define ADXL345_OK 1 // No Error
tifo 0:a0f7c6807a3a 85 #define ADXL345_ERROR 0 // Error Exists
tifo 0:a0f7c6807a3a 86
tifo 0:a0f7c6807a3a 87 #define ADXL345_NO_ERROR 0 // Initial State
tifo 0:a0f7c6807a3a 88 #define ADXL345_READ_ERROR 1 // Accelerometer Reading Error
tifo 0:a0f7c6807a3a 89 #define ADXL345_BAD_ARG 2 // Bad Argument
tifo 0:a0f7c6807a3a 90
tifo 0:a0f7c6807a3a 91
tifo 0:a0f7c6807a3a 92 class ADXL345
tifo 0:a0f7c6807a3a 93 {
tifo 0:a0f7c6807a3a 94 public:
tifo 0:a0f7c6807a3a 95 bool status; // Set When Error Exists
tifo 0:a0f7c6807a3a 96
tifo 0:a0f7c6807a3a 97 char error_code; // Initial State
tifo 0:a0f7c6807a3a 98 double gains[3]; // Counts to Gs
tifo 0:a0f7c6807a3a 99
tifo 0:a0f7c6807a3a 100
tifo 0:a0f7c6807a3a 101
tifo 0:a0f7c6807a3a 102 ADXL345();
tifo 0:a0f7c6807a3a 103
tifo 0:a0f7c6807a3a 104 void powerOn();
tifo 0:a0f7c6807a3a 105 void readAccel(int* xyx);
tifo 0:a0f7c6807a3a 106 void readAccel(int* x, int* y, int* z);
tifo 0:a0f7c6807a3a 107 void get_Gxyz(double *xyz);
tifo 0:a0f7c6807a3a 108
tifo 0:a0f7c6807a3a 109 void setTapThreshold(int tapThreshold);
tifo 0:a0f7c6807a3a 110 int getTapThreshold();
tifo 0:a0f7c6807a3a 111 void setAxisGains(double *_gains);
tifo 0:a0f7c6807a3a 112 void getAxisGains(double *_gains);
tifo 0:a0f7c6807a3a 113 void setAxisOffset(int x, int y, int z);
tifo 0:a0f7c6807a3a 114 void getAxisOffset(int* x, int* y, int*z);
tifo 0:a0f7c6807a3a 115 void setTapDuration(int tapDuration);
tifo 0:a0f7c6807a3a 116 int getTapDuration();
tifo 0:a0f7c6807a3a 117 void setDoubleTapLatency(int doubleTapLatency);
tifo 0:a0f7c6807a3a 118 int getDoubleTapLatency();
tifo 0:a0f7c6807a3a 119 void setDoubleTapWindow(int doubleTapWindow);
tifo 0:a0f7c6807a3a 120 int getDoubleTapWindow();
tifo 0:a0f7c6807a3a 121 void setActivityThreshold(int activityThreshold);
tifo 0:a0f7c6807a3a 122 int getActivityThreshold();
tifo 0:a0f7c6807a3a 123 void setInactivityThreshold(int inactivityThreshold);
tifo 0:a0f7c6807a3a 124 int getInactivityThreshold();
tifo 0:a0f7c6807a3a 125 void setTimeInactivity(int timeInactivity);
tifo 0:a0f7c6807a3a 126 int getTimeInactivity();
tifo 0:a0f7c6807a3a 127 void setFreeFallThreshold(int freeFallthreshold);
tifo 0:a0f7c6807a3a 128 int getFreeFallThreshold();
tifo 0:a0f7c6807a3a 129 void setFreeFallDuration(int freeFallDuration);
tifo 0:a0f7c6807a3a 130 int getFreeFallDuration();
tifo 0:a0f7c6807a3a 131
tifo 0:a0f7c6807a3a 132 bool isActivityXEnabled();
tifo 0:a0f7c6807a3a 133 bool isActivityYEnabled();
tifo 0:a0f7c6807a3a 134 bool isActivityZEnabled();
tifo 0:a0f7c6807a3a 135 bool isInactivityXEnabled();
tifo 0:a0f7c6807a3a 136 bool isInactivityYEnabled();
tifo 0:a0f7c6807a3a 137 bool isInactivityZEnabled();
tifo 0:a0f7c6807a3a 138 bool isActivityAc();
tifo 0:a0f7c6807a3a 139 bool isInactivityAc();
tifo 0:a0f7c6807a3a 140 void setActivityAc(bool state);
tifo 0:a0f7c6807a3a 141 void setInactivityAc(bool state);
tifo 0:a0f7c6807a3a 142
tifo 0:a0f7c6807a3a 143 bool getSuppressBit();
tifo 0:a0f7c6807a3a 144 void setSuppressBit(bool state);
tifo 0:a0f7c6807a3a 145 bool isTapDetectionOnX();
tifo 0:a0f7c6807a3a 146 void setTapDetectionOnX(bool state);
tifo 0:a0f7c6807a3a 147 bool isTapDetectionOnY();
tifo 0:a0f7c6807a3a 148 void setTapDetectionOnY(bool state);
tifo 0:a0f7c6807a3a 149 bool isTapDetectionOnZ();
tifo 0:a0f7c6807a3a 150 void setTapDetectionOnZ(bool state);
tifo 0:a0f7c6807a3a 151 void setTapDetectionOnXYZ(bool stateX, bool stateY, bool stateZ);
tifo 0:a0f7c6807a3a 152
tifo 0:a0f7c6807a3a 153 void setActivityX(bool state);
tifo 0:a0f7c6807a3a 154 void setActivityY(bool state);
tifo 0:a0f7c6807a3a 155 void setActivityZ(bool state);
tifo 0:a0f7c6807a3a 156 void setActivityXYZ(bool stateX, bool stateY, bool stateZ);
tifo 0:a0f7c6807a3a 157 void setInactivityX(bool state);
tifo 0:a0f7c6807a3a 158 void setInactivityY(bool state);
tifo 0:a0f7c6807a3a 159 void setInactivityZ(bool state);
tifo 0:a0f7c6807a3a 160 void setInactivityXYZ(bool stateX, bool stateY, bool stateZ);
tifo 0:a0f7c6807a3a 161
tifo 0:a0f7c6807a3a 162 bool isActivitySourceOnX();
tifo 0:a0f7c6807a3a 163 bool isActivitySourceOnY();
tifo 0:a0f7c6807a3a 164 bool isActivitySourceOnZ();
tifo 0:a0f7c6807a3a 165 bool isTapSourceOnX();
tifo 0:a0f7c6807a3a 166 bool isTapSourceOnY();
tifo 0:a0f7c6807a3a 167 bool isTapSourceOnZ();
tifo 0:a0f7c6807a3a 168 bool isAsleep();
tifo 0:a0f7c6807a3a 169
tifo 0:a0f7c6807a3a 170 bool isLowPower();
tifo 0:a0f7c6807a3a 171 void setLowPower(bool state);
tifo 0:a0f7c6807a3a 172
tifo 0:a0f7c6807a3a 173
tifo 0:a0f7c6807a3a 174 bool triggered(char interrupts, int mask);
tifo 0:a0f7c6807a3a 175
tifo 0:a0f7c6807a3a 176 char getInterruptSource();
tifo 0:a0f7c6807a3a 177 bool getInterruptSource(char interruptBit);
tifo 0:a0f7c6807a3a 178 bool getInterruptMapping(char interruptBit);
tifo 0:a0f7c6807a3a 179 void setInterruptMapping(char interruptBit, bool interruptPin);
tifo 0:a0f7c6807a3a 180 bool isInterruptEnabled(char interruptBit);
tifo 0:a0f7c6807a3a 181 void setInterrupt(char interruptBit, bool state);
tifo 0:a0f7c6807a3a 182 void setImportantInterruptMapping(int single_tap, int double_tap, int free_fall, int activity, int inactivity);
tifo 0:a0f7c6807a3a 183 void InactivityINT(bool status);
tifo 0:a0f7c6807a3a 184 void ActivityINT(bool status);
tifo 0:a0f7c6807a3a 185 void FreeFallINT(bool status);
tifo 0:a0f7c6807a3a 186 void doubleTapINT(bool status);
tifo 0:a0f7c6807a3a 187 void singleTapINT(bool status);
tifo 0:a0f7c6807a3a 188
tifo 0:a0f7c6807a3a 189 void getRangeSetting(char* rangeSetting);
tifo 0:a0f7c6807a3a 190 void setRangeSetting(int val);
tifo 0:a0f7c6807a3a 191 bool getSelfTestBit();
tifo 0:a0f7c6807a3a 192 void setSelfTestBit(bool selfTestBit);
tifo 0:a0f7c6807a3a 193 bool getInterruptLevelBit();
tifo 0:a0f7c6807a3a 194 void setInterruptLevelBit(bool interruptLevelBit);
tifo 0:a0f7c6807a3a 195 bool getFullResBit();
tifo 0:a0f7c6807a3a 196 void setFullResBit(bool fullResBit);
tifo 0:a0f7c6807a3a 197 bool getJustifyBit();
tifo 0:a0f7c6807a3a 198 void setJustifyBit(bool justifyBit);
tifo 0:a0f7c6807a3a 199 void printAllRegister();
tifo 0:a0f7c6807a3a 200
tifo 0:a0f7c6807a3a 201 private:
tifo 0:a0f7c6807a3a 202 void writeToI2C(char address, char val);
tifo 0:a0f7c6807a3a 203 void readFromI2C(char address, int num, char buff[]);
tifo 0:a0f7c6807a3a 204 void setRegisterBit(char regAdress, int bitPos, bool state);
tifo 0:a0f7c6807a3a 205 bool getRegisterBit(char regAdress, int bitPos);
tifo 0:a0f7c6807a3a 206 char _buff[6] ; // 6 chars Buffer
tifo 0:a0f7c6807a3a 207 };
tifo 0:a0f7c6807a3a 208 void print_char(char val);
tifo 0:a0f7c6807a3a 209
tifo 0:a0f7c6807a3a 210 #endif