Committer:
atommota
Date:
Fri Jul 01 21:07:48 2011 +0000
Revision:
0:0c0be03e887d
Child:
1:3a46d098c175

        

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 0:0c0be03e887d 40 //CTRL_REG_1 [ABCDEFGH] ABC=PowerMode, DE=OutputDataRate, F=Zenable, G=Yenable, H=XEnable
atommota 0:0c0be03e887d 41 tx[1] = 0x3F;
atommota 0:0c0be03e887d 42 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 0:0c0be03e887d 43
atommota 0:0c0be03e887d 44
atommota 0:0c0be03e887d 45
atommota 0:0c0be03e887d 46 //set default scale of 4g's
atommota 0:0c0be03e887d 47 //scaling_factor = 8192.0;
atommota 0:0c0be03e887d 48 //current_range = 24;
atommota 0:0c0be03e887d 49
atommota 0:0c0be03e887d 50 tx[0] = CTRL_REG_4;
atommota 0:0c0be03e887d 51 tx[1] = 0x00;
atommota 0:0c0be03e887d 52
atommota 0:0c0be03e887d 53 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 0:0c0be03e887d 54
atommota 0:0c0be03e887d 55
atommota 0:0c0be03e887d 56
atommota 0:0c0be03e887d 57 }
atommota 0:0c0be03e887d 58
atommota 0:0c0be03e887d 59 char LIS331HH::getWhoAmI(void){
atommota 0:0c0be03e887d 60
atommota 0:0c0be03e887d 61 //WhoAmI Register address.
atommota 0:0c0be03e887d 62 char tx = WHO_AM_I_REG_LIS331;
atommota 0:0c0be03e887d 63 char rx;
atommota 0:0c0be03e887d 64
atommota 0:0c0be03e887d 65 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:0c0be03e887d 66
atommota 0:0c0be03e887d 67 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 0:0c0be03e887d 68
atommota 0:0c0be03e887d 69 return rx;
atommota 0:0c0be03e887d 70
atommota 0:0c0be03e887d 71 }
atommota 0:0c0be03e887d 72
atommota 0:0c0be03e887d 73
atommota 0:0c0be03e887d 74
atommota 0:0c0be03e887d 75
atommota 0:0c0be03e887d 76 void LIS331HH::setPowerMode(char power_mode){
atommota 0:0c0be03e887d 77 // Currently sets all 3 axis to enabled. Will be set to preserve existing status in future
atommota 0:0c0be03e887d 78 char tx[2];
atommota 0:0c0be03e887d 79 tx[0] = CTRL_REG_1;
atommota 0:0c0be03e887d 80 tx[1] = power_mode;
atommota 0:0c0be03e887d 81
atommota 0:0c0be03e887d 82 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 0:0c0be03e887d 83
atommota 0:0c0be03e887d 84 }
atommota 0:0c0be03e887d 85
atommota 0:0c0be03e887d 86 char LIS331HH::getPowerMode(void){
atommota 0:0c0be03e887d 87
atommota 0:0c0be03e887d 88 char tx = CTRL_REG_1;
atommota 0:0c0be03e887d 89 char rx;
atommota 0:0c0be03e887d 90
atommota 0:0c0be03e887d 91 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:0c0be03e887d 92
atommota 0:0c0be03e887d 93 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 0:0c0be03e887d 94
atommota 0:0c0be03e887d 95
atommota 0:0c0be03e887d 96 return rx;
atommota 0:0c0be03e887d 97
atommota 0:0c0be03e887d 98 }
atommota 0:0c0be03e887d 99
atommota 0:0c0be03e887d 100
atommota 0:0c0be03e887d 101
atommota 0:0c0be03e887d 102 char LIS331HH::getInterruptConfiguration(void){
atommota 0:0c0be03e887d 103
atommota 0:0c0be03e887d 104 char tx = CTRL_REG_3;
atommota 0:0c0be03e887d 105 char rx;
atommota 0:0c0be03e887d 106
atommota 0:0c0be03e887d 107 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:0c0be03e887d 108
atommota 0:0c0be03e887d 109 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 0:0c0be03e887d 110
atommota 0:0c0be03e887d 111 return rx;
atommota 0:0c0be03e887d 112
atommota 0:0c0be03e887d 113 }
atommota 0:0c0be03e887d 114
atommota 0:0c0be03e887d 115
atommota 0:0c0be03e887d 116 void LIS331HH::setFullScaleRange8g(void){ // Does not preserve rest of CTRL_REG_4!
atommota 0:0c0be03e887d 117 scaling_factor = 4096.0;
atommota 0:0c0be03e887d 118 current_range = 8;
atommota 0:0c0be03e887d 119
atommota 0:0c0be03e887d 120 char tx[2];
atommota 0:0c0be03e887d 121 tx[0] = CTRL_REG_4;
atommota 0:0c0be03e887d 122 tx[1] = 0x30;
atommota 0:0c0be03e887d 123
atommota 0:0c0be03e887d 124 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 0:0c0be03e887d 125
atommota 0:0c0be03e887d 126 }
atommota 0:0c0be03e887d 127
atommota 0:0c0be03e887d 128 void LIS331HH::setFullScaleRange4g(void){ // Does not preserve rest of CTRL_REG_4!
atommota 0:0c0be03e887d 129 scaling_factor = 8192.0;
atommota 0:0c0be03e887d 130 current_range = 4;
atommota 0:0c0be03e887d 131
atommota 0:0c0be03e887d 132 char tx[2];
atommota 0:0c0be03e887d 133 tx[0] = CTRL_REG_4;
atommota 0:0c0be03e887d 134 tx[1] = 0x10;
atommota 0:0c0be03e887d 135
atommota 0:0c0be03e887d 136 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 0:0c0be03e887d 137
atommota 0:0c0be03e887d 138 }
atommota 0:0c0be03e887d 139
atommota 0:0c0be03e887d 140
atommota 0:0c0be03e887d 141 void LIS331HH::setFullScaleRange2g(void){ // Does not preserve rest of CTRL_REG_4!
atommota 0:0c0be03e887d 142 scaling_factor = 16384.0;
atommota 0:0c0be03e887d 143 current_range = 2;
atommota 0:0c0be03e887d 144
atommota 0:0c0be03e887d 145 char tx[2];
atommota 0:0c0be03e887d 146 tx[0] = CTRL_REG_4;
atommota 0:0c0be03e887d 147 tx[1] = 0x00;
atommota 0:0c0be03e887d 148
atommota 0:0c0be03e887d 149 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 0:0c0be03e887d 150
atommota 0:0c0be03e887d 151 }
atommota 0:0c0be03e887d 152
atommota 0:0c0be03e887d 153
atommota 0:0c0be03e887d 154 char LIS331HH::getAccelStatus(void){
atommota 0:0c0be03e887d 155
atommota 0:0c0be03e887d 156 char tx = STATUS_REG;
atommota 0:0c0be03e887d 157 char rx;
atommota 0:0c0be03e887d 158
atommota 0:0c0be03e887d 159 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:0c0be03e887d 160
atommota 0:0c0be03e887d 161 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 0:0c0be03e887d 162
atommota 0:0c0be03e887d 163 return rx;
atommota 0:0c0be03e887d 164 }
atommota 0:0c0be03e887d 165
atommota 0:0c0be03e887d 166
atommota 0:0c0be03e887d 167
atommota 0:0c0be03e887d 168 float LIS331HH::getAccelX(void){
atommota 0:0c0be03e887d 169
atommota 0:0c0be03e887d 170 char tx = ACCEL_XOUT_H_REG;
atommota 0:0c0be03e887d 171 char rx[2];
atommota 0:0c0be03e887d 172
atommota 0:0c0be03e887d 173 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:0c0be03e887d 174
atommota 0:0c0be03e887d 175 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 0:0c0be03e887d 176
atommota 0:0c0be03e887d 177 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
atommota 0:0c0be03e887d 178
atommota 0:0c0be03e887d 179 return output/scaling_factor;
atommota 0:0c0be03e887d 180
atommota 0:0c0be03e887d 181 }
atommota 0:0c0be03e887d 182
atommota 0:0c0be03e887d 183 float LIS331HH::getAccelY(void){
atommota 0:0c0be03e887d 184
atommota 0:0c0be03e887d 185 char tx = ACCEL_YOUT_H_REG;
atommota 0:0c0be03e887d 186 char rx[2];
atommota 0:0c0be03e887d 187
atommota 0:0c0be03e887d 188 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:0c0be03e887d 189
atommota 0:0c0be03e887d 190 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 0:0c0be03e887d 191
atommota 0:0c0be03e887d 192 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
atommota 0:0c0be03e887d 193
atommota 0:0c0be03e887d 194 return output/scaling_factor;
atommota 0:0c0be03e887d 195
atommota 0:0c0be03e887d 196 }
atommota 0:0c0be03e887d 197
atommota 0:0c0be03e887d 198 float LIS331HH::getAccelZ(void){
atommota 0:0c0be03e887d 199
atommota 0:0c0be03e887d 200 char tx = ACCEL_ZOUT_H_REG;
atommota 0:0c0be03e887d 201 char rx[2];
atommota 0:0c0be03e887d 202
atommota 0:0c0be03e887d 203 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 0:0c0be03e887d 204
atommota 0:0c0be03e887d 205 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 0:0c0be03e887d 206
atommota 0:0c0be03e887d 207 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
atommota 0:0c0be03e887d 208
atommota 0:0c0be03e887d 209 return output/scaling_factor;
atommota 0:0c0be03e887d 210 }