Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MPU9150.cpp
00001 // Check methods that write bit fields 00002 #include "MPU9150.h" 00003 #include "mbed.h" 00004 00005 MPU9150::MPU9150() : i2c_(I2C_SDA, I2C_SCL) { 00006 address_ = MPU9150_ADDRESS_DEFAULT; 00007 00008 accelRange_ = 0; 00009 gyroRange_ = 0; 00010 } 00011 00012 MPU9150::MPU9150(const bool AD0) : i2c_(I2C_SDA, I2C_SCL) { 00013 if(AD0) { 00014 address_ = MPU9150_ADDRESS_AD0_HIGH; 00015 } else { 00016 address_ = MPU9150_ADDRESS_AD0_LOW; 00017 } 00018 00019 accelRange_ = 0; 00020 gyroRange_ = 0; 00021 } 00022 00023 MPU9150::MPU9150(const PinName sda, const PinName scl, const bool AD0) : i2c_(sda, scl) { 00024 if(AD0) { 00025 address_ = MPU9150_ADDRESS_AD0_HIGH; 00026 } else { 00027 address_ = MPU9150_ADDRESS_AD0_LOW; 00028 } 00029 00030 accelRange_ = 0; 00031 gyroRange_ = 0; 00032 } 00033 00034 void MPU9150::initialize() { 00035 i2c_.setFrequency(100000); // Set frequency to 400 kHz 00036 setClockSource(MPU9150_PWR_MGMT_CLOCK_GYRO_X); 00037 setGyroFullScaleRange(MPU9150_GYRO_FS_250); 00038 setAccelFullScaleRange(MPU9150_ACCEL_FS_2); 00039 setSleepEnabled(false); 00040 } 00041 00042 bool MPU9150::test() { 00043 return getDeviceID() == 0x34; 00044 } 00045 00046 uint8_t MPU9150::getSampleRateDivider() { 00047 uint8_t buf = 0xFF; 00048 i2c_.readByte(address_, MPU9150_RA_SMPRT_DIV, &buf); 00049 return buf; 00050 } 00051 void MPU9150::setSampleRateDivider(const uint8_t divider) { 00052 i2c_.writeByte(address_, MPU9150_RA_SMPRT_DIV, divider); 00053 } 00054 00055 uint8_t MPU9150::getExternalFrameSync() { 00056 uint8_t buf = 0xFF; 00057 i2c_.readBits(address_, MPU9150_RA_CONFIG, 5, 3, &buf); 00058 return buf; 00059 } 00060 void MPU9150::setExternalFrameSync(const uint8_t sync) { 00061 i2c_.writeBits(address_, MPU9150_RA_CONFIG, 5, 3, sync); 00062 } 00063 uint8_t MPU9150::getDLPFBandwidth() { 00064 uint8_t buf = 0xFF; 00065 i2c_.readBits(address_, MPU9150_RA_CONFIG, 2, 3, &buf); 00066 return buf; 00067 } 00068 void MPU9150::setDLPFBandwidth(const uint8_t bandwidth) { 00069 i2c_.writeBits(address_, MPU9150_RA_CONFIG, 2, 3, bandwidth); 00070 } 00071 00072 uint8_t MPU9150::getGyroFullScaleRange() { 00073 uint8_t buf = 0xFF; 00074 i2c_.readBits(address_, MPU9150_RA_GYRO_CONFIG, 4, 2, &buf); 00075 return buf; 00076 } 00077 void MPU9150::setGyroFullScaleRange(const uint8_t range) { 00078 i2c_.writeBits(address_, MPU9150_RA_GYRO_CONFIG, 4, 2, range); 00079 } 00080 00081 uint8_t MPU9150::getAccelFullScaleRange() { 00082 uint8_t buf = 0xFF; 00083 i2c_.readBits(address_, MPU9150_RA_ACCEL_CONFIG, 4, 2, &buf); 00084 return buf; 00085 } 00086 void MPU9150::setAccelFullScaleRange(const uint8_t range) { 00087 i2c_.writeBits(address_, MPU9150_RA_ACCEL_CONFIG, 4, 2, range); 00088 } 00089 00090 int16_t MPU9150::getAccelX() { 00091 uint16_t buf = 0xFFFF; 00092 i2c_.readWord(address_, MPU9150_RA_ACCEL_XOUT_H, &buf); 00093 return (int16_t)buf; 00094 } 00095 int16_t MPU9150::getAccelY() { 00096 uint16_t buf = 0xFFFF; 00097 i2c_.readWord(address_, MPU9150_RA_ACCEL_YOUT_H, &buf); 00098 return (int16_t)buf; 00099 } 00100 int16_t MPU9150::getAccelZ() { 00101 uint16_t buf = 0xFFFF; 00102 i2c_.readWord(address_, MPU9150_RA_ACCEL_ZOUT_H, &buf); 00103 return (int16_t)buf; 00104 } 00105 00106 int16_t MPU9150::getTemp() { 00107 uint16_t buf = 0xFFFF; 00108 i2c_.readWord(address_, MPU9150_RA_TEMP_OUT_H, &buf); 00109 return (int16_t)buf; 00110 } 00111 00112 int16_t MPU9150::getGyroX() { 00113 uint16_t buf = 0xFFFF; 00114 i2c_.readWord(address_, MPU9150_RA_GYRO_XOUT_H, &buf); 00115 return (int16_t)buf; 00116 } 00117 int16_t MPU9150::getGyroY() { 00118 uint16_t buf = 0xFFFF; 00119 i2c_.readWord(address_, MPU9150_RA_GYRO_YOUT_H, &buf); 00120 return (int16_t)buf; 00121 } 00122 int16_t MPU9150::getGyroZ() { 00123 uint16_t buf = 0xFFFF; 00124 i2c_.readWord(address_, MPU9150_RA_GYRO_ZOUT_H, &buf); 00125 return (int16_t)buf; 00126 } 00127 00128 void MPU9150::resetGyroPath() { 00129 i2c_.writeBit(address_, MPU9150_RA_SIGNAL_PATH_RESET, 2, 0x01); 00130 } 00131 void MPU9150::resetAccelPath() { 00132 i2c_.writeBit(address_, MPU9150_RA_SIGNAL_PATH_RESET, 1, 0x01); 00133 } 00134 void MPU9150::resetTempPath() { 00135 i2c_.writeBit(address_, MPU9150_RA_SIGNAL_PATH_RESET, 0, 0x01); 00136 } 00137 00138 bool MPU9150::getFifoEnabled() { 00139 uint8_t buf = 0xFF; 00140 i2c_.readBit(address_, MPU9150_RA_USER_CTRL, 6, &buf); 00141 return (bool)buf; 00142 } 00143 void MPU9150::setFifoEnabled(const bool fifo) { 00144 i2c_.writeBit(address_, MPU9150_RA_USER_CTRL, 6, fifo); 00145 } 00146 bool MPU9150::getI2CMasterEnabled() { 00147 uint8_t buf = 0xFF; 00148 i2c_.readBit(address_, MPU9150_RA_USER_CTRL, 5, &buf); 00149 return (bool)buf; 00150 } 00151 void MPU9150::setI2CMasterEnabled(const bool master) { 00152 i2c_.writeBit(address_, MPU9150_RA_USER_CTRL, 5, master); 00153 } 00154 void MPU9150::resetFifo() { 00155 i2c_.writeBit(address_, MPU9150_RA_USER_CTRL, 2, 0x01); 00156 } 00157 void MPU9150::resetI2CMaster() { 00158 i2c_.writeBit(address_, MPU9150_RA_USER_CTRL, 1, 0x01); 00159 } 00160 void MPU9150::resetSensors() { 00161 i2c_.writeBit(address_, MPU9150_RA_USER_CTRL, 0, 0x01); 00162 } 00163 00164 void MPU9150::reset() { 00165 i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_1, 7, 0x01); 00166 } 00167 bool MPU9150::getSleepEnabled() { 00168 uint8_t buf = 0xFF; 00169 i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_1, 6, &buf); 00170 return (bool)buf; 00171 } 00172 void MPU9150::setSleepEnabled(const bool sleep) { 00173 i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_1, 6, sleep); 00174 } 00175 bool MPU9150::getCycleEnabled() { 00176 uint8_t buf = 0xFF; 00177 i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_1, 5, &buf); 00178 return (bool)buf; 00179 } 00180 void MPU9150::setCycleEnabled(const bool cycle) { 00181 i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_1, 5, cycle); 00182 } 00183 bool MPU9150::getTempEnabled() { 00184 uint8_t buf = 0xFF; 00185 i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_1, 3, &buf); 00186 return (bool)buf; 00187 } 00188 void MPU9150::setTempEnabled(const bool temp) { 00189 i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_1, 3, temp); 00190 } 00191 uint8_t MPU9150::getClockSource() { 00192 uint8_t buf = 0xFF; 00193 i2c_.readBits(address_, MPU9150_RA_PWR_MGMT_1, 2, 3, &buf); 00194 return buf; 00195 } 00196 void MPU9150::setClockSource(const uint8_t source) { 00197 i2c_.writeBits(address_, MPU9150_RA_PWR_MGMT_1, 2, 3, source); 00198 } 00199 00200 uint8_t MPU9150::getCycleFrequency() { 00201 uint8_t buf = 0xFF; 00202 i2c_.readBits(address_, MPU9150_RA_PWR_MGMT_2, 7, 2, &buf); 00203 return buf; 00204 } 00205 void MPU9150::setCycleFrequency(const uint8_t frequency) { 00206 i2c_.writeBits(address_, MPU9150_RA_PWR_MGMT_2, 7, 2, frequency); 00207 } 00208 bool MPU9150::getStandbyAccelXEnabled() { 00209 uint8_t buf = 0xFF; 00210 i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_2, 5, &buf); 00211 return (bool)buf; 00212 } 00213 void MPU9150::setStandbyAccelXEnabled(const bool enabled) { 00214 i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_2, 5, enabled); 00215 } 00216 bool MPU9150::getStandbyAccelYEnabled() { 00217 uint8_t buf = 0xFF; 00218 i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_2, 4, &buf); 00219 return (bool)buf; 00220 } 00221 void MPU9150::setStandbyAccelYEnabled(const bool enabled) { 00222 i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_2, 4, enabled); 00223 } 00224 bool MPU9150::getStandbyAccelZEnabled() { 00225 uint8_t buf = 0xFF; 00226 i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_2, 3, &buf); 00227 return (bool)buf; 00228 } 00229 void MPU9150::setStandbyAccelZEnabled(const bool enabled) { 00230 i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_2, 3, enabled); 00231 } 00232 bool MPU9150::getStandbyGyroXEnabled() { 00233 uint8_t buf = 0xFF; 00234 i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_2, 2, &buf); 00235 return (bool)buf; 00236 } 00237 void MPU9150::setStandbyGyroXEnabled(const bool enabled) { 00238 i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_2, 2, enabled); 00239 } 00240 bool MPU9150::getStandbyGyroYEnabled() { 00241 uint8_t buf = 0xFF; 00242 i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_2, 1, &buf); 00243 return (bool)buf; 00244 } 00245 void MPU9150::setStandbyGyroYEnabled(const bool enabled) { 00246 i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_2, 1, enabled); 00247 } 00248 bool MPU9150::getStandbyGyroZEnabled() { 00249 uint8_t buf = 0xFF; 00250 i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_2, 0, &buf); 00251 return (bool)buf; 00252 } 00253 void MPU9150::setStandbyGyroZEnabled(const bool enabled) { 00254 i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_2, 0, enabled); 00255 } 00256 00257 uint8_t MPU9150::getDeviceID() { 00258 uint8_t buf = 0xFF; 00259 i2c_.readBits(address_, MPU9150_RA_WHO_AM_I, 6, 6, &buf); 00260 return buf; 00261 }
Generated on Thu Jul 28 2022 00:11:41 by
1.7.2