Library to communicate with a ST LIS331DLH

Committer:
atommota
Date:
Tue Jun 17 21:40:59 2014 +0000
Revision:
10:dcd11c84305e
Parent:
9:5af73355984f
Fixed bug in last release where getAccelZ was being cast to an int instead of a float. Changed the set range functions to preserve the contents of the CTRL_REG_4 register. Additionally, those functions now return the value written to the register.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
atommota 2:d4b810a888b5 1 /**
atommota 2:d4b810a888b5 2 * @section LICENSE
atommota 2:d4b810a888b5 3 *
atommota 2:d4b810a888b5 4 *
atommota 2:d4b810a888b5 5 * @section DESCRIPTION
atommota 2:d4b810a888b5 6 *
atommota 2:d4b810a888b5 7 * LIS331 triple axis, digital interface, accelerometer.
atommota 2:d4b810a888b5 8 * Based off Aaron Berk's ITG3200 Gyro Library
atommota 2:d4b810a888b5 9 *
atommota 2:d4b810a888b5 10 * Datasheet:
atommota 2:d4b810a888b5 11 *
atommota 2:d4b810a888b5 12 * http://www.st.com/stonline/products/literature/ds/13951.pdf
atommota 2:d4b810a888b5 13 */
atommota 2:d4b810a888b5 14
atommota 2:d4b810a888b5 15 #ifndef LIS331_H
atommota 2:d4b810a888b5 16 #define LIS331_H
atommota 2:d4b810a888b5 17
atommota 2:d4b810a888b5 18 /**
atommota 2:d4b810a888b5 19 * Includes
atommota 2:d4b810a888b5 20 */
atommota 2:d4b810a888b5 21 #include "mbed.h"
atommota 2:d4b810a888b5 22
atommota 2:d4b810a888b5 23 /**
atommota 2:d4b810a888b5 24 * Defines
atommota 2:d4b810a888b5 25 */
atommota 2:d4b810a888b5 26 #define LIS331_I2C_ADDRESS 0x19 //7-bit address.
atommota 2:d4b810a888b5 27
atommota 2:d4b810a888b5 28 //-----------
atommota 2:d4b810a888b5 29 // Registers
atommota 2:d4b810a888b5 30 //-----------
atommota 3:147d95b7a525 31 #define WHO_AM_I_REG_LIS331 0x0F
atommota 2:d4b810a888b5 32 #define ACCEL_XOUT_H_REG 0x29
atommota 2:d4b810a888b5 33 #define ACCEL_XOUT_L_REG 0x28
atommota 2:d4b810a888b5 34 #define ACCEL_YOUT_H_REG 0x2B
atommota 2:d4b810a888b5 35 #define ACCEL_YOUT_L_REG 0x2A
atommota 2:d4b810a888b5 36 #define ACCEL_ZOUT_H_REG 0x2D
atommota 2:d4b810a888b5 37 #define ACCEL_ZOUT_L_REG 0x2C
atommota 2:d4b810a888b5 38
atommota 2:d4b810a888b5 39 #define CTRL_REG_1 0x20
atommota 2:d4b810a888b5 40 #define CTRL_REG_2 0x21
atommota 2:d4b810a888b5 41 #define CTRL_REG_3 0x22
atommota 2:d4b810a888b5 42 #define CTRL_REG_4 0x23
atommota 2:d4b810a888b5 43 #define CTRL_REG_5 0x24
atommota 2:d4b810a888b5 44
atommota 2:d4b810a888b5 45 #define STATUS_REG 0x27
atommota 2:d4b810a888b5 46
atommota 2:d4b810a888b5 47
atommota 2:d4b810a888b5 48 //------------------------------
atommota 2:d4b810a888b5 49 // Power Mode and Output Data Rates
atommota 2:d4b810a888b5 50 //------------------------------
atommota 2:d4b810a888b5 51 #define POWER_DOWN 0x6F
atommota 2:d4b810a888b5 52 #define NORMAL_50HZ 0x27
atommota 2:d4b810a888b5 53 #define NORMAL_100HZ 0x2F
atommota 2:d4b810a888b5 54 #define NORMAL_400HZ 0x37
atommota 2:d4b810a888b5 55 #define NORMAL_1000HZ 0x3F
atommota 2:d4b810a888b5 56 #define LOW_POWER_0_5HZ 0x47
atommota 2:d4b810a888b5 57 #define LOW_POWER_1HZ 0x67
atommota 2:d4b810a888b5 58 #define LOW_POWER_2HZ 0x87
atommota 2:d4b810a888b5 59 #define LOW_POWER_5HZ 0xA7
atommota 2:d4b810a888b5 60 #define LOW_POWER_10HZ 0xC7
atommota 2:d4b810a888b5 61
atommota 2:d4b810a888b5 62 /**
atommota 2:d4b810a888b5 63 * LIS331 triple axis digital accelerometer.
atommota 2:d4b810a888b5 64 */
atommota 2:d4b810a888b5 65 class LIS331 {
atommota 2:d4b810a888b5 66
atommota 2:d4b810a888b5 67 public:
atommota 2:d4b810a888b5 68
atommota 2:d4b810a888b5 69 /**
atommota 2:d4b810a888b5 70 * Constructor.
atommota 2:d4b810a888b5 71 *
atommota 2:d4b810a888b5 72 * Sets FS_SEL to 0x03 for proper opertaion.
atommota 2:d4b810a888b5 73 *
atommota 2:d4b810a888b5 74 * @param sda - mbed pin to use for the SDA I2C line.
atommota 2:d4b810a888b5 75 * @param scl - mbed pin to use for the SCL I2C line.
atommota 2:d4b810a888b5 76 */
atommota 2:d4b810a888b5 77 LIS331(PinName sda, PinName scl);
atommota 2:d4b810a888b5 78
atommota 2:d4b810a888b5 79 /**
atommota 2:d4b810a888b5 80 * Get the identity of the device.
atommota 2:d4b810a888b5 81 *
atommota 2:d4b810a888b5 82 * @return The contents of the Who Am I register which contains the I2C
atommota 2:d4b810a888b5 83 * address of the device.
atommota 2:d4b810a888b5 84 */
atommota 2:d4b810a888b5 85 char getWhoAmI(void);
atommota 2:d4b810a888b5 86
atommota 2:d4b810a888b5 87
atommota 2:d4b810a888b5 88
atommota 2:d4b810a888b5 89
atommota 2:d4b810a888b5 90
atommota 2:d4b810a888b5 91
atommota 2:d4b810a888b5 92
atommota 2:d4b810a888b5 93
atommota 2:d4b810a888b5 94
atommota 2:d4b810a888b5 95 /**
atommota 2:d4b810a888b5 96 * Set the power mode (power down, low power, normal mode)
atommota 2:d4b810a888b5 97 *
atommota 2:d4b810a888b5 98 *
atommota 2:d4b810a888b5 99 * @param
atommota 2:d4b810a888b5 100 *
atommota 2:d4b810a888b5 101 * Power Mode | Output Data Rate (Hz) | Low-pass Filter Cut off (Hz) | #define
atommota 2:d4b810a888b5 102 * --------------------------------------------------------------------------------
atommota 2:d4b810a888b5 103 * Power-down | -- | -- | POWER_DOWN
atommota 2:d4b810a888b5 104 * Normal | 50 | 37 | NORMAL_50HZ
atommota 2:d4b810a888b5 105 * Normal | 100 | 74 | NORMAL_100HZ
atommota 2:d4b810a888b5 106 * Normal | 400 | 292 | NORMAL_400HZ
atommota 2:d4b810a888b5 107 * Normal | 1000 | 780 | NORMAL_1000HZ
atommota 2:d4b810a888b5 108 * Low-power | 0.5 | -- | LOW_POWER_0_5HZ
atommota 2:d4b810a888b5 109 * Low-power | 1 | -- | LOW_POWER_1HZ
atommota 2:d4b810a888b5 110 * Low-power | 2 | -- | LOW_POWER_2HZ
atommota 2:d4b810a888b5 111 * Low-power | 5 | -- | LOW_POWER_5HZ
atommota 2:d4b810a888b5 112 * Low-power | 10 | -- | LOW_POWER_10HZ
atommota 2:d4b810a888b5 113 */
atommota 2:d4b810a888b5 114
atommota 2:d4b810a888b5 115 void setPowerMode(char power_mode);
atommota 2:d4b810a888b5 116
atommota 2:d4b810a888b5 117
atommota 2:d4b810a888b5 118
atommota 2:d4b810a888b5 119 /**
atommota 2:d4b810a888b5 120 * Get the current power mode
atommota 2:d4b810a888b5 121 *
atommota 2:d4b810a888b5 122 * @return
atommota 2:d4b810a888b5 123 */
atommota 2:d4b810a888b5 124 char getPowerMode(void);
atommota 2:d4b810a888b5 125
atommota 2:d4b810a888b5 126
atommota 2:d4b810a888b5 127 char getInterruptConfiguration(void);
atommota 2:d4b810a888b5 128
atommota 2:d4b810a888b5 129 /**
atommota 2:d4b810a888b5 130 * Set the interrupt configuration.
atommota 2:d4b810a888b5 131 *
atommota 2:d4b810a888b5 132 * See datasheet for configuration byte details.
atommota 2:d4b810a888b5 133 *
atommota 2:d4b810a888b5 134 * 7 6 5 4
atommota 2:d4b810a888b5 135 * +-------+-------+------+--------+
atommota 2:d4b810a888b5 136 * | IHL | PP_OD | LIR2 | I2_CFG |
atommota 2:d4b810a888b5 137 * +-------+-------+------+--------+
atommota 2:d4b810a888b5 138 *
atommota 2:d4b810a888b5 139 * 3 2 1 0
atommota 2:d4b810a888b5 140 * +---------+------+---------+---------+
atommota 2:d4b810a888b5 141 * | I2_CFG0 | LIR1 | I1_CFG1 | I1-CFG0 |
atommota 2:d4b810a888b5 142 * +---------+------+---------+---------+
atommota 2:d4b810a888b5 143 *
atommota 2:d4b810a888b5 144 * IHL Interrupt active high or low. 0:active high; 1:active low (default:0)
atommota 2:d4b810a888b5 145 * PP_OD Push-pull/Open drain selection on interrupt pad. 0:push-pull; 1:open drain (default:0)
atommota 2:d4b810a888b5 146 * LIR2 Latch interupt request on INT2_SRC register, with INT2_SRC register cleared by reading INT2_SRC itself
atommota 2:d4b810a888b5 147 * 0: irq not latched; 1:irq latched (default:0)
atommota 2:d4b810a888b5 148 * I2_CFG1, I2_CFG0 See datasheet table
atommota 2:d4b810a888b5 149 * LIR1 Latch interupt request on INT1_SRC register, with INT1_SRC register cleared by reading INT1_SRC itself
atommota 2:d4b810a888b5 150 * 0: irq not latched; 1:irq latched (default:0)
atommota 2:d4b810a888b5 151 * I1_CFG1, I1_CFG0 See datasheet table
atommota 2:d4b810a888b5 152 *
atommota 2:d4b810a888b5 153 * @param config Configuration byte to write to INT_CFG register.
atommota 2:d4b810a888b5 154 */
atommota 2:d4b810a888b5 155
atommota 2:d4b810a888b5 156
atommota 2:d4b810a888b5 157 // void setInterruptConfiguration(char config);
atommota 2:d4b810a888b5 158
atommota 2:d4b810a888b5 159 /**
atommota 2:d4b810a888b5 160 * Check the status register
atommota 2:d4b810a888b5 161 *
atommota 2:d4b810a888b5 162 * @return
atommota 2:d4b810a888b5 163 *
atommota 2:d4b810a888b5 164 */
atommota 5:3443fb9646bd 165
atommota 9:5af73355984f 166
atommota 7:7eeb1fe4d787 167 /**
atommota 7:7eeb1fe4d787 168 * Set the Full Scale Range to +/- 8g's.
atommota 10:dcd11c84305e 169 * Returns the value written to CTRL_REG_4
atommota 7:7eeb1fe4d787 170 */
atommota 10:dcd11c84305e 171 char setFullScaleRange8g(void);
atommota 7:7eeb1fe4d787 172
atommota 7:7eeb1fe4d787 173 /**
atommota 7:7eeb1fe4d787 174 * Set the Full Scale Range to +/- 4g's.
atommota 10:dcd11c84305e 175 * Returns the value written to CTRL_REG_4
atommota 7:7eeb1fe4d787 176 */
atommota 10:dcd11c84305e 177 char setFullScaleRange4g(void);
atommota 7:7eeb1fe4d787 178
atommota 7:7eeb1fe4d787 179 /**
atommota 7:7eeb1fe4d787 180 * Set the Full Scale Range to +/- 2g's.
atommota 10:dcd11c84305e 181 * Returns the value written to CTRL_REG_4
atommota 7:7eeb1fe4d787 182 */
atommota 10:dcd11c84305e 183 char setFullScaleRange2g(void);
atommota 5:3443fb9646bd 184
atommota 2:d4b810a888b5 185
atommota 2:d4b810a888b5 186 char getAccelStatus(void);
atommota 2:d4b810a888b5 187
atommota 2:d4b810a888b5 188
atommota 2:d4b810a888b5 189
atommota 2:d4b810a888b5 190
atommota 2:d4b810a888b5 191 /**
atommota 7:7eeb1fe4d787 192 * Get the output for the x-axis accelerometer.
atommota 2:d4b810a888b5 193 *
atommota 7:7eeb1fe4d787 194 * @return The output on the x-axis in engineering units (g's).
atommota 2:d4b810a888b5 195 */
atommota 8:ff365f926a52 196 float getAccelX(void);
atommota 2:d4b810a888b5 197
atommota 2:d4b810a888b5 198 /**
atommota 7:7eeb1fe4d787 199 * Get the output for the y-axis accelerometer.
atommota 2:d4b810a888b5 200 *
atommota 7:7eeb1fe4d787 201 * @return The output on the y-axis in engineering units (g's).
atommota 2:d4b810a888b5 202 */
atommota 8:ff365f926a52 203 float getAccelY(void);
atommota 2:d4b810a888b5 204
atommota 2:d4b810a888b5 205 /**
atommota 7:7eeb1fe4d787 206 * Get the output on the z-axis accelerometer.
atommota 2:d4b810a888b5 207 *
atommota 7:7eeb1fe4d787 208 * @return The output on the z-axis in engineering units (g's).
atommota 2:d4b810a888b5 209 */
atommota 10:dcd11c84305e 210 float getAccelZ(void);
atommota 2:d4b810a888b5 211
atommota 2:d4b810a888b5 212
atommota 2:d4b810a888b5 213 private:
atommota 2:d4b810a888b5 214
atommota 6:53e92e7fc073 215 float scaling_factor;
atommota 6:53e92e7fc073 216 int current_range;
atommota 6:53e92e7fc073 217
atommota 2:d4b810a888b5 218 I2C i2c_;
atommota 2:d4b810a888b5 219
atommota 2:d4b810a888b5 220 };
atommota 2:d4b810a888b5 221
atommota 2:d4b810a888b5 222 #endif /* LIS331_H */
atommota 2:d4b810a888b5 223