Committer:
atommota
Date:
Thu Oct 27 19:43:20 2011 +0000
Revision:
1:3a46d098c175
Parent:
0:0c0be03e887d
Changed default output data rate to 100Hz (from 1000Hz) to reduce noise issues.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
atommota 0:0c0be03e887d 1 /**
atommota 0:0c0be03e887d 2 * @section LICENSE
atommota 0:0c0be03e887d 3 *
atommota 0:0c0be03e887d 4 *
atommota 0:0c0be03e887d 5 * @section DESCRIPTION
atommota 0:0c0be03e887d 6 *
atommota 0:0c0be03e887d 7 * LIS331HH triple axis, digital interface, accelerometer.
atommota 0:0c0be03e887d 8 * Based off Aaron Berk's ITG3200 Gyro Library
atommota 0:0c0be03e887d 9 *
atommota 0:0c0be03e887d 10 * Tim Marvin - July 2011
atommota 0:0c0be03e887d 11 * Datasheet:
atommota 0:0c0be03e887d 12 *
atommota 0:0c0be03e887d 13 * http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00213470.pdf
atommota 0:0c0be03e887d 14 */
atommota 0:0c0be03e887d 15
atommota 0:0c0be03e887d 16 /**
atommota 0:0c0be03e887d 17 * Includes
atommota 0:0c0be03e887d 18 */
atommota 0:0c0be03e887d 19
atommota 0:0c0be03e887d 20 #include "LIS331HH.h"
atommota 0:0c0be03e887d 21
atommota 0:0c0be03e887d 22 LIS331HH::LIS331HH(PinName sda, PinName scl) : i2c_(sda, scl) {
atommota 0:0c0be03e887d 23 // set default scaling factor
atommota 0:0c0be03e887d 24 scaling_factor = 5140.0;
atommota 0:0c0be03e887d 25
atommota 0:0c0be03e887d 26 //set default range to zero.
atommota 0:0c0be03e887d 27 //current_range = 0;
atommota 0:0c0be03e887d 28
atommota 0:0c0be03e887d 29 //400kHz, fast mode.
atommota 0:0c0be03e887d 30 i2c_.frequency(400000);
atommota 0:0c0be03e887d 31
atommota 0:0c0be03e887d 32
atommota 0:0c0be03e887d 33 //Power Up Device, Set Output data rate, Enable All 3 Axis
atommota 0:0c0be03e887d 34 //See datasheet for details.
atommota 0:0c0be03e887d 35 char tx[2];
atommota 0:0c0be03e887d 36 //char tx2[2];
atommota 0:0c0be03e887d 37 //char rx[1];
atommota 0:0c0be03e887d 38 tx[0] = CTRL_REG_1;
atommota 0:0c0be03e887d 39 //CTRL_REG_1 [00111111] / [0x3F] to power up, set output rate to 1000Hz, and enable all 3 axis.
atommota 1:3a46d098c175 40 //CTRL_REG_1 [00101111] / [0x2F] to power up, set output rate to 100Hz, and enable all 3 axis.
atommota 0:0c0be03e887d 41 //CTRL_REG_1 [ABCDEFGH] ABC=PowerMode, DE=OutputDataRate, F=Zenable, G=Yenable, H=XEnable
atommota 1:3a46d098c175 42 tx[1] = 0x2F;
atommota 0:0c0be03e887d 43 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 0:0c0be03e887d 44
atommota 0:0c0be03e887d 45
atommota 0:0c0be03e887d 46
atommota 0:0c0be03e887d 47 //set default scale of 4g's
atommota 0:0c0be03e887d 48 //scaling_factor = 8192.0;
atommota 0:0c0be03e887d 49 //current_range = 24;
atommota 0:0c0be03e887d 50
atommota 0:0c0be03e887d 51 tx[0] = CTRL_REG_4;
atommota 0:0c0be03e887d 52 tx[1] = 0x00;
atommota 0:0c0be03e887d 53
atommota 0:0c0be03e887d 54 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 0:0c0be03e887d 55
atommota 0:0c0be03e887d 56
atommota 0:0c0be03e887d 57
atommota 0:0c0be03e887d 58 }
atommota 0:0c0be03e887d 59
atommota 0:0c0be03e887d 60 char LIS331HH::getWhoAmI(void){
atommota 0:0c0be03e887d 61
atommota 0:0c0be03e887d 62 //WhoAmI Register address.
atommota 0:0c0be03e887d 63 char tx = WHO_AM_I_REG_LIS331;
atommota 0:0c0be03e887d 64 char rx;
atommota 0:0c0be03e887d 65
atommota 0:0c0be03e887d 66 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:0c0be03e887d 67
atommota 0:0c0be03e887d 68 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 0:0c0be03e887d 69
atommota 0:0c0be03e887d 70 return rx;
atommota 0:0c0be03e887d 71
atommota 0:0c0be03e887d 72 }
atommota 0:0c0be03e887d 73
atommota 0:0c0be03e887d 74
atommota 0:0c0be03e887d 75
atommota 0:0c0be03e887d 76
atommota 0:0c0be03e887d 77 void LIS331HH::setPowerMode(char power_mode){
atommota 0:0c0be03e887d 78 // Currently sets all 3 axis to enabled. Will be set to preserve existing status in future
atommota 0:0c0be03e887d 79 char tx[2];
atommota 0:0c0be03e887d 80 tx[0] = CTRL_REG_1;
atommota 0:0c0be03e887d 81 tx[1] = power_mode;
atommota 0:0c0be03e887d 82
atommota 0:0c0be03e887d 83 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 0:0c0be03e887d 84
atommota 0:0c0be03e887d 85 }
atommota 0:0c0be03e887d 86
atommota 0:0c0be03e887d 87 char LIS331HH::getPowerMode(void){
atommota 0:0c0be03e887d 88
atommota 0:0c0be03e887d 89 char tx = CTRL_REG_1;
atommota 0:0c0be03e887d 90 char rx;
atommota 0:0c0be03e887d 91
atommota 0:0c0be03e887d 92 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:0c0be03e887d 93
atommota 0:0c0be03e887d 94 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 0:0c0be03e887d 95
atommota 0:0c0be03e887d 96
atommota 0:0c0be03e887d 97 return rx;
atommota 0:0c0be03e887d 98
atommota 0:0c0be03e887d 99 }
atommota 0:0c0be03e887d 100
atommota 0:0c0be03e887d 101
atommota 0:0c0be03e887d 102
atommota 0:0c0be03e887d 103 char LIS331HH::getInterruptConfiguration(void){
atommota 0:0c0be03e887d 104
atommota 0:0c0be03e887d 105 char tx = CTRL_REG_3;
atommota 0:0c0be03e887d 106 char rx;
atommota 0:0c0be03e887d 107
atommota 0:0c0be03e887d 108 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:0c0be03e887d 109
atommota 0:0c0be03e887d 110 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 0:0c0be03e887d 111
atommota 0:0c0be03e887d 112 return rx;
atommota 0:0c0be03e887d 113
atommota 0:0c0be03e887d 114 }
atommota 0:0c0be03e887d 115
atommota 0:0c0be03e887d 116
atommota 0:0c0be03e887d 117 void LIS331HH::setFullScaleRange8g(void){ // Does not preserve rest of CTRL_REG_4!
atommota 0:0c0be03e887d 118 scaling_factor = 4096.0;
atommota 0:0c0be03e887d 119 current_range = 8;
atommota 0:0c0be03e887d 120
atommota 0:0c0be03e887d 121 char tx[2];
atommota 0:0c0be03e887d 122 tx[0] = CTRL_REG_4;
atommota 0:0c0be03e887d 123 tx[1] = 0x30;
atommota 0:0c0be03e887d 124
atommota 0:0c0be03e887d 125 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 0:0c0be03e887d 126
atommota 0:0c0be03e887d 127 }
atommota 0:0c0be03e887d 128
atommota 0:0c0be03e887d 129 void LIS331HH::setFullScaleRange4g(void){ // Does not preserve rest of CTRL_REG_4!
atommota 0:0c0be03e887d 130 scaling_factor = 8192.0;
atommota 0:0c0be03e887d 131 current_range = 4;
atommota 0:0c0be03e887d 132
atommota 0:0c0be03e887d 133 char tx[2];
atommota 0:0c0be03e887d 134 tx[0] = CTRL_REG_4;
atommota 0:0c0be03e887d 135 tx[1] = 0x10;
atommota 0:0c0be03e887d 136
atommota 0:0c0be03e887d 137 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 0:0c0be03e887d 138
atommota 0:0c0be03e887d 139 }
atommota 0:0c0be03e887d 140
atommota 0:0c0be03e887d 141
atommota 0:0c0be03e887d 142 void LIS331HH::setFullScaleRange2g(void){ // Does not preserve rest of CTRL_REG_4!
atommota 0:0c0be03e887d 143 scaling_factor = 16384.0;
atommota 0:0c0be03e887d 144 current_range = 2;
atommota 0:0c0be03e887d 145
atommota 0:0c0be03e887d 146 char tx[2];
atommota 0:0c0be03e887d 147 tx[0] = CTRL_REG_4;
atommota 0:0c0be03e887d 148 tx[1] = 0x00;
atommota 0:0c0be03e887d 149
atommota 0:0c0be03e887d 150 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 0:0c0be03e887d 151
atommota 0:0c0be03e887d 152 }
atommota 0:0c0be03e887d 153
atommota 0:0c0be03e887d 154
atommota 0:0c0be03e887d 155 char LIS331HH::getAccelStatus(void){
atommota 0:0c0be03e887d 156
atommota 0:0c0be03e887d 157 char tx = STATUS_REG;
atommota 0:0c0be03e887d 158 char rx;
atommota 0:0c0be03e887d 159
atommota 0:0c0be03e887d 160 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:0c0be03e887d 161
atommota 0:0c0be03e887d 162 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 0:0c0be03e887d 163
atommota 0:0c0be03e887d 164 return rx;
atommota 0:0c0be03e887d 165 }
atommota 0:0c0be03e887d 166
atommota 0:0c0be03e887d 167
atommota 0:0c0be03e887d 168
atommota 0:0c0be03e887d 169 float LIS331HH::getAccelX(void){
atommota 0:0c0be03e887d 170
atommota 0:0c0be03e887d 171 char tx = ACCEL_XOUT_H_REG;
atommota 0:0c0be03e887d 172 char rx[2];
atommota 0:0c0be03e887d 173
atommota 0:0c0be03e887d 174 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:0c0be03e887d 175
atommota 0:0c0be03e887d 176 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 0:0c0be03e887d 177
atommota 0:0c0be03e887d 178 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
atommota 0:0c0be03e887d 179
atommota 0:0c0be03e887d 180 return output/scaling_factor;
atommota 0:0c0be03e887d 181
atommota 0:0c0be03e887d 182 }
atommota 0:0c0be03e887d 183
atommota 0:0c0be03e887d 184 float LIS331HH::getAccelY(void){
atommota 0:0c0be03e887d 185
atommota 0:0c0be03e887d 186 char tx = ACCEL_YOUT_H_REG;
atommota 0:0c0be03e887d 187 char rx[2];
atommota 0:0c0be03e887d 188
atommota 0:0c0be03e887d 189 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:0c0be03e887d 190
atommota 0:0c0be03e887d 191 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 0:0c0be03e887d 192
atommota 0:0c0be03e887d 193 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
atommota 0:0c0be03e887d 194
atommota 0:0c0be03e887d 195 return output/scaling_factor;
atommota 0:0c0be03e887d 196
atommota 0:0c0be03e887d 197 }
atommota 0:0c0be03e887d 198
atommota 0:0c0be03e887d 199 float LIS331HH::getAccelZ(void){
atommota 0:0c0be03e887d 200
atommota 0:0c0be03e887d 201 char tx = ACCEL_ZOUT_H_REG;
atommota 0:0c0be03e887d 202 char rx[2];
atommota 0:0c0be03e887d 203
atommota 0:0c0be03e887d 204 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:0c0be03e887d 205
atommota 0:0c0be03e887d 206 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 0:0c0be03e887d 207
atommota 0:0c0be03e887d 208 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
atommota 0:0c0be03e887d 209
atommota 0:0c0be03e887d 210 return output/scaling_factor;
atommota 0:0c0be03e887d 211 }