Library to communicate with a ST LIS331DLH

Committer:
atommota
Date:
Fri Nov 12 23:40:28 2010 +0000
Revision:
0:d835d6cac146
Child:
1:02c1f5bb1c90
v0.5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
atommota 0:d835d6cac146 1 /**
atommota 0:d835d6cac146 2 * @section LICENSE
atommota 0:d835d6cac146 3 *
atommota 0:d835d6cac146 4 *
atommota 0:d835d6cac146 5 * @section DESCRIPTION
atommota 0:d835d6cac146 6 *
atommota 0:d835d6cac146 7 * LIS331 triple axis, digital interface, accelerometer.
atommota 0:d835d6cac146 8 * Based off Aaron Berk's ITG3200 Gyro Library
atommota 0:d835d6cac146 9 *
atommota 0:d835d6cac146 10 * Datasheet:
atommota 0:d835d6cac146 11 *
atommota 0:d835d6cac146 12 * http://www.st.com/stonline/products/literature/ds/13951.pdf
atommota 0:d835d6cac146 13 */
atommota 0:d835d6cac146 14
atommota 0:d835d6cac146 15 #ifndef LIS331_H
atommota 0:d835d6cac146 16 #define LIS331_H
atommota 0:d835d6cac146 17
atommota 0:d835d6cac146 18 /**
atommota 0:d835d6cac146 19 * Includes
atommota 0:d835d6cac146 20 */
atommota 0:d835d6cac146 21 #include "mbed.h"
atommota 0:d835d6cac146 22
atommota 0:d835d6cac146 23 /**
atommota 0:d835d6cac146 24 * Defines
atommota 0:d835d6cac146 25 */
atommota 0:d835d6cac146 26 #define LIS331_I2C_ADDRESS 0x19 //7-bit address.
atommota 0:d835d6cac146 27
atommota 0:d835d6cac146 28 //-----------
atommota 0:d835d6cac146 29 // Registers
atommota 0:d835d6cac146 30 //-----------
atommota 0:d835d6cac146 31 #define WHO_AM_I_REG 0x0F
atommota 0:d835d6cac146 32 #define ACCEL_XOUT_H_REG 0x29
atommota 0:d835d6cac146 33 #define ACCEL_XOUT_L_REG 0x28
atommota 0:d835d6cac146 34 #define ACCEL_YOUT_H_REG 0x2B
atommota 0:d835d6cac146 35 #define ACCEL_YOUT_L_REG 0x2A
atommota 0:d835d6cac146 36 #define ACCEL_ZOUT_H_REG 0x2D
atommota 0:d835d6cac146 37 #define ACCEL_ZOUT_L_REG 0x2C
atommota 0:d835d6cac146 38
atommota 0:d835d6cac146 39
atommota 0:d835d6cac146 40
atommota 0:d835d6cac146 41 #define CTRL_REG_1 0x20
atommota 0:d835d6cac146 42 #define CTRL_REG_2 0x21
atommota 0:d835d6cac146 43 #define CTRL_REG_3 0x22
atommota 0:d835d6cac146 44 #define CTRL_REG_4 0x23
atommota 0:d835d6cac146 45 #define CTRL_REG_5 0x24
atommota 0:d835d6cac146 46
atommota 0:d835d6cac146 47 #define STATUS_REG 0x27
atommota 0:d835d6cac146 48
atommota 0:d835d6cac146 49
atommota 0:d835d6cac146 50 //------------------------------
atommota 0:d835d6cac146 51 // Power Mode and Output Data Rates
atommota 0:d835d6cac146 52 //------------------------------
atommota 0:d835d6cac146 53 #define POWER_DOWN 0x6F
atommota 0:d835d6cac146 54 #define NORMAL_50HZ 0x27
atommota 0:d835d6cac146 55 #define NORMAL_100HZ 0x2F
atommota 0:d835d6cac146 56 #define NORMAL_400HZ 0x37
atommota 0:d835d6cac146 57 #define NORMAL_1000HZ 0x3F
atommota 0:d835d6cac146 58 #define LOW_POWER_0_5HZ 0x47
atommota 0:d835d6cac146 59 #define LOW_POWER_1HZ 0x67
atommota 0:d835d6cac146 60 #define LOW_POWER_2HZ 0x87
atommota 0:d835d6cac146 61 #define LOW_POWER_5HZ 0xA7
atommota 0:d835d6cac146 62 #define LOW_POWER_10HZ 0xC7
atommota 0:d835d6cac146 63
atommota 0:d835d6cac146 64 /**
atommota 0:d835d6cac146 65 * LIS331 triple axis digital accelerometer.
atommota 0:d835d6cac146 66 */
atommota 0:d835d6cac146 67 class LIS331 {
atommota 0:d835d6cac146 68
atommota 0:d835d6cac146 69 public:
atommota 0:d835d6cac146 70
atommota 0:d835d6cac146 71 /**
atommota 0:d835d6cac146 72 * Constructor.
atommota 0:d835d6cac146 73 *
atommota 0:d835d6cac146 74 * Sets FS_SEL to 0x03 for proper opertaion.
atommota 0:d835d6cac146 75 *
atommota 0:d835d6cac146 76 * @param sda - mbed pin to use for the SDA I2C line.
atommota 0:d835d6cac146 77 * @param scl - mbed pin to use for the SCL I2C line.
atommota 0:d835d6cac146 78 */
atommota 0:d835d6cac146 79 LIS331(PinName sda, PinName scl);
atommota 0:d835d6cac146 80
atommota 0:d835d6cac146 81 /**
atommota 0:d835d6cac146 82 * Get the identity of the device.
atommota 0:d835d6cac146 83 *
atommota 0:d835d6cac146 84 * @return The contents of the Who Am I register which contains the I2C
atommota 0:d835d6cac146 85 * address of the device.
atommota 0:d835d6cac146 86 */
atommota 0:d835d6cac146 87 char getWhoAmI(void);
atommota 0:d835d6cac146 88
atommota 0:d835d6cac146 89
atommota 0:d835d6cac146 90
atommota 0:d835d6cac146 91 /**
atommota 0:d835d6cac146 92 * Get the current power mode
atommota 0:d835d6cac146 93 *
atommota 0:d835d6cac146 94 * @return
atommota 0:d835d6cac146 95 */
atommota 0:d835d6cac146 96
atommota 0:d835d6cac146 97 //char getPowerMode(void);
atommota 0:d835d6cac146 98
atommota 0:d835d6cac146 99
atommota 0:d835d6cac146 100
atommota 0:d835d6cac146 101
atommota 0:d835d6cac146 102 /**
atommota 0:d835d6cac146 103 * Set the power mode (power down, low power, normal mode)
atommota 0:d835d6cac146 104 *
atommota 0:d835d6cac146 105 *
atommota 0:d835d6cac146 106 * @param
atommota 0:d835d6cac146 107 *
atommota 0:d835d6cac146 108 * Power Mode | Output Data Rate (Hz) | Low-pass Filter Cut off (Hz) | #define
atommota 0:d835d6cac146 109 * --------------------------------------------------------------------------------
atommota 0:d835d6cac146 110 * Power-down | -- | -- | POWER_DOWN
atommota 0:d835d6cac146 111 * Normal | 50 | 37 | NORMAL_50HZ
atommota 0:d835d6cac146 112 * Normal | 100 | 74 | NORMAL_100HZ
atommota 0:d835d6cac146 113 * Normal | 400 | 292 | NORMAL_400HZ
atommota 0:d835d6cac146 114 * Normal | 1000 | 780 | NORMAL_1000HZ
atommota 0:d835d6cac146 115 * Low-power | 0.5 | -- | LOW_POWER_0_5HZ
atommota 0:d835d6cac146 116 * Low-power | 1 | -- | LOW_POWER_1HZ
atommota 0:d835d6cac146 117 * Low-power | 2 | -- | LOW_POWER_2HZ
atommota 0:d835d6cac146 118 * Low-power | 5 | -- | LOW_POWER_5HZ
atommota 0:d835d6cac146 119 * Low-power | 10 | -- | LOW_POWER_10HZ
atommota 0:d835d6cac146 120 */
atommota 0:d835d6cac146 121
atommota 0:d835d6cac146 122 //void setPowerMode(char powermode);
atommota 0:d835d6cac146 123
atommota 0:d835d6cac146 124
atommota 0:d835d6cac146 125 char getInterruptConfiguration(void);
atommota 0:d835d6cac146 126
atommota 0:d835d6cac146 127 /**
atommota 0:d835d6cac146 128 * Set the interrupt configuration.
atommota 0:d835d6cac146 129 *
atommota 0:d835d6cac146 130 * See datasheet for configuration byte details.
atommota 0:d835d6cac146 131 *
atommota 0:d835d6cac146 132 * 7 6 5 4
atommota 0:d835d6cac146 133 * +-------+-------+------+--------+
atommota 0:d835d6cac146 134 * | IHL | PP_OD | LIR2 | I2_CFG |
atommota 0:d835d6cac146 135 * +-------+-------+------+--------+
atommota 0:d835d6cac146 136 *
atommota 0:d835d6cac146 137 * 3 2 1 0
atommota 0:d835d6cac146 138 * +---------+------+---------+---------+
atommota 0:d835d6cac146 139 * | I2_CFG0 | LIR1 | I1_CFG1 | I1-CFG0 |
atommota 0:d835d6cac146 140 * +---------+------+---------+---------+
atommota 0:d835d6cac146 141 *
atommota 0:d835d6cac146 142 * IHL Interrupt active high or low. 0:active high; 1:active low (default:0)
atommota 0:d835d6cac146 143 * PP_OD Push-pull/Open drain selection on interrupt pad. 0:push-pull; 1:open drain (default:0)
atommota 0:d835d6cac146 144 * LIR2 Latch interupt request on INT2_SRC register, with INT2_SRC register cleared by reading INT2_SRC itself
atommota 0:d835d6cac146 145 * 0: irq not latched; 1:irq latched (default:0)
atommota 0:d835d6cac146 146 * I2_CFG1, I2_CFG0 See datasheet table
atommota 0:d835d6cac146 147 * LIR1 Latch interupt request on INT1_SRC register, with INT1_SRC register cleared by reading INT1_SRC itself
atommota 0:d835d6cac146 148 * 0: irq not latched; 1:irq latched (default:0)
atommota 0:d835d6cac146 149 * I1_CFG1, I1_CFG0 See datasheet table
atommota 0:d835d6cac146 150 *
atommota 0:d835d6cac146 151 * @param config Configuration byte to write to INT_CFG register.
atommota 0:d835d6cac146 152 */
atommota 0:d835d6cac146 153
atommota 0:d835d6cac146 154
atommota 0:d835d6cac146 155 // void setInterruptConfiguration(char config);
atommota 0:d835d6cac146 156
atommota 0:d835d6cac146 157 /**
atommota 0:d835d6cac146 158 * Check the status register
atommota 0:d835d6cac146 159 *
atommota 0:d835d6cac146 160 * @return
atommota 0:d835d6cac146 161 *
atommota 0:d835d6cac146 162 */
atommota 0:d835d6cac146 163
atommota 0:d835d6cac146 164 char getAccelStatus(void);
atommota 0:d835d6cac146 165
atommota 0:d835d6cac146 166
atommota 0:d835d6cac146 167
atommota 0:d835d6cac146 168
atommota 0:d835d6cac146 169 /**
atommota 0:d835d6cac146 170 * Get the temperature of the device.
atommota 0:d835d6cac146 171 *
atommota 0:d835d6cac146 172 * @return The temperature in degrees celsius.
atommota 0:d835d6cac146 173 */
atommota 0:d835d6cac146 174 //float getTemperature(void);
atommota 0:d835d6cac146 175
atommota 0:d835d6cac146 176 /**
atommota 0:d835d6cac146 177 * Get the output for the x-axis gyroscope.
atommota 0:d835d6cac146 178 *
atommota 0:d835d6cac146 179 * Typical sensitivity is 14.375 LSB/(degrees/sec).
atommota 0:d835d6cac146 180 *
atommota 0:d835d6cac146 181 * @return The output on the x-axis in raw ADC counts.
atommota 0:d835d6cac146 182 */
atommota 0:d835d6cac146 183 int getAccelX(void);
atommota 0:d835d6cac146 184
atommota 0:d835d6cac146 185 /**
atommota 0:d835d6cac146 186 * Get the output for the y-axis gyroscope.
atommota 0:d835d6cac146 187 *
atommota 0:d835d6cac146 188 * Typical sensitivity is 14.375 LSB/(degrees/sec).
atommota 0:d835d6cac146 189 *
atommota 0:d835d6cac146 190 * @return The output on the y-axis in raw ADC counts.
atommota 0:d835d6cac146 191 */
atommota 0:d835d6cac146 192 int getAccelY(void);
atommota 0:d835d6cac146 193
atommota 0:d835d6cac146 194 /**
atommota 0:d835d6cac146 195 * Get the output on the z-axis gyroscope.
atommota 0:d835d6cac146 196 *
atommota 0:d835d6cac146 197 * Typical sensitivity is 14.375 LSB/(degrees/sec).
atommota 0:d835d6cac146 198 *
atommota 0:d835d6cac146 199 * @return The output on the z-axis in raw ADC counts.
atommota 0:d835d6cac146 200 */
atommota 0:d835d6cac146 201 int getAccelZ(void);
atommota 0:d835d6cac146 202
atommota 0:d835d6cac146 203 /**
atommota 0:d835d6cac146 204 * Get the power management configuration.
atommota 0:d835d6cac146 205 *
atommota 0:d835d6cac146 206 * See the datasheet for register contents details.
atommota 0:d835d6cac146 207 *
atommota 0:d835d6cac146 208 * 7 6 5 4
atommota 0:d835d6cac146 209 * +---------+-------+---------+---------+
atommota 0:d835d6cac146 210 * | H_RESET | SLEEP | STBY_XG | STBY_YG |
atommota 0:d835d6cac146 211 * +---------+-------+---------+---------+
atommota 0:d835d6cac146 212 *
atommota 0:d835d6cac146 213 * 3 2 1 0
atommota 0:d835d6cac146 214 * +---------+----------+----------+----------+
atommota 0:d835d6cac146 215 * | STBY_ZG | CLK_SEL2 | CLK_SEL1 | CLK_SEL0 |
atommota 0:d835d6cac146 216 * +---------+----------+----------+----------+
atommota 0:d835d6cac146 217 *
atommota 0:d835d6cac146 218 * H_RESET Reset device and internal registers to the power-up-default settings.
atommota 0:d835d6cac146 219 * SLEEP Enable low power sleep mode.
atommota 0:d835d6cac146 220 * STBY_XG Put gyro X in standby mode (1=standby, 0=normal).
atommota 0:d835d6cac146 221 * STBY_YG Put gyro Y in standby mode (1=standby, 0=normal).
atommota 0:d835d6cac146 222 * STBY_ZG Put gyro Z in standby mode (1=standby, 0=normal).
atommota 0:d835d6cac146 223 * CLK_SEL Select device clock source:
atommota 0:d835d6cac146 224 *
atommota 0:d835d6cac146 225 * CLK_SEL | Clock Source
atommota 0:d835d6cac146 226 * --------+--------------
atommota 0:d835d6cac146 227 * 0 Internal oscillator
atommota 0:d835d6cac146 228 * 1 PLL with X Gyro reference
atommota 0:d835d6cac146 229 * 2 PLL with Y Gyro reference
atommota 0:d835d6cac146 230 * 3 PLL with Z Gyro reference
atommota 0:d835d6cac146 231 * 4 PLL with external 32.768kHz reference
atommota 0:d835d6cac146 232 * 5 PLL with external 19.2MHz reference
atommota 0:d835d6cac146 233 * 6 Reserved
atommota 0:d835d6cac146 234 * 7 Reserved
atommota 0:d835d6cac146 235 *
atommota 0:d835d6cac146 236 * @return The contents of the PWR_MGM register.
atommota 0:d835d6cac146 237 */
atommota 0:d835d6cac146 238 // char getPowerManagement(void);
atommota 0:d835d6cac146 239
atommota 0:d835d6cac146 240 /**
atommota 0:d835d6cac146 241 * Set power management configuration.
atommota 0:d835d6cac146 242 *
atommota 0:d835d6cac146 243 * See the datasheet for configuration byte details
atommota 0:d835d6cac146 244 *
atommota 0:d835d6cac146 245 * 7 6 5 4
atommota 0:d835d6cac146 246 * +---------+-------+---------+---------+
atommota 0:d835d6cac146 247 * | H_RESET | SLEEP | STBY_XG | STBY_YG |
atommota 0:d835d6cac146 248 * +---------+-------+---------+---------+
atommota 0:d835d6cac146 249 *
atommota 0:d835d6cac146 250 * 3 2 1 0
atommota 0:d835d6cac146 251 * +---------+----------+----------+----------+
atommota 0:d835d6cac146 252 * | STBY_ZG | CLK_SEL2 | CLK_SEL1 | CLK_SEL0 |
atommota 0:d835d6cac146 253 * +---------+----------+----------+----------+
atommota 0:d835d6cac146 254 *
atommota 0:d835d6cac146 255 * H_RESET Reset device and internal registers to the power-up-default settings.
atommota 0:d835d6cac146 256 * SLEEP Enable low power sleep mode.
atommota 0:d835d6cac146 257 * STBY_XG Put gyro X in standby mode (1=standby, 0=normal).
atommota 0:d835d6cac146 258 * STBY_YG Put gyro Y in standby mode (1=standby, 0=normal).
atommota 0:d835d6cac146 259 * STBY_ZG Put gyro Z in standby mode (1=standby, 0=normal).
atommota 0:d835d6cac146 260 * CLK_SEL Select device clock source:
atommota 0:d835d6cac146 261 *
atommota 0:d835d6cac146 262 * CLK_SEL | Clock Source
atommota 0:d835d6cac146 263 * --------+--------------
atommota 0:d835d6cac146 264 * 0 Internal oscillator
atommota 0:d835d6cac146 265 * 1 PLL with X Gyro reference
atommota 0:d835d6cac146 266 * 2 PLL with Y Gyro reference
atommota 0:d835d6cac146 267 * 3 PLL with Z Gyro reference
atommota 0:d835d6cac146 268 * 4 PLL with external 32.768kHz reference
atommota 0:d835d6cac146 269 * 5 PLL with external 19.2MHz reference
atommota 0:d835d6cac146 270 * 6 Reserved
atommota 0:d835d6cac146 271 * 7 Reserved
atommota 0:d835d6cac146 272 *
atommota 0:d835d6cac146 273 * @param config The configuration byte to write to the PWR_MGM register.
atommota 0:d835d6cac146 274 */
atommota 0:d835d6cac146 275 // void setPowerManagement(char config);
atommota 0:d835d6cac146 276
atommota 0:d835d6cac146 277 private:
atommota 0:d835d6cac146 278
atommota 0:d835d6cac146 279 I2C i2c_;
atommota 0:d835d6cac146 280
atommota 0:d835d6cac146 281 };
atommota 0:d835d6cac146 282
atommota 0:d835d6cac146 283 #endif /* LIS331_H */
atommota 0:d835d6cac146 284