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 /**
atommota 0:d835d6cac146 16 * Includes
atommota 0:d835d6cac146 17 */
atommota 0:d835d6cac146 18 #include "LIS331.h"
atommota 0:d835d6cac146 19
atommota 0:d835d6cac146 20 LIS331::LIS331(PinName sda, PinName scl) : i2c_(sda, scl) {
atommota 0:d835d6cac146 21
atommota 0:d835d6cac146 22 //400kHz, fast mode.
atommota 0:d835d6cac146 23 i2c_.frequency(400000);
atommota 0:d835d6cac146 24
atommota 0:d835d6cac146 25
atommota 0:d835d6cac146 26 //Power Up Device, Set Output data rate, Enable All 3 Axis
atommota 0:d835d6cac146 27 //See datasheet for details.
atommota 0:d835d6cac146 28 char tx[2];
atommota 0:d835d6cac146 29 tx[0] = CTRL_REG_1;
atommota 0:d835d6cac146 30 //CTRL_REG_1 [00111111] / [0x3F] to power up, set output rate to 1000Hz, and enable all 3 axis.
atommota 0:d835d6cac146 31 tx[1] = 0x3F;
atommota 0:d835d6cac146 32
atommota 0:d835d6cac146 33 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 0:d835d6cac146 34
atommota 0:d835d6cac146 35 }
atommota 0:d835d6cac146 36
atommota 0:d835d6cac146 37 char LIS331::getWhoAmI(void){
atommota 0:d835d6cac146 38
atommota 0:d835d6cac146 39 //WhoAmI Register address.
atommota 0:d835d6cac146 40 char tx = WHO_AM_I_REG;
atommota 0:d835d6cac146 41 char rx;
atommota 0:d835d6cac146 42
atommota 0:d835d6cac146 43 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:d835d6cac146 44
atommota 0:d835d6cac146 45 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 0:d835d6cac146 46
atommota 0:d835d6cac146 47 return rx;
atommota 0:d835d6cac146 48
atommota 0:d835d6cac146 49 }
atommota 0:d835d6cac146 50
atommota 0:d835d6cac146 51
atommota 0:d835d6cac146 52 /* Needs to be implemented
atommota 0:d835d6cac146 53 char LIS331::getPowerMode(void){
atommota 0:d835d6cac146 54
atommota 0:d835d6cac146 55 char tx = CTRL_REG_1;
atommota 0:d835d6cac146 56 char rx;
atommota 0:d835d6cac146 57
atommota 0:d835d6cac146 58 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:d835d6cac146 59
atommota 0:d835d6cac146 60 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 0:d835d6cac146 61
atommota 0:d835d6cac146 62
atommota 0:d835d6cac146 63 return rx;
atommota 0:d835d6cac146 64
atommota 0:d835d6cac146 65 }
atommota 0:d835d6cac146 66
atommota 0:d835d6cac146 67 */
atommota 0:d835d6cac146 68
atommota 0:d835d6cac146 69
atommota 0:d835d6cac146 70
atommota 0:d835d6cac146 71 void LIS331::setPowerMode(char power_mode){
atommota 0:d835d6cac146 72 // Currently sets all 3 axis to enabled. Will be set to preserve existing status in future
atommota 0:d835d6cac146 73 char tx[2];
atommota 0:d835d6cac146 74 tx[0] = CTRL_REG_1;
atommota 0:d835d6cac146 75 tx[1] = power_mode;
atommota 0:d835d6cac146 76
atommota 0:d835d6cac146 77 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 0:d835d6cac146 78
atommota 0:d835d6cac146 79 }
atommota 0:d835d6cac146 80
atommota 0:d835d6cac146 81
atommota 0:d835d6cac146 82 /*
atommota 0:d835d6cac146 83 int LIS331::getInternalSampleRate(void){
atommota 0:d835d6cac146 84
atommota 0:d835d6cac146 85 char tx = DLPF_FS_REG;
atommota 0:d835d6cac146 86 char rx;
atommota 0:d835d6cac146 87
atommota 0:d835d6cac146 88 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:d835d6cac146 89
atommota 0:d835d6cac146 90 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 0:d835d6cac146 91
atommota 0:d835d6cac146 92 //DLPF_CFG == 0 -> sample rate = 8kHz.
atommota 0:d835d6cac146 93 if(rx == 0){
atommota 0:d835d6cac146 94 return 8;
atommota 0:d835d6cac146 95 }
atommota 0:d835d6cac146 96 //DLPF_CFG = 1..7 -> sample rate = 1kHz.
atommota 0:d835d6cac146 97 else if(rx >= 1 && rx <= 7){
atommota 0:d835d6cac146 98 return 1;
atommota 0:d835d6cac146 99 }
atommota 0:d835d6cac146 100 //DLPF_CFG = anything else -> something's wrong!
atommota 0:d835d6cac146 101 else{
atommota 0:d835d6cac146 102 return -1;
atommota 0:d835d6cac146 103 }
atommota 0:d835d6cac146 104
atommota 0:d835d6cac146 105 }
atommota 0:d835d6cac146 106
atommota 0:d835d6cac146 107
atommota 0:d835d6cac146 108 */
atommota 0:d835d6cac146 109
atommota 0:d835d6cac146 110
atommota 0:d835d6cac146 111
atommota 0:d835d6cac146 112 char LIS331::getInterruptConfiguration(void){
atommota 0:d835d6cac146 113
atommota 0:d835d6cac146 114 char tx = CTRL_REG_3;
atommota 0:d835d6cac146 115 char rx;
atommota 0:d835d6cac146 116
atommota 0:d835d6cac146 117 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:d835d6cac146 118
atommota 0:d835d6cac146 119 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 0:d835d6cac146 120
atommota 0:d835d6cac146 121 return rx;
atommota 0:d835d6cac146 122
atommota 0:d835d6cac146 123 }
atommota 0:d835d6cac146 124
atommota 0:d835d6cac146 125
atommota 0:d835d6cac146 126
atommota 0:d835d6cac146 127
atommota 0:d835d6cac146 128
atommota 0:d835d6cac146 129 /*
atommota 0:d835d6cac146 130 void LIS331::setInterruptConfiguration(char config){
atommota 0:d835d6cac146 131
atommota 0:d835d6cac146 132 char tx[2];
atommota 0:d835d6cac146 133 tx[0] = INT_CFG_REG;
atommota 0:d835d6cac146 134 tx[1] = config;
atommota 0:d835d6cac146 135
atommota 0:d835d6cac146 136 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 0:d835d6cac146 137
atommota 0:d835d6cac146 138 }
atommota 0:d835d6cac146 139
atommota 0:d835d6cac146 140 */
atommota 0:d835d6cac146 141
atommota 0:d835d6cac146 142
atommota 0:d835d6cac146 143
atommota 0:d835d6cac146 144 /*
atommota 0:d835d6cac146 145 bool LIS331::isPllReady(void){
atommota 0:d835d6cac146 146
atommota 0:d835d6cac146 147 char tx = INT_STATUS;
atommota 0:d835d6cac146 148 char rx;
atommota 0:d835d6cac146 149
atommota 0:d835d6cac146 150 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:d835d6cac146 151
atommota 0:d835d6cac146 152 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 0:d835d6cac146 153
atommota 0:d835d6cac146 154 //ITG_RDY bit is bit 4 of INT_STATUS register.
atommota 0:d835d6cac146 155 if(rx & 0x04){
atommota 0:d835d6cac146 156 return true;
atommota 0:d835d6cac146 157 }
atommota 0:d835d6cac146 158 else{
atommota 0:d835d6cac146 159 return false;
atommota 0:d835d6cac146 160 }
atommota 0:d835d6cac146 161
atommota 0:d835d6cac146 162 }
atommota 0:d835d6cac146 163
atommota 0:d835d6cac146 164 */
atommota 0:d835d6cac146 165
atommota 0:d835d6cac146 166
atommota 0:d835d6cac146 167
atommota 0:d835d6cac146 168 char LIS331::getAccelStatus(void){
atommota 0:d835d6cac146 169
atommota 0:d835d6cac146 170 char tx = STATSUS_REG;
atommota 0:d835d6cac146 171 char rx;
atommota 0:d835d6cac146 172
atommota 0:d835d6cac146 173 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:d835d6cac146 174
atommota 0:d835d6cac146 175 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 0:d835d6cac146 176
atommota 0:d835d6cac146 177 return rx;
atommota 0:d835d6cac146 178 }
atommota 0:d835d6cac146 179
atommota 0:d835d6cac146 180 }
atommota 0:d835d6cac146 181
atommota 0:d835d6cac146 182
atommota 0:d835d6cac146 183 int LIS331::getAccelX(void){
atommota 0:d835d6cac146 184
atommota 0:d835d6cac146 185 char tx = ACCEL_XOUT_H_REG;
atommota 0:d835d6cac146 186 char rx[2];
atommota 0:d835d6cac146 187
atommota 0:d835d6cac146 188 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:d835d6cac146 189
atommota 0:d835d6cac146 190 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 0:d835d6cac146 191
atommota 0:d835d6cac146 192 int16_t output = ((int) rx[1] << 8) | ((int) rx[0]);
atommota 0:d835d6cac146 193
atommota 0:d835d6cac146 194 return output;
atommota 0:d835d6cac146 195
atommota 0:d835d6cac146 196 }
atommota 0:d835d6cac146 197
atommota 0:d835d6cac146 198 int LIS331::getAccelY(void){
atommota 0:d835d6cac146 199
atommota 0:d835d6cac146 200 char tx = ACCEL_YOUT_H_REG;
atommota 0:d835d6cac146 201 char rx[2];
atommota 0:d835d6cac146 202
atommota 0:d835d6cac146 203 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:d835d6cac146 204
atommota 0:d835d6cac146 205 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 0:d835d6cac146 206
atommota 0:d835d6cac146 207 int16_t output = ((int) rx[1] << 8) | ((int) rx[0]);
atommota 0:d835d6cac146 208
atommota 0:d835d6cac146 209 return output;
atommota 0:d835d6cac146 210
atommota 0:d835d6cac146 211 }
atommota 0:d835d6cac146 212
atommota 0:d835d6cac146 213 int LIS331::getAccelZ(void){
atommota 0:d835d6cac146 214
atommota 0:d835d6cac146 215 char tx = ACCEL_ZOUT_H_REG;
atommota 0:d835d6cac146 216 char rx[2];
atommota 0:d835d6cac146 217
atommota 0:d835d6cac146 218 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:d835d6cac146 219
atommota 0:d835d6cac146 220 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 0:d835d6cac146 221
atommota 0:d835d6cac146 222 int16_t output = ((int) rx[1] << 8) | ((int) rx[0]);
atommota 0:d835d6cac146 223
atommota 0:d835d6cac146 224 return output;
atommota 0:d835d6cac146 225 }
atommota 0:d835d6cac146 226
atommota 0:d835d6cac146 227