hige dura
/
ITG3200_I2C
Embed:
(wiki syntax)
Show/hide line numbers
ITG3200.cpp
00001 /** 00002 * @author Aaron Berk 00003 * 00004 * @section LICENSE 00005 * 00006 * Copyright (c) 2010 ARM Limited 00007 * 00008 * Permission is hereby granted, free of charge, to any person obtaining a copy 00009 * of this software and associated documentation files (the "Software"), to deal 00010 * in the Software without restriction, including without limitation the rights 00011 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00012 * copies of the Software, and to permit persons to whom the Software is 00013 * furnished to do so, subject to the following conditions: 00014 * 00015 * The above copyright notice and this permission notice shall be included in 00016 * all copies or substantial portions of the Software. 00017 * 00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00023 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00024 * THE SOFTWARE. 00025 * 00026 * @section DESCRIPTION 00027 * 00028 * ITG-3200 triple axis, digital interface, gyroscope. 00029 * 00030 * Datasheet: 00031 * 00032 * http://invensense.com/mems/gyro/documents/PS-ITG-3200-00-01.4.pdf 00033 */ 00034 00035 /** 00036 * Includes 00037 */ 00038 #include "ITG3200.h" 00039 00040 ITG3200::ITG3200(PinName sda, PinName scl) : i2c_(sda, scl) { 00041 00042 //400kHz, fast mode. 00043 i2c_.frequency(400000); 00044 00045 //Set FS_SEL to 0x03 for proper operation. 00046 //See datasheet for details. 00047 char tx[2]; 00048 tx[0] = DLPF_FS_REG; 00049 //FS_SEL bits sit in bits 4 and 3 of DLPF_FS register. 00050 tx[1] = 0x03 << 3; 00051 00052 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2); 00053 00054 } 00055 00056 char ITG3200::getWhoAmI(void){ 00057 00058 //WhoAmI Register address. 00059 char tx = WHO_AM_I_REG; 00060 char rx; 00061 00062 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00063 00064 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1); 00065 00066 return rx; 00067 00068 } 00069 00070 void ITG3200::setWhoAmI(char address){ 00071 00072 char tx[2]; 00073 tx[0] = WHO_AM_I_REG; 00074 tx[1] = address; 00075 00076 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2); 00077 00078 } 00079 00080 char ITG3200::getSampleRateDivider(void){ 00081 00082 char tx = SMPLRT_DIV_REG; 00083 char rx; 00084 00085 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00086 00087 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1); 00088 00089 return rx; 00090 00091 } 00092 00093 void ITG3200::setSampleRateDivider(char divider){ 00094 00095 char tx[2]; 00096 tx[0] = SMPLRT_DIV_REG; 00097 tx[1] = divider; 00098 00099 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2); 00100 00101 } 00102 00103 int ITG3200::getInternalSampleRate(void){ 00104 00105 char tx = DLPF_FS_REG; 00106 char rx; 00107 00108 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00109 00110 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1); 00111 00112 //DLPF_CFG == 0 -> sample rate = 8kHz. 00113 if(rx == 0){ 00114 return 8; 00115 } 00116 //DLPF_CFG = 1..7 -> sample rate = 1kHz. 00117 else if(rx >= 1 && rx <= 7){ 00118 return 1; 00119 } 00120 //DLPF_CFG = anything else -> something's wrong! 00121 else{ 00122 return -1; 00123 } 00124 00125 } 00126 00127 void ITG3200::setLpBandwidth(char bandwidth){ 00128 00129 char tx[2]; 00130 tx[0] = DLPF_FS_REG; 00131 //Bits 4,3 are required to be 0x03 for proper operation. 00132 tx[1] = bandwidth | (0x03 << 3); 00133 00134 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2); 00135 00136 } 00137 00138 char ITG3200::getInterruptConfiguration(void){ 00139 00140 char tx = INT_CFG_REG; 00141 char rx; 00142 00143 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00144 00145 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1); 00146 00147 return rx; 00148 00149 } 00150 00151 void ITG3200::setInterruptConfiguration(char config){ 00152 00153 char tx[2]; 00154 tx[0] = INT_CFG_REG; 00155 tx[1] = config; 00156 00157 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2); 00158 00159 } 00160 00161 bool ITG3200::isPllReady(void){ 00162 00163 char tx = INT_STATUS; 00164 char rx; 00165 00166 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00167 00168 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1); 00169 00170 //ITG_RDY bit is bit 4 of INT_STATUS register. 00171 if(rx & 0x04){ 00172 return true; 00173 } 00174 else{ 00175 return false; 00176 } 00177 00178 } 00179 00180 bool ITG3200::isRawDataReady(void){ 00181 00182 char tx = INT_STATUS; 00183 char rx; 00184 00185 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00186 00187 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1); 00188 00189 //RAW_DATA_RDY bit is bit 1 of INT_STATUS register. 00190 if(rx & 0x01){ 00191 return true; 00192 } 00193 else{ 00194 return false; 00195 } 00196 00197 } 00198 00199 float ITG3200::getTemperature(void){ 00200 00201 char tx = TEMP_OUT_H_REG; 00202 char rx[2]; 00203 00204 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00205 00206 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, rx, 2); 00207 00208 int16_t temperature = ((int) rx[0] << 8) | ((int) rx[1]); 00209 //Offset = -35 degrees, 13200 counts. 280 counts/degrees C. 00210 return 35.0 + ((temperature + 13200)/280.0); 00211 00212 } 00213 00214 int ITG3200::getGyroX(void){ 00215 00216 char tx = GYRO_XOUT_H_REG; 00217 char rx[2]; 00218 00219 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00220 00221 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, rx, 2); 00222 00223 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]); 00224 00225 return output; 00226 00227 } 00228 00229 int ITG3200::getGyroY(void){ 00230 00231 char tx = GYRO_YOUT_H_REG; 00232 char rx[2]; 00233 00234 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00235 00236 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, rx, 2); 00237 00238 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]); 00239 00240 return output; 00241 00242 } 00243 00244 int ITG3200::getGyroZ(void){ 00245 00246 char tx = GYRO_ZOUT_H_REG; 00247 char rx[2]; 00248 00249 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00250 00251 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, rx, 2); 00252 00253 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]); 00254 00255 return output; 00256 00257 } 00258 00259 char ITG3200::getPowerManagement(void){ 00260 00261 char tx = PWR_MGM_REG; 00262 char rx; 00263 00264 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00265 00266 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1); 00267 00268 return rx; 00269 00270 } 00271 00272 void ITG3200::setPowerManagement(char config){ 00273 00274 char tx[2]; 00275 tx[0] = PWR_MGM_REG; 00276 tx[1] = config; 00277 00278 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2); 00279 00280 }
Generated on Wed Jul 20 2022 02:11:10 by 1.7.2