Library to communicate with a ST LIS331DLH
LIS331.cpp@4:c3df518a938e, 2011-01-07 (annotated)
- Committer:
- atommota
- Date:
- Fri Jan 07 16:31:35 2011 +0000
- Revision:
- 4:c3df518a938e
- Parent:
- 3:147d95b7a525
- Child:
- 5:3443fb9646bd
Added support to set full scale ranges of 2, 4, and 8g\s
Updated LIS331DLH datasheet web reference
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:c3df518a938e | 7 | * LIS331DLH 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 | 4:c3df518a938e | 12 | * http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00213470.pdf |
atommota | 2:d4b810a888b5 | 13 | */ |
atommota | 2:d4b810a888b5 | 14 | |
atommota | 2:d4b810a888b5 | 15 | /** |
atommota | 2:d4b810a888b5 | 16 | * Includes |
atommota | 2:d4b810a888b5 | 17 | */ |
atommota | 2:d4b810a888b5 | 18 | |
atommota | 2:d4b810a888b5 | 19 | #include "LIS331.h" |
atommota | 2:d4b810a888b5 | 20 | |
atommota | 2:d4b810a888b5 | 21 | LIS331::LIS331(PinName sda, PinName scl) : i2c_(sda, scl) { |
atommota | 2:d4b810a888b5 | 22 | |
atommota | 2:d4b810a888b5 | 23 | //400kHz, fast mode. |
atommota | 2:d4b810a888b5 | 24 | i2c_.frequency(400000); |
atommota | 2:d4b810a888b5 | 25 | |
atommota | 2:d4b810a888b5 | 26 | |
atommota | 2:d4b810a888b5 | 27 | //Power Up Device, Set Output data rate, Enable All 3 Axis |
atommota | 2:d4b810a888b5 | 28 | //See datasheet for details. |
atommota | 2:d4b810a888b5 | 29 | char tx[2]; |
atommota | 2:d4b810a888b5 | 30 | //char tx2[2]; |
atommota | 2:d4b810a888b5 | 31 | //char rx[1]; |
atommota | 2:d4b810a888b5 | 32 | tx[0] = CTRL_REG_1; |
atommota | 2:d4b810a888b5 | 33 | //CTRL_REG_1 [00111111] / [0x3F] to power up, set output rate to 1000Hz, and enable all 3 axis. |
atommota | 2:d4b810a888b5 | 34 | tx[1] = 0x3F; |
atommota | 2:d4b810a888b5 | 35 | i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2); |
atommota | 2:d4b810a888b5 | 36 | |
atommota | 2:d4b810a888b5 | 37 | } |
atommota | 2:d4b810a888b5 | 38 | |
atommota | 2:d4b810a888b5 | 39 | char LIS331::getWhoAmI(void){ |
atommota | 2:d4b810a888b5 | 40 | |
atommota | 2:d4b810a888b5 | 41 | //WhoAmI Register address. |
atommota | 3:147d95b7a525 | 42 | char tx = WHO_AM_I_REG_LIS331; |
atommota | 2:d4b810a888b5 | 43 | char rx; |
atommota | 2:d4b810a888b5 | 44 | |
atommota | 2:d4b810a888b5 | 45 | i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1); |
atommota | 2:d4b810a888b5 | 46 | |
atommota | 2:d4b810a888b5 | 47 | i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1); |
atommota | 2:d4b810a888b5 | 48 | |
atommota | 2:d4b810a888b5 | 49 | return rx; |
atommota | 2:d4b810a888b5 | 50 | |
atommota | 2:d4b810a888b5 | 51 | } |
atommota | 2:d4b810a888b5 | 52 | |
atommota | 2:d4b810a888b5 | 53 | |
atommota | 2:d4b810a888b5 | 54 | |
atommota | 2:d4b810a888b5 | 55 | |
atommota | 2:d4b810a888b5 | 56 | void LIS331::setPowerMode(char power_mode){ |
atommota | 2:d4b810a888b5 | 57 | // Currently sets all 3 axis to enabled. Will be set to preserve existing status in future |
atommota | 2:d4b810a888b5 | 58 | char tx[2]; |
atommota | 2:d4b810a888b5 | 59 | tx[0] = CTRL_REG_1; |
atommota | 2:d4b810a888b5 | 60 | tx[1] = power_mode; |
atommota | 2:d4b810a888b5 | 61 | |
atommota | 2:d4b810a888b5 | 62 | i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2); |
atommota | 2:d4b810a888b5 | 63 | |
atommota | 2:d4b810a888b5 | 64 | } |
atommota | 2:d4b810a888b5 | 65 | |
atommota | 2:d4b810a888b5 | 66 | char LIS331::getPowerMode(void){ |
atommota | 2:d4b810a888b5 | 67 | |
atommota | 2:d4b810a888b5 | 68 | char tx = CTRL_REG_1; |
atommota | 2:d4b810a888b5 | 69 | char rx; |
atommota | 2:d4b810a888b5 | 70 | |
atommota | 2:d4b810a888b5 | 71 | i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1); |
atommota | 2:d4b810a888b5 | 72 | |
atommota | 2:d4b810a888b5 | 73 | i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1); |
atommota | 2:d4b810a888b5 | 74 | |
atommota | 2:d4b810a888b5 | 75 | |
atommota | 2:d4b810a888b5 | 76 | return rx; |
atommota | 2:d4b810a888b5 | 77 | |
atommota | 2:d4b810a888b5 | 78 | } |
atommota | 2:d4b810a888b5 | 79 | |
atommota | 2:d4b810a888b5 | 80 | |
atommota | 2:d4b810a888b5 | 81 | |
atommota | 2:d4b810a888b5 | 82 | char LIS331::getInterruptConfiguration(void){ |
atommota | 2:d4b810a888b5 | 83 | |
atommota | 2:d4b810a888b5 | 84 | char tx = CTRL_REG_3; |
atommota | 2:d4b810a888b5 | 85 | char rx; |
atommota | 2:d4b810a888b5 | 86 | |
atommota | 2:d4b810a888b5 | 87 | i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1); |
atommota | 2:d4b810a888b5 | 88 | |
atommota | 2:d4b810a888b5 | 89 | i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1); |
atommota | 2:d4b810a888b5 | 90 | |
atommota | 2:d4b810a888b5 | 91 | return rx; |
atommota | 2:d4b810a888b5 | 92 | |
atommota | 2:d4b810a888b5 | 93 | } |
atommota | 2:d4b810a888b5 | 94 | |
atommota | 2:d4b810a888b5 | 95 | |
atommota | 4:c3df518a938e | 96 | void LIS331:setFullScaleRange8g(void){ // Does not preserve rest of CTRL_REG_4! |
atommota | 4:c3df518a938e | 97 | |
atommota | 4:c3df518a938e | 98 | char tx[2]; |
atommota | 4:c3df518a938e | 99 | tx[0] = CTRL_REG_4; |
atommota | 4:c3df518a938e | 100 | tx[1] = 0x30; |
atommota | 4:c3df518a938e | 101 | |
atommota | 4:c3df518a938e | 102 | i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 2); |
atommota | 4:c3df518a938e | 103 | |
atommota | 4:c3df518a938e | 104 | } |
atommota | 4:c3df518a938e | 105 | |
atommota | 4:c3df518a938e | 106 | void LIS331:setFullScaleRange4g(void){ // Does not preserve rest of CTRL_REG_4! |
atommota | 4:c3df518a938e | 107 | |
atommota | 4:c3df518a938e | 108 | char tx[2]; |
atommota | 4:c3df518a938e | 109 | tx[0] = CTRL_REG_4; |
atommota | 4:c3df518a938e | 110 | tx[1] = 0x10; |
atommota | 4:c3df518a938e | 111 | |
atommota | 4:c3df518a938e | 112 | i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 2); |
atommota | 4:c3df518a938e | 113 | |
atommota | 4:c3df518a938e | 114 | } |
atommota | 4:c3df518a938e | 115 | |
atommota | 4:c3df518a938e | 116 | |
atommota | 4:c3df518a938e | 117 | void LIS331:setFullScaleRange2g(void){ // Does not preserve rest of CTRL_REG_4! |
atommota | 4:c3df518a938e | 118 | |
atommota | 4:c3df518a938e | 119 | char tx[2]; |
atommota | 4:c3df518a938e | 120 | tx[0] = CTRL_REG_4; |
atommota | 4:c3df518a938e | 121 | tx[1] = 0x00; |
atommota | 4:c3df518a938e | 122 | |
atommota | 4:c3df518a938e | 123 | i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 2); |
atommota | 4:c3df518a938e | 124 | |
atommota | 4:c3df518a938e | 125 | } |
atommota | 4:c3df518a938e | 126 | |
atommota | 2:d4b810a888b5 | 127 | |
atommota | 2:d4b810a888b5 | 128 | char LIS331::getAccelStatus(void){ |
atommota | 2:d4b810a888b5 | 129 | |
atommota | 2:d4b810a888b5 | 130 | char tx = STATUS_REG; |
atommota | 2:d4b810a888b5 | 131 | char rx; |
atommota | 2:d4b810a888b5 | 132 | |
atommota | 2:d4b810a888b5 | 133 | i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1); |
atommota | 2:d4b810a888b5 | 134 | |
atommota | 2:d4b810a888b5 | 135 | i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1); |
atommota | 2:d4b810a888b5 | 136 | |
atommota | 2:d4b810a888b5 | 137 | return rx; |
atommota | 2:d4b810a888b5 | 138 | } |
atommota | 2:d4b810a888b5 | 139 | |
atommota | 2:d4b810a888b5 | 140 | |
atommota | 2:d4b810a888b5 | 141 | |
atommota | 2:d4b810a888b5 | 142 | int LIS331::getAccelX(void){ |
atommota | 2:d4b810a888b5 | 143 | |
atommota | 2:d4b810a888b5 | 144 | char tx = ACCEL_XOUT_H_REG; |
atommota | 2:d4b810a888b5 | 145 | char rx[2]; |
atommota | 2:d4b810a888b5 | 146 | |
atommota | 2:d4b810a888b5 | 147 | i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1); |
atommota | 2:d4b810a888b5 | 148 | |
atommota | 2:d4b810a888b5 | 149 | i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2); |
atommota | 2:d4b810a888b5 | 150 | |
atommota | 2:d4b810a888b5 | 151 | int16_t output = ((int) rx[0] << 8) | ((int) rx[1]); |
atommota | 2:d4b810a888b5 | 152 | |
atommota | 2:d4b810a888b5 | 153 | return output; |
atommota | 2:d4b810a888b5 | 154 | |
atommota | 2:d4b810a888b5 | 155 | } |
atommota | 2:d4b810a888b5 | 156 | |
atommota | 2:d4b810a888b5 | 157 | int LIS331::getAccelY(void){ |
atommota | 2:d4b810a888b5 | 158 | |
atommota | 2:d4b810a888b5 | 159 | char tx = ACCEL_YOUT_H_REG; |
atommota | 2:d4b810a888b5 | 160 | char rx[2]; |
atommota | 2:d4b810a888b5 | 161 | |
atommota | 2:d4b810a888b5 | 162 | i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1); |
atommota | 2:d4b810a888b5 | 163 | |
atommota | 2:d4b810a888b5 | 164 | i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2); |
atommota | 2:d4b810a888b5 | 165 | |
atommota | 2:d4b810a888b5 | 166 | int16_t output = ((int) rx[0] << 8) | ((int) rx[1]); |
atommota | 2:d4b810a888b5 | 167 | |
atommota | 2:d4b810a888b5 | 168 | return output; |
atommota | 2:d4b810a888b5 | 169 | |
atommota | 2:d4b810a888b5 | 170 | } |
atommota | 2:d4b810a888b5 | 171 | |
atommota | 2:d4b810a888b5 | 172 | int LIS331::getAccelZ(void){ |
atommota | 2:d4b810a888b5 | 173 | |
atommota | 2:d4b810a888b5 | 174 | char tx = ACCEL_ZOUT_H_REG; |
atommota | 2:d4b810a888b5 | 175 | char rx[2]; |
atommota | 2:d4b810a888b5 | 176 | |
atommota | 2:d4b810a888b5 | 177 | i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1); |
atommota | 2:d4b810a888b5 | 178 | |
atommota | 2:d4b810a888b5 | 179 | i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2); |
atommota | 2:d4b810a888b5 | 180 | |
atommota | 2:d4b810a888b5 | 181 | int16_t output = ((int) rx[0] << 8) | ((int) rx[1]); |
atommota | 2:d4b810a888b5 | 182 | |
atommota | 2:d4b810a888b5 | 183 | return output; |
atommota | 1:02c1f5bb1c90 | 184 | } |