Library to communicate with a ST LIS331DLH

Committer:
atommota
Date:
Wed Feb 16 21:06:30 2011 +0000
Revision:
6:53e92e7fc073
Parent:
5:3443fb9646bd
Child:
8:ff365f926a52
Changed output to engineering units that scale depending on range selected with provide range functions.

Default range is +/- 4g\s

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 5:3443fb9646bd 114 void LIS331::setFullScaleRange8g(void){ // Does not preserve rest of CTRL_REG_4!
atommota 6:53e92e7fc073 115 scaling_factor = 4096.0;
atommota 6:53e92e7fc073 116 current_range = 8;
atommota 4:c3df518a938e 117
atommota 4:c3df518a938e 118 char tx[2];
atommota 4:c3df518a938e 119 tx[0] = CTRL_REG_4;
atommota 4:c3df518a938e 120 tx[1] = 0x30;
atommota 4:c3df518a938e 121
atommota 5:3443fb9646bd 122 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 4:c3df518a938e 123
atommota 4:c3df518a938e 124 }
atommota 4:c3df518a938e 125
atommota 5:3443fb9646bd 126 void LIS331::setFullScaleRange4g(void){ // Does not preserve rest of CTRL_REG_4!
atommota 6:53e92e7fc073 127 scaling_factor = 8192.0;
atommota 6:53e92e7fc073 128 current_range = 4;
atommota 6:53e92e7fc073 129
atommota 4:c3df518a938e 130 char tx[2];
atommota 4:c3df518a938e 131 tx[0] = CTRL_REG_4;
atommota 4:c3df518a938e 132 tx[1] = 0x10;
atommota 4:c3df518a938e 133
atommota 5:3443fb9646bd 134 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 4:c3df518a938e 135
atommota 4:c3df518a938e 136 }
atommota 4:c3df518a938e 137
atommota 4:c3df518a938e 138
atommota 5:3443fb9646bd 139 void LIS331::setFullScaleRange2g(void){ // Does not preserve rest of CTRL_REG_4!
atommota 6:53e92e7fc073 140 scaling_factor = 16384.0;
atommota 6:53e92e7fc073 141 current_range = 2;
atommota 6:53e92e7fc073 142
atommota 4:c3df518a938e 143 char tx[2];
atommota 4:c3df518a938e 144 tx[0] = CTRL_REG_4;
atommota 4:c3df518a938e 145 tx[1] = 0x00;
atommota 4:c3df518a938e 146
atommota 5:3443fb9646bd 147 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 4:c3df518a938e 148
atommota 4:c3df518a938e 149 }
atommota 4:c3df518a938e 150
atommota 2:d4b810a888b5 151
atommota 2:d4b810a888b5 152 char LIS331::getAccelStatus(void){
atommota 2:d4b810a888b5 153
atommota 2:d4b810a888b5 154 char tx = STATUS_REG;
atommota 2:d4b810a888b5 155 char rx;
atommota 2:d4b810a888b5 156
atommota 2:d4b810a888b5 157 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 158
atommota 2:d4b810a888b5 159 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 2:d4b810a888b5 160
atommota 2:d4b810a888b5 161 return rx;
atommota 2:d4b810a888b5 162 }
atommota 2:d4b810a888b5 163
atommota 2:d4b810a888b5 164
atommota 2:d4b810a888b5 165
atommota 2:d4b810a888b5 166 int LIS331::getAccelX(void){
atommota 2:d4b810a888b5 167
atommota 2:d4b810a888b5 168 char tx = ACCEL_XOUT_H_REG;
atommota 2:d4b810a888b5 169 char rx[2];
atommota 2:d4b810a888b5 170
atommota 2:d4b810a888b5 171 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 172
atommota 2:d4b810a888b5 173 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 2:d4b810a888b5 174
atommota 2:d4b810a888b5 175 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
atommota 2:d4b810a888b5 176
atommota 6:53e92e7fc073 177 return output/scaling_factor;
atommota 2:d4b810a888b5 178
atommota 2:d4b810a888b5 179 }
atommota 2:d4b810a888b5 180
atommota 2:d4b810a888b5 181 int LIS331::getAccelY(void){
atommota 2:d4b810a888b5 182
atommota 2:d4b810a888b5 183 char tx = ACCEL_YOUT_H_REG;
atommota 2:d4b810a888b5 184 char rx[2];
atommota 2:d4b810a888b5 185
atommota 2:d4b810a888b5 186 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 187
atommota 2:d4b810a888b5 188 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 2:d4b810a888b5 189
atommota 2:d4b810a888b5 190 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
atommota 2:d4b810a888b5 191
atommota 6:53e92e7fc073 192 return output/scaling_factor;
atommota 2:d4b810a888b5 193
atommota 2:d4b810a888b5 194 }
atommota 2:d4b810a888b5 195
atommota 6:53e92e7fc073 196 float LIS331::getAccelZ(void){
atommota 2:d4b810a888b5 197
atommota 2:d4b810a888b5 198 char tx = ACCEL_ZOUT_H_REG;
atommota 2:d4b810a888b5 199 char rx[2];
atommota 2:d4b810a888b5 200
atommota 2:d4b810a888b5 201 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 202
atommota 2:d4b810a888b5 203 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 2:d4b810a888b5 204
atommota 2:d4b810a888b5 205 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
atommota 2:d4b810a888b5 206
atommota 6:53e92e7fc073 207 return output/scaling_factor;
atommota 1:02c1f5bb1c90 208 }