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 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 6:53e92e7fc073 22 // set default scaling factor
atommota 6:53e92e7fc073 23 scaling_factor = 4096.0;
atommota 6:53e92e7fc073 24
atommota 6:53e92e7fc073 25 //set default range to zero.
atommota 6:53e92e7fc073 26 current_range = 0;
atommota 2:d4b810a888b5 27
atommota 2:d4b810a888b5 28 //400kHz, fast mode.
atommota 2:d4b810a888b5 29 i2c_.frequency(400000);
atommota 2:d4b810a888b5 30
atommota 2:d4b810a888b5 31
atommota 2:d4b810a888b5 32 //Power Up Device, Set Output data rate, Enable All 3 Axis
atommota 2:d4b810a888b5 33 //See datasheet for details.
atommota 2:d4b810a888b5 34 char tx[2];
atommota 2:d4b810a888b5 35 //char tx2[2];
atommota 2:d4b810a888b5 36 //char rx[1];
atommota 2:d4b810a888b5 37 tx[0] = CTRL_REG_1;
atommota 2:d4b810a888b5 38 //CTRL_REG_1 [00111111] / [0x3F] to power up, set output rate to 1000Hz, and enable all 3 axis.
atommota 2:d4b810a888b5 39 tx[1] = 0x3F;
atommota 2:d4b810a888b5 40 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 2:d4b810a888b5 41
atommota 6:53e92e7fc073 42
atommota 6:53e92e7fc073 43
atommota 6:53e92e7fc073 44 //set default scale of 4g's
atommota 6:53e92e7fc073 45 scaling_factor = 8192.0;
atommota 6:53e92e7fc073 46 current_range = 4;
atommota 6:53e92e7fc073 47
atommota 6:53e92e7fc073 48 tx[0] = CTRL_REG_4;
atommota 6:53e92e7fc073 49 tx[1] = 0x10;
atommota 6:53e92e7fc073 50
atommota 6:53e92e7fc073 51 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 6:53e92e7fc073 52
atommota 6:53e92e7fc073 53
atommota 6:53e92e7fc073 54
atommota 2:d4b810a888b5 55 }
atommota 2:d4b810a888b5 56
atommota 2:d4b810a888b5 57 char LIS331::getWhoAmI(void){
atommota 2:d4b810a888b5 58
atommota 2:d4b810a888b5 59 //WhoAmI Register address.
atommota 3:147d95b7a525 60 char tx = WHO_AM_I_REG_LIS331;
atommota 2:d4b810a888b5 61 char rx;
atommota 2:d4b810a888b5 62
atommota 2:d4b810a888b5 63 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 64
atommota 2:d4b810a888b5 65 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 2:d4b810a888b5 66
atommota 2:d4b810a888b5 67 return rx;
atommota 2:d4b810a888b5 68
atommota 2:d4b810a888b5 69 }
atommota 2:d4b810a888b5 70
atommota 2:d4b810a888b5 71
atommota 2:d4b810a888b5 72
atommota 2:d4b810a888b5 73
atommota 2:d4b810a888b5 74 void LIS331::setPowerMode(char power_mode){
atommota 2:d4b810a888b5 75 // Currently sets all 3 axis to enabled. Will be set to preserve existing status in future
atommota 2:d4b810a888b5 76 char tx[2];
atommota 2:d4b810a888b5 77 tx[0] = CTRL_REG_1;
atommota 2:d4b810a888b5 78 tx[1] = power_mode;
atommota 2:d4b810a888b5 79
atommota 2:d4b810a888b5 80 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 2:d4b810a888b5 81
atommota 2:d4b810a888b5 82 }
atommota 2:d4b810a888b5 83
atommota 2:d4b810a888b5 84 char LIS331::getPowerMode(void){
atommota 2:d4b810a888b5 85
atommota 2:d4b810a888b5 86 char tx = CTRL_REG_1;
atommota 2:d4b810a888b5 87 char rx;
atommota 2:d4b810a888b5 88
atommota 2:d4b810a888b5 89 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 90
atommota 2:d4b810a888b5 91 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 2:d4b810a888b5 92
atommota 2:d4b810a888b5 93
atommota 2:d4b810a888b5 94 return rx;
atommota 2:d4b810a888b5 95
atommota 2:d4b810a888b5 96 }
atommota 2:d4b810a888b5 97
atommota 2:d4b810a888b5 98
atommota 2:d4b810a888b5 99
atommota 2:d4b810a888b5 100 char LIS331::getInterruptConfiguration(void){
atommota 2:d4b810a888b5 101
atommota 2:d4b810a888b5 102 char tx = CTRL_REG_3;
atommota 2:d4b810a888b5 103 char rx;
atommota 2:d4b810a888b5 104
atommota 2:d4b810a888b5 105 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 106
atommota 2:d4b810a888b5 107 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 2:d4b810a888b5 108
atommota 2:d4b810a888b5 109 return rx;
atommota 2:d4b810a888b5 110
atommota 2:d4b810a888b5 111 }
atommota 2:d4b810a888b5 112
atommota 2:d4b810a888b5 113
atommota 10:dcd11c84305e 114 char LIS331::setFullScaleRange8g(void){
atommota 10:dcd11c84305e 115 // Set our scaling factor and G range variables
atommota 6:53e92e7fc073 116 scaling_factor = 4096.0;
atommota 6:53e92e7fc073 117 current_range = 8;
atommota 10:dcd11c84305e 118
atommota 10:dcd11c84305e 119 // Setup our tx and rx arrays
atommota 10:dcd11c84305e 120 char tx1;
atommota 10:dcd11c84305e 121 char tx2[2];
atommota 10:dcd11c84305e 122 char rx;
atommota 9:5af73355984f 123
atommota 10:dcd11c84305e 124 // Need to read current register value so we can preserve it
atommota 10:dcd11c84305e 125 // Set our device register address to Control Register 4 and read its contents
atommota 10:dcd11c84305e 126 tx1 = CTRL_REG_4;
atommota 10:dcd11c84305e 127 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx1, 1);
atommota 10:dcd11c84305e 128 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 4:c3df518a938e 129
atommota 10:dcd11c84305e 130 tx2[0] = CTRL_REG_4;
atommota 10:dcd11c84305e 131 rx |= 1 << 5; // set 6th bit
atommota 10:dcd11c84305e 132 rx |= 1 << 4; // set 5th bit
atommota 10:dcd11c84305e 133 tx2[1] = rx;
atommota 10:dcd11c84305e 134
atommota 10:dcd11c84305e 135 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx2, 2);
atommota 10:dcd11c84305e 136 return rx;
atommota 4:c3df518a938e 137 }
atommota 4:c3df518a938e 138
atommota 10:dcd11c84305e 139 char LIS331::setFullScaleRange4g(void){
atommota 6:53e92e7fc073 140 scaling_factor = 8192.0;
atommota 6:53e92e7fc073 141 current_range = 4;
atommota 6:53e92e7fc073 142
atommota 10:dcd11c84305e 143 char tx1;
atommota 10:dcd11c84305e 144 char tx2[2];
atommota 10:dcd11c84305e 145 char rx;
atommota 10:dcd11c84305e 146
atommota 10:dcd11c84305e 147 // Need to read current register value so we can preserve it
atommota 10:dcd11c84305e 148 // Set our device register address to Control Register 4 and read its contents
atommota 10:dcd11c84305e 149 tx1 = CTRL_REG_4;
atommota 10:dcd11c84305e 150 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx1, 1);
atommota 10:dcd11c84305e 151 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 10:dcd11c84305e 152 tx2[0] = CTRL_REG_4;
atommota 10:dcd11c84305e 153 rx &= ~(1 << 5); // Clear 6th bit
atommota 10:dcd11c84305e 154 rx |= 1 << 4; // set 5th bit
atommota 10:dcd11c84305e 155 tx2[1] = rx;
atommota 4:c3df518a938e 156
atommota 10:dcd11c84305e 157 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx2, 2);
atommota 10:dcd11c84305e 158 return rx;
atommota 4:c3df518a938e 159 }
atommota 4:c3df518a938e 160
atommota 4:c3df518a938e 161
atommota 10:dcd11c84305e 162 char LIS331::setFullScaleRange2g(void){
atommota 6:53e92e7fc073 163 scaling_factor = 16384.0;
atommota 6:53e92e7fc073 164 current_range = 2;
atommota 6:53e92e7fc073 165
atommota 10:dcd11c84305e 166 char tx1;
atommota 10:dcd11c84305e 167 char tx2[2];
atommota 10:dcd11c84305e 168 char rx;
atommota 10:dcd11c84305e 169
atommota 10:dcd11c84305e 170 // Need to read current register value so we can preserve it
atommota 10:dcd11c84305e 171 // Set our device register address to Control Register 4 and read its contents
atommota 10:dcd11c84305e 172 tx1 = CTRL_REG_4;
atommota 10:dcd11c84305e 173 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx1, 1);
atommota 10:dcd11c84305e 174 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 10:dcd11c84305e 175
atommota 10:dcd11c84305e 176 tx2[0] = CTRL_REG_4;
atommota 10:dcd11c84305e 177 rx &= ~(1 << 5); // Clear 6th bit
atommota 10:dcd11c84305e 178 rx &= ~(1 << 4); // clear 5th bit
atommota 10:dcd11c84305e 179 tx2[1] = rx;
atommota 4:c3df518a938e 180
atommota 10:dcd11c84305e 181 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx2, 2);
atommota 10:dcd11c84305e 182 return rx;
atommota 4:c3df518a938e 183 }
atommota 4:c3df518a938e 184
atommota 2:d4b810a888b5 185
atommota 2:d4b810a888b5 186 char LIS331::getAccelStatus(void){
atommota 2:d4b810a888b5 187
atommota 2:d4b810a888b5 188 char tx = STATUS_REG;
atommota 2:d4b810a888b5 189 char rx;
atommota 2:d4b810a888b5 190
atommota 2:d4b810a888b5 191 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 192
atommota 2:d4b810a888b5 193 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 2:d4b810a888b5 194
atommota 2:d4b810a888b5 195 return rx;
atommota 2:d4b810a888b5 196 }
atommota 2:d4b810a888b5 197
atommota 2:d4b810a888b5 198
atommota 2:d4b810a888b5 199
atommota 8:ff365f926a52 200 float LIS331::getAccelX(void){
atommota 2:d4b810a888b5 201
atommota 9:5af73355984f 202 char tx = ACCEL_XOUT_L_REG | 0x80;
atommota 2:d4b810a888b5 203 char rx[2];
atommota 2:d4b810a888b5 204
atommota 2:d4b810a888b5 205 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 206
atommota 2:d4b810a888b5 207 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 2:d4b810a888b5 208
atommota 10:dcd11c84305e 209 int16_t output = ((int) rx[1] << 8) | ((int) rx[0]);
atommota 2:d4b810a888b5 210
atommota 6:53e92e7fc073 211 return output/scaling_factor;
atommota 2:d4b810a888b5 212
atommota 2:d4b810a888b5 213 }
atommota 2:d4b810a888b5 214
atommota 8:ff365f926a52 215 float LIS331::getAccelY(void){
atommota 2:d4b810a888b5 216
atommota 9:5af73355984f 217 char tx = ACCEL_YOUT_L_REG | 0x80;
atommota 2:d4b810a888b5 218 char rx[2];
atommota 2:d4b810a888b5 219
atommota 2:d4b810a888b5 220 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 221
atommota 2:d4b810a888b5 222 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 2:d4b810a888b5 223
atommota 10:dcd11c84305e 224 int16_t output = ((int) rx[1] << 8) | ((int) rx[0]);
atommota 2:d4b810a888b5 225
atommota 6:53e92e7fc073 226 return output/scaling_factor;
atommota 2:d4b810a888b5 227
atommota 2:d4b810a888b5 228 }
atommota 2:d4b810a888b5 229
atommota 10:dcd11c84305e 230 float LIS331::getAccelZ(void){
atommota 2:d4b810a888b5 231
atommota 9:5af73355984f 232 char tx = ACCEL_ZOUT_L_REG | 0x80;
atommota 2:d4b810a888b5 233 char rx[2];
atommota 2:d4b810a888b5 234
atommota 2:d4b810a888b5 235 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 236
atommota 2:d4b810a888b5 237 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 2:d4b810a888b5 238
atommota 10:dcd11c84305e 239 int16_t output = ((int) rx[1] << 8) | ((int) rx[0]);
atommota 2:d4b810a888b5 240
atommota 6:53e92e7fc073 241 return output/scaling_factor;
atommota 9:5af73355984f 242
atommota 1:02c1f5bb1c90 243 }