WITGyro

Dependencies:   Terminal mbed

Committer:
marcuss421
Date:
Mon Nov 11 21:00:18 2013 +0000
Revision:
0:0d844f8ecb72
Matt K

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcuss421 0:0d844f8ecb72 1 /**
marcuss421 0:0d844f8ecb72 2 * @author Aaron Berk
marcuss421 0:0d844f8ecb72 3 *
marcuss421 0:0d844f8ecb72 4 * @section LICENSE
marcuss421 0:0d844f8ecb72 5 *
marcuss421 0:0d844f8ecb72 6 * Copyright (c) 2010 ARM Limited
marcuss421 0:0d844f8ecb72 7 *
marcuss421 0:0d844f8ecb72 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
marcuss421 0:0d844f8ecb72 9 * of this software and associated documentation files (the "Software"), to deal
marcuss421 0:0d844f8ecb72 10 * in the Software without restriction, including without limitation the rights
marcuss421 0:0d844f8ecb72 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
marcuss421 0:0d844f8ecb72 12 * copies of the Software, and to permit persons to whom the Software is
marcuss421 0:0d844f8ecb72 13 * furnished to do so, subject to the following conditions:
marcuss421 0:0d844f8ecb72 14 *
marcuss421 0:0d844f8ecb72 15 * The above copyright notice and this permission notice shall be included in
marcuss421 0:0d844f8ecb72 16 * all copies or substantial portions of the Software.
marcuss421 0:0d844f8ecb72 17 *
marcuss421 0:0d844f8ecb72 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
marcuss421 0:0d844f8ecb72 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
marcuss421 0:0d844f8ecb72 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
marcuss421 0:0d844f8ecb72 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
marcuss421 0:0d844f8ecb72 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
marcuss421 0:0d844f8ecb72 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
marcuss421 0:0d844f8ecb72 24 * THE SOFTWARE.
marcuss421 0:0d844f8ecb72 25 *
marcuss421 0:0d844f8ecb72 26 * @section DESCRIPTION
marcuss421 0:0d844f8ecb72 27 *
marcuss421 0:0d844f8ecb72 28 * ITG-3200 triple axis, digital interface, gyroscope.
marcuss421 0:0d844f8ecb72 29 *
marcuss421 0:0d844f8ecb72 30 * Datasheet:
marcuss421 0:0d844f8ecb72 31 *
marcuss421 0:0d844f8ecb72 32 * http://invensense.com/mems/gyro/documents/PS-ITG-3200-00-01.4.pdf
marcuss421 0:0d844f8ecb72 33 */
marcuss421 0:0d844f8ecb72 34
marcuss421 0:0d844f8ecb72 35 /**
marcuss421 0:0d844f8ecb72 36 * Includes
marcuss421 0:0d844f8ecb72 37 */
marcuss421 0:0d844f8ecb72 38 #include "ITG3200.h"
marcuss421 0:0d844f8ecb72 39
marcuss421 0:0d844f8ecb72 40 ITG3200::ITG3200(PinName sda, PinName scl) : i2c_(sda, scl) {
marcuss421 0:0d844f8ecb72 41
marcuss421 0:0d844f8ecb72 42 //400kHz, fast mode.
marcuss421 0:0d844f8ecb72 43 i2c_.frequency(400000);
marcuss421 0:0d844f8ecb72 44
marcuss421 0:0d844f8ecb72 45 //Set FS_SEL to 0x03 for proper operation.
marcuss421 0:0d844f8ecb72 46 //See datasheet for details.
marcuss421 0:0d844f8ecb72 47 char tx[2];
marcuss421 0:0d844f8ecb72 48 tx[0] = DLPF_FS_REG;
marcuss421 0:0d844f8ecb72 49 //FS_SEL bits sit in bits 4 and 3 of DLPF_FS register.
marcuss421 0:0d844f8ecb72 50 tx[1] = 0x03 << 3;
marcuss421 0:0d844f8ecb72 51
marcuss421 0:0d844f8ecb72 52 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
marcuss421 0:0d844f8ecb72 53
marcuss421 0:0d844f8ecb72 54 }
marcuss421 0:0d844f8ecb72 55
marcuss421 0:0d844f8ecb72 56 char ITG3200::getWhoAmI(void){
marcuss421 0:0d844f8ecb72 57
marcuss421 0:0d844f8ecb72 58 //WhoAmI Register address.
marcuss421 0:0d844f8ecb72 59 char tx = WHO_AM_I_REG;
marcuss421 0:0d844f8ecb72 60 char rx;
marcuss421 0:0d844f8ecb72 61
marcuss421 0:0d844f8ecb72 62 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
marcuss421 0:0d844f8ecb72 63
marcuss421 0:0d844f8ecb72 64 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
marcuss421 0:0d844f8ecb72 65
marcuss421 0:0d844f8ecb72 66 return rx;
marcuss421 0:0d844f8ecb72 67
marcuss421 0:0d844f8ecb72 68 }
marcuss421 0:0d844f8ecb72 69
marcuss421 0:0d844f8ecb72 70 void ITG3200::setWhoAmI(char address){
marcuss421 0:0d844f8ecb72 71
marcuss421 0:0d844f8ecb72 72 char tx[2];
marcuss421 0:0d844f8ecb72 73 tx[0] = WHO_AM_I_REG;
marcuss421 0:0d844f8ecb72 74 tx[1] = address;
marcuss421 0:0d844f8ecb72 75
marcuss421 0:0d844f8ecb72 76 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
marcuss421 0:0d844f8ecb72 77
marcuss421 0:0d844f8ecb72 78 }
marcuss421 0:0d844f8ecb72 79
marcuss421 0:0d844f8ecb72 80 char ITG3200::getSampleRateDivider(void){
marcuss421 0:0d844f8ecb72 81
marcuss421 0:0d844f8ecb72 82 char tx = SMPLRT_DIV_REG;
marcuss421 0:0d844f8ecb72 83 char rx;
marcuss421 0:0d844f8ecb72 84
marcuss421 0:0d844f8ecb72 85 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
marcuss421 0:0d844f8ecb72 86
marcuss421 0:0d844f8ecb72 87 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
marcuss421 0:0d844f8ecb72 88
marcuss421 0:0d844f8ecb72 89 return rx;
marcuss421 0:0d844f8ecb72 90
marcuss421 0:0d844f8ecb72 91 }
marcuss421 0:0d844f8ecb72 92
marcuss421 0:0d844f8ecb72 93 void ITG3200::setSampleRateDivider(char divider){
marcuss421 0:0d844f8ecb72 94
marcuss421 0:0d844f8ecb72 95 char tx[2];
marcuss421 0:0d844f8ecb72 96 tx[0] = SMPLRT_DIV_REG;
marcuss421 0:0d844f8ecb72 97 tx[1] = divider;
marcuss421 0:0d844f8ecb72 98
marcuss421 0:0d844f8ecb72 99 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
marcuss421 0:0d844f8ecb72 100
marcuss421 0:0d844f8ecb72 101 }
marcuss421 0:0d844f8ecb72 102
marcuss421 0:0d844f8ecb72 103 int ITG3200::getInternalSampleRate(void){
marcuss421 0:0d844f8ecb72 104
marcuss421 0:0d844f8ecb72 105 char tx = DLPF_FS_REG;
marcuss421 0:0d844f8ecb72 106 char rx;
marcuss421 0:0d844f8ecb72 107
marcuss421 0:0d844f8ecb72 108 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
marcuss421 0:0d844f8ecb72 109
marcuss421 0:0d844f8ecb72 110 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
marcuss421 0:0d844f8ecb72 111
marcuss421 0:0d844f8ecb72 112 //DLPF_CFG == 0 -> sample rate = 8kHz.
marcuss421 0:0d844f8ecb72 113 if(rx == 0){
marcuss421 0:0d844f8ecb72 114 return 8;
marcuss421 0:0d844f8ecb72 115 }
marcuss421 0:0d844f8ecb72 116 //DLPF_CFG = 1..7 -> sample rate = 1kHz.
marcuss421 0:0d844f8ecb72 117 else if(rx >= 1 && rx <= 7){
marcuss421 0:0d844f8ecb72 118 return 1;
marcuss421 0:0d844f8ecb72 119 }
marcuss421 0:0d844f8ecb72 120 //DLPF_CFG = anything else -> something's wrong!
marcuss421 0:0d844f8ecb72 121 else{
marcuss421 0:0d844f8ecb72 122 return -1;
marcuss421 0:0d844f8ecb72 123 }
marcuss421 0:0d844f8ecb72 124
marcuss421 0:0d844f8ecb72 125 }
marcuss421 0:0d844f8ecb72 126
marcuss421 0:0d844f8ecb72 127 void ITG3200::setLpBandwidth(char bandwidth){
marcuss421 0:0d844f8ecb72 128
marcuss421 0:0d844f8ecb72 129 char tx[2];
marcuss421 0:0d844f8ecb72 130 tx[0] = DLPF_FS_REG;
marcuss421 0:0d844f8ecb72 131 //Bits 4,3 are required to be 0x03 for proper operation.
marcuss421 0:0d844f8ecb72 132 tx[1] = bandwidth | (0x03 << 3);
marcuss421 0:0d844f8ecb72 133
marcuss421 0:0d844f8ecb72 134 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
marcuss421 0:0d844f8ecb72 135
marcuss421 0:0d844f8ecb72 136 }
marcuss421 0:0d844f8ecb72 137
marcuss421 0:0d844f8ecb72 138 char ITG3200::getInterruptConfiguration(void){
marcuss421 0:0d844f8ecb72 139
marcuss421 0:0d844f8ecb72 140 char tx = INT_CFG_REG;
marcuss421 0:0d844f8ecb72 141 char rx;
marcuss421 0:0d844f8ecb72 142
marcuss421 0:0d844f8ecb72 143 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
marcuss421 0:0d844f8ecb72 144
marcuss421 0:0d844f8ecb72 145 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
marcuss421 0:0d844f8ecb72 146
marcuss421 0:0d844f8ecb72 147 return rx;
marcuss421 0:0d844f8ecb72 148
marcuss421 0:0d844f8ecb72 149 }
marcuss421 0:0d844f8ecb72 150
marcuss421 0:0d844f8ecb72 151 void ITG3200::setInterruptConfiguration(char config){
marcuss421 0:0d844f8ecb72 152
marcuss421 0:0d844f8ecb72 153 char tx[2];
marcuss421 0:0d844f8ecb72 154 tx[0] = INT_CFG_REG;
marcuss421 0:0d844f8ecb72 155 tx[1] = config;
marcuss421 0:0d844f8ecb72 156
marcuss421 0:0d844f8ecb72 157 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
marcuss421 0:0d844f8ecb72 158
marcuss421 0:0d844f8ecb72 159 }
marcuss421 0:0d844f8ecb72 160
marcuss421 0:0d844f8ecb72 161 bool ITG3200::isPllReady(void){
marcuss421 0:0d844f8ecb72 162
marcuss421 0:0d844f8ecb72 163 char tx = INT_STATUS;
marcuss421 0:0d844f8ecb72 164 char rx;
marcuss421 0:0d844f8ecb72 165
marcuss421 0:0d844f8ecb72 166 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
marcuss421 0:0d844f8ecb72 167
marcuss421 0:0d844f8ecb72 168 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
marcuss421 0:0d844f8ecb72 169
marcuss421 0:0d844f8ecb72 170 //ITG_RDY bit is bit 4 of INT_STATUS register.
marcuss421 0:0d844f8ecb72 171 if(rx & 0x04){
marcuss421 0:0d844f8ecb72 172 return true;
marcuss421 0:0d844f8ecb72 173 }
marcuss421 0:0d844f8ecb72 174 else{
marcuss421 0:0d844f8ecb72 175 return false;
marcuss421 0:0d844f8ecb72 176 }
marcuss421 0:0d844f8ecb72 177
marcuss421 0:0d844f8ecb72 178 }
marcuss421 0:0d844f8ecb72 179
marcuss421 0:0d844f8ecb72 180 bool ITG3200::isRawDataReady(void){
marcuss421 0:0d844f8ecb72 181
marcuss421 0:0d844f8ecb72 182 char tx = INT_STATUS;
marcuss421 0:0d844f8ecb72 183 char rx;
marcuss421 0:0d844f8ecb72 184
marcuss421 0:0d844f8ecb72 185 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
marcuss421 0:0d844f8ecb72 186
marcuss421 0:0d844f8ecb72 187 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
marcuss421 0:0d844f8ecb72 188
marcuss421 0:0d844f8ecb72 189 //RAW_DATA_RDY bit is bit 1 of INT_STATUS register.
marcuss421 0:0d844f8ecb72 190 if(rx & 0x01){
marcuss421 0:0d844f8ecb72 191 return true;
marcuss421 0:0d844f8ecb72 192 }
marcuss421 0:0d844f8ecb72 193 else{
marcuss421 0:0d844f8ecb72 194 return false;
marcuss421 0:0d844f8ecb72 195 }
marcuss421 0:0d844f8ecb72 196
marcuss421 0:0d844f8ecb72 197 }
marcuss421 0:0d844f8ecb72 198
marcuss421 0:0d844f8ecb72 199 float ITG3200::getTemperature(void){
marcuss421 0:0d844f8ecb72 200
marcuss421 0:0d844f8ecb72 201 char tx = TEMP_OUT_H_REG;
marcuss421 0:0d844f8ecb72 202 char rx[2];
marcuss421 0:0d844f8ecb72 203
marcuss421 0:0d844f8ecb72 204 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
marcuss421 0:0d844f8ecb72 205
marcuss421 0:0d844f8ecb72 206 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, rx, 2);
marcuss421 0:0d844f8ecb72 207
marcuss421 0:0d844f8ecb72 208 int16_t temperature = ((int) rx[0] << 8) | ((int) rx[1]);
marcuss421 0:0d844f8ecb72 209 //Offset = -35 degrees, 13200 counts. 280 counts/degrees C.
marcuss421 0:0d844f8ecb72 210 return 35.0 + ((temperature + 13200)/280.0);
marcuss421 0:0d844f8ecb72 211
marcuss421 0:0d844f8ecb72 212 }
marcuss421 0:0d844f8ecb72 213
marcuss421 0:0d844f8ecb72 214 int ITG3200::getGyroX(void){
marcuss421 0:0d844f8ecb72 215
marcuss421 0:0d844f8ecb72 216 char tx = GYRO_XOUT_H_REG;
marcuss421 0:0d844f8ecb72 217 char rx[2];
marcuss421 0:0d844f8ecb72 218
marcuss421 0:0d844f8ecb72 219 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
marcuss421 0:0d844f8ecb72 220
marcuss421 0:0d844f8ecb72 221 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, rx, 2);
marcuss421 0:0d844f8ecb72 222
marcuss421 0:0d844f8ecb72 223 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
marcuss421 0:0d844f8ecb72 224
marcuss421 0:0d844f8ecb72 225 return output;
marcuss421 0:0d844f8ecb72 226
marcuss421 0:0d844f8ecb72 227 }
marcuss421 0:0d844f8ecb72 228
marcuss421 0:0d844f8ecb72 229 int ITG3200::getGyroY(void){
marcuss421 0:0d844f8ecb72 230
marcuss421 0:0d844f8ecb72 231 char tx = GYRO_YOUT_H_REG;
marcuss421 0:0d844f8ecb72 232 char rx[2];
marcuss421 0:0d844f8ecb72 233
marcuss421 0:0d844f8ecb72 234 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
marcuss421 0:0d844f8ecb72 235
marcuss421 0:0d844f8ecb72 236 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, rx, 2);
marcuss421 0:0d844f8ecb72 237
marcuss421 0:0d844f8ecb72 238 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
marcuss421 0:0d844f8ecb72 239
marcuss421 0:0d844f8ecb72 240 return output;
marcuss421 0:0d844f8ecb72 241
marcuss421 0:0d844f8ecb72 242 }
marcuss421 0:0d844f8ecb72 243
marcuss421 0:0d844f8ecb72 244 int ITG3200::getGyroZ(void){
marcuss421 0:0d844f8ecb72 245
marcuss421 0:0d844f8ecb72 246 char tx = GYRO_ZOUT_H_REG;
marcuss421 0:0d844f8ecb72 247 char rx[2];
marcuss421 0:0d844f8ecb72 248
marcuss421 0:0d844f8ecb72 249 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
marcuss421 0:0d844f8ecb72 250
marcuss421 0:0d844f8ecb72 251 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, rx, 2);
marcuss421 0:0d844f8ecb72 252
marcuss421 0:0d844f8ecb72 253 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
marcuss421 0:0d844f8ecb72 254
marcuss421 0:0d844f8ecb72 255 return output;
marcuss421 0:0d844f8ecb72 256
marcuss421 0:0d844f8ecb72 257 }
marcuss421 0:0d844f8ecb72 258
marcuss421 0:0d844f8ecb72 259 char ITG3200::getPowerManagement(void){
marcuss421 0:0d844f8ecb72 260
marcuss421 0:0d844f8ecb72 261 char tx = PWR_MGM_REG;
marcuss421 0:0d844f8ecb72 262 char rx;
marcuss421 0:0d844f8ecb72 263
marcuss421 0:0d844f8ecb72 264 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
marcuss421 0:0d844f8ecb72 265
marcuss421 0:0d844f8ecb72 266 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
marcuss421 0:0d844f8ecb72 267
marcuss421 0:0d844f8ecb72 268 return rx;
marcuss421 0:0d844f8ecb72 269
marcuss421 0:0d844f8ecb72 270 }
marcuss421 0:0d844f8ecb72 271
marcuss421 0:0d844f8ecb72 272 void ITG3200::setPowerManagement(char config){
marcuss421 0:0d844f8ecb72 273
marcuss421 0:0d844f8ecb72 274 char tx[2];
marcuss421 0:0d844f8ecb72 275 tx[0] = PWR_MGM_REG;
marcuss421 0:0d844f8ecb72 276 tx[1] = config;
marcuss421 0:0d844f8ecb72 277
marcuss421 0:0d844f8ecb72 278 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
marcuss421 0:0d844f8ecb72 279
marcuss421 0:0d844f8ecb72 280 }