huyukuo 发表于 2013-11-4 12:59 
谢谢答复,MPU6050和BMP085都读出来了。
现在就HMC5883没读出来。这个也有Bypass模式吗?
看下面的代码- #include "Wire.h"
- #include "I2Cdev.h"
- #include "MPU6050.h"
- #include "HMC5883L.h"
- #include "BMP085.h"
- MPU6050 accelgyro;
- HMC5883L mag;
- BMP085 barometer;
- int16_t ax, ay, az;
- int16_t gx, gy, gz;
- int16_t mx, my, mz;
- float temperature;
- float pressure;
- float altitude;
- int32_t lastMicros;
- #define LED_PIN 13
- bool blinkState = false;
- void setup() {
- Wire.begin();
- Serial.begin(38400);
- Serial.println("Initializing MPU6050 devices...");
- accelgyro.initialize();
- Serial.println("Testing MPU6050 connections...");
- Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
- delay(1000);
- accelgyro.setIntI2CMasterEnabled(0);
- accelgyro.setI2CBypassEnabled(1);
- if((!accelgyro.getI2CMasterModeEnabled()) && accelgyro.getI2CBypassEnabled())
- Serial.println("Set MPU6050 Bypass Mode success!");
- Serial.println("Initializing HMC5883L devices...");
-
- mag.initialize();
- Serial.println("Testing HMC5883L connections...");
- Serial.println(mag.testConnection() ? "HMC5883L connection successful" : "HMC5883L connection failed");
- delay(1000);
- // initialize device
- Serial.println("Initializing BMP085 ...");
- barometer.initialize();
- // verify connection
- Serial.println("Testing device connections...");
- Serial.println(barometer.testConnection() ? "BMP085 connection successful" : "BMP085 connection failed");
- delay(1000);
- pinMode(LED_PIN, OUTPUT);
- }
- void loop() {
- getMPU6050Arguments();
- getHMC5883LArguments();
- getBMP085Arguments();
- delay(500);
- blinkState = !blinkState;
- digitalWrite(LED_PIN, blinkState);
- }
- void getMPU6050Arguments(){
- // read raw accel/gyro measurements from device
- accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
- // display tab-separated accel/gyro x/y/z values
- Serial.print("a/g:\t");
- Serial.print(ax);
- Serial.print("\t");
- Serial.print(ay);
- Serial.print("\t");
- Serial.print(az);
- Serial.print("\t");
- Serial.print(gx);
- Serial.print("\t");
- Serial.print(gy);
- Serial.print("\t");
- Serial.println(gz);
- }
- void getHMC5883LArguments(){
- mag.getHeading(&mx, &my, &mz);
- // display tab-separated gyro x/y/z values
- Serial.print("mag:\t");
- Serial.print(mx);
- Serial.print("\t");
- Serial.print(my);
- Serial.print("\t");
- Serial.print(mz);
- Serial.print("\t");
- // To calculate heading in degrees. 0 degree indicates North
- float heading = atan2(my, mx);
- if(heading < 0)
- heading += 2 * M_PI;
- Serial.print("heading:\t");
- Serial.println(heading * 180/M_PI);
- }
- void getBMP085Arguments(){
- // request temperature
- barometer.setControl(BMP085_MODE_TEMPERATURE);
- // wait appropriate time for conversion (4.5ms delay)
- lastMicros = micros();
- while (micros() - lastMicros < barometer.getMeasureDelayMicroseconds());
- // read calibrated temperature value in degrees Celsius
- temperature = barometer.getTemperatureC();
- // request pressure (3x oversampling mode, high detail, 23.5ms delay)
- barometer.setControl(BMP085_MODE_PRESSURE_3);
- while (micros() - lastMicros < barometer.getMeasureDelayMicroseconds());
- // read calibrated pressure value in Pascals (Pa)
- pressure = barometer.getPressure();
- // calculate absolute altitude in meters based on known pressure
- // (may pass a second "sea level pressure" parameter here,
- // otherwise uses the standard value of 101325 Pa)
- altitude = barometer.getAltitude(pressure);
- // display measured values if appropriate
- Serial.print("T/P/A\t");
- Serial.print(temperature);
- Serial.print("\t");
- Serial.print(pressure);
- Serial.print("\t");
- Serial.print(altitude);
- Serial.println("");
- }
复制代码 |