Kenji Arai / ADXL345
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ADXL345.h Source File

ADXL345.h

00001 /*
00002  * mbed library program
00003  *  ADXL345: 3-axis accelerometer, made by Analog Devices
00004  *  http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
00005  *
00006  * Copyright (c) 2017 Kenji Arai / JH1PJL
00007  *  http://www.page.sannet.ne.jp/kenjia/index.html
00008  *  http://mbed.org/users/kenjiArai/
00009  *      Modify:     August    13th, 2017
00010  *      Revised:    September 23rd, 2017
00011  *
00012  */
00013 
00014 #ifndef ADXL345_H
00015 #define ADXL345_H
00016 
00017 #include "mbed.h"
00018 
00019 //  ADXL345 Address
00020 //  7bit address = 0x1D or 0x53 (depends on ALT ADDRESS pin)
00021 #define ADXL345_G_CHIP_ADDR  (0x53 << 1)    //  ALT ADDRESS = Ground
00022 #define ADXL345_V_CHIP_ADDR  (0x1D << 1)    //  ALT ADDRESS = Vdd
00023 
00024 //   ADXL345 ID
00025 #define ADXL345_DEVICE_ID       0xE5
00026 
00027 //  Register's definition
00028 #define ADXL345_DEVID           0x00
00029 #define ADXL345_THRESH_TAP      0x1D
00030 #define ADXL345_OFSX            0x1E
00031 #define ADXL345_OFSY            0x1F
00032 #define ADXL345_OFSZ            0x20
00033 #define ADXL345_DUR             0x21
00034 #define ADXL345_LATENT          0x22
00035 #define ADXL345_WINDOW          0x23
00036 #define ADXL345_THRESH_ACT      0x24
00037 #define ADXL345_THRESH_INACT    0x25
00038 #define ADXL345_TIME_INACT      0x26
00039 #define ADXL345_ACT_INACT_CTL   0x27
00040 #define ADXL345_THRESH_FF       0x28
00041 #define ADXL345_TIME_FF         0x29
00042 #define ADXL345_TAP_AXES        0x2A
00043 #define ADXL345_ACT_TAP_STATUS  0x2B
00044 #define ADXL345_BW_RATE         0x2C
00045 #define ADXL345_POWER_CTL       0x2D
00046 #define ADXL345_INT_ENABLE      0x2E
00047 #define ADXL345_INT_MAP         0x2F
00048 #define ADXL345_INT_SOURCE      0x30
00049 #define ADXL345_DATA_FORMAT     0x31
00050 #define ADXL345_DATAX0          0x32
00051 #define ADXL345_DATAX1          0x33
00052 #define ADXL345_DATAY0          0x34
00053 #define ADXL345_DATAY1          0x35
00054 #define ADXL345_DATAZ0          0x36
00055 #define ADXL345_DATAZ1          0x37
00056 #define ADXL345_FIFO_CTL        0x38
00057 #define ADXL345_FIFO_STATUS     0x39
00058 
00059 // Data Rate
00060 #define ADXL345_LOW_PWR         0x10
00061 #define ADXL345_NOT_LOW_PWR     0x00
00062 #define ADXL345_DR_R10HZ        0x00
00063 #define ADXL345_DR_R20HZ        0x01
00064 #define ADXL345_DR_R39HZ        0x02
00065 #define ADXL345_DR_R78HZ        0x03
00066 #define ADXL345_DR_1R56HZ       0x04
00067 #define ADXL345_DR_3R13HZ       0x05
00068 #define ADXL345_DR_6R25HZ       0x06
00069 #define ADXL345_DR_12R5HZ       0x07
00070 #define ADXL345_DR_25HZ         0x08
00071 #define ADXL345_DR_50HZ         0x09
00072 #define ADXL345_DR_100HZ        0x0A
00073 #define ADXL345_DR_200HZ        0x0B
00074 #define ADXL345_DR_400HZ        0x0C
00075 #define ADXL345_DR_800HZ        0x0D
00076 #define ADXL345_DR_1R6KHZ       0x0E
00077 #define ADXL345_DR_3R2KHZ       0x0F
00078 
00079 // FIFO Mode
00080 #define ADXL345_FIFO_BYPASS     0x00    // Not use FIFO
00081 #define ADXL345_FIFO_FIFO       0x40    // FIFO collects 32 then stop
00082 #define ADXL345_FIFO_STREAM     0x80    // last 32 & continue sampling
00083 #define ADXL345_FIFO_TRIGER     0xC0    // Start by trigger
00084 
00085 // FIFO Trigger Source
00086 #define ADXL345_FIFO_TRG_INT1   0x00
00087 #define ADXL345_FIFO_TRG_INT2   0x20
00088 
00089 // FIFO Trigger Source
00090 #define ADXL345_FIFO_SAMPLES    0x31    // default value
00091 
00092 // Full Scale
00093 #define ADXL345_FS_2G           0x00
00094 #define ADXL345_FS_4G           0x01
00095 #define ADXL345_FS_8G           0x02
00096 #define ADXL345_FS_16G          0x03
00097 #define ADXL345_FULL_RES_16G    0x0B
00098 
00099 /** Interface for Analog Devices : 3-axis accelerometer
00100  *      Chip: ADXL345
00101  *
00102  * @code
00103  * #include "mbed.h"
00104  *
00105  * // I2C Communication
00106  * I2C i2c(D14,D15);  // SDA, SCL
00107  * ADXL345 acc(i2c);
00108  *
00109  * int main() {
00110  *  float f[3];
00111  *   while(1){
00112  *      acc.read_data(f);
00113  *   }
00114  * }
00115  * @endcode
00116  */
00117 
00118 class ADXL345
00119 {
00120 public:
00121     /** Configure data pin (with other devices on I2C line)
00122       * @param I2C PinName SDA &SDL
00123       * @param device address
00124       * @param output data rate selection, power down mode, 0.1Hz to 3.2KHz
00125       * @param full scale selection, +/-2g to +/-16g
00126       */
00127     ADXL345(PinName p_sda, PinName p_scl,
00128            uint8_t addr, uint8_t data_rate, uint8_t fullscale);
00129 
00130     /** Configure data pin (with other devices on I2C line)
00131       * @param I2C previous definition
00132       * @param device address
00133       */
00134     ADXL345(PinName p_sda, PinName p_scl, uint8_t addr);
00135 
00136     /** Configure data pin (with other devices on I2C line)
00137       * @param I2C previous definition
00138       */
00139     ADXL345(PinName p_sda, PinName p_scl);
00140 
00141     /** Configure data pin (with other devices on I2C line)
00142       * @param I2C previous definition
00143       * @param device address
00144       * @param output data rate selection, power down mode, 0.1Hz to 3.2KHz
00145       * @param full scale selection, +/-2g to +/-16g
00146       */
00147     ADXL345(I2C& p_i2c,
00148            uint8_t addr, uint8_t data_rate, uint8_t fullscale);
00149 
00150     /** Configure data pin (with other devices on I2C line)
00151       * @param I2C previous definition
00152       * @default output data rate selection = 100Hz
00153       * @default full scale selection = +/-2g
00154       */
00155     ADXL345(I2C& p_i2c, uint8_t addr);
00156 
00157     /** Configure data pin (with other devices on I2C line)
00158       * @param I2C previous definition
00159       * @default address check both G & V
00160       * @default output data rate selection = 100Hz
00161       * @default full scale selection = +/-2g
00162       */
00163     ADXL345(I2C& p_i2c);
00164 
00165     /** Read a float type data from acc
00166       * @param float type of three arry's address, e.g. float dt_usr[3];
00167       * @return acc motion data unit: m/s/s(m/s2)
00168       * @return dt_usr[0]->x, dt_usr[1]->y, dt_usr[2]->z
00169       */
00170     void read_data(float *dt_usr);
00171 
00172     /** Read a float type data from acc
00173       * @param float type of three arry's address, e.g. float dt_usr[3];
00174       * @return acc motion data unit: mg
00175       * @return dt_usr[0]->x, dt_usr[1]->y, dt_usr[2]->z
00176       */
00177     void read_mg_data(float *dt_usr);
00178 
00179     /** Read a float type data from acc
00180       * @param float type of three arry's address, e.g. float dt_usr[3];
00181       * @return acc motion data unit: g
00182       * @return dt_usr[0]->x, dt_usr[1]->y, dt_usr[2]->z
00183       */
00184     void read_g_data(float *dt_usr);
00185 
00186     /** Read a acc ID number
00187       * @param none
00188       * @return ID is okay (I_AM_ ADXL345(0x33)) or not
00189       */
00190     uint8_t read_id();
00191 
00192     /** Read Data Ready flag
00193       * @param none
00194       * @return true = Ready
00195       */
00196     bool data_ready();
00197 
00198     /** Set I2C clock frequency
00199       * @param freq.
00200       * @return none
00201       */
00202     void frequency(int hz);
00203 
00204     /** Read register (general purpose)
00205       * @param register's address
00206       * @return register data
00207       */
00208     uint8_t read_reg(uint8_t addr);
00209 
00210     /** Write register (general purpose)
00211       * @param register's address
00212       * @param data
00213       * @return none
00214       */
00215     void write_reg(uint8_t addr, uint8_t data);
00216 
00217     /** data print for debug
00218       * @param none
00219       * @return none
00220       */
00221     void debug_print(void);
00222 
00223     /** Self-Test Feature
00224       * @param none
00225       * @return none
00226       */
00227     void self_test(void);
00228 
00229 protected:
00230     void initialize(uint8_t, uint8_t, uint8_t);
00231     void read_reg_data(char *data);
00232     void read_mg_g_data(float *dt_usr, uint8_t n);
00233 
00234     I2C *_i2c_p;
00235     I2C &_i2c;
00236 
00237 private:
00238     float   fs_factor;  // full scale factor
00239     char    dt[2];      // working buffer
00240     uint8_t acc_addr;   // acc sensor address
00241     uint8_t acc_id;     // acc ID
00242     bool    acc_ready;  // acc is on I2C line = 1, not = 0
00243     uint8_t setting_data[4];    // Reg. recovery data
00244 
00245 };
00246 
00247 #endif      // ADXL345_H