极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 26051|回复: 9

HMC5883L 读不出,请大家帮忙看看。

[复制链接]
发表于 2013-11-2 17:33:01 | 显示全部楼层 |阅读模式
我用的是MPU6050+HMC5883L+BMP085一体传感,板子是mega 2560。
其中MPU6050和BMP085使用例子成功读数。
但HMC5883L就是读不出,网上能搜索到的例子都试过了,所有的都不行。
本来以为坏了,刷上MWC2.0,结果发现传感器是好的。郁闷了。。请大家帮帮忙。

第一种是出来都是零


第二种是读数没有变化。


是这个板子。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2013-11-2 23:29:14 | 显示全部楼层
数据太少。你这样不好分析啊
起码把模块电路。代码发一下
回复 支持 反对

使用道具 举报

发表于 2013-11-3 04:58:58 | 显示全部楼层
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-3 09:31:01 | 显示全部楼层
天天向上/tp 发表于 2013-11-3 04:58
试试这个例子http://www.geek-workshop.com/thread-105-1-1.html

这个例子试了,结果就是第二张截图那样。
回复 支持 反对

使用道具 举报

发表于 2013-11-4 09:43:59 | 显示全部楼层
把MPU6050配置成Bypass模式,就相当于把辅助I2C总线直接连到了主I2C总线上,利用I2C协议按地址去操作就可以了。

//配置MPU6000  bypass模式
accelgyro.setI2CMasterModeEnabled(0);
accelgyro.setI2CBypassEnabled(1);
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-4 12:59:06 | 显示全部楼层
ent173 发表于 2013-11-4 09:43
把MPU6050配置成Bypass模式,就相当于把辅助I2C总线直接连到了主I2C总线上,利用I2C协议按地址去操作就可以 ...

谢谢答复,MPU6050和BMP085都读出来了。
现在就HMC5883没读出来。这个也有Bypass模式吗?
回复 支持 反对

使用道具 举报

发表于 2013-11-4 17:57:12 | 显示全部楼层
huyukuo 发表于 2013-11-4 12:59
谢谢答复,MPU6050和BMP085都读出来了。
现在就HMC5883没读出来。这个也有Bypass模式吗?

http://www.geek-workshop.com/thread-1793-1-1.html
看下这个帖子
回复 支持 反对

使用道具 举报

发表于 2013-11-4 18:00:36 | 显示全部楼层
huyukuo 发表于 2013-11-4 12:59
谢谢答复,MPU6050和BMP085都读出来了。
现在就HMC5883没读出来。这个也有Bypass模式吗?

看下面的代码
  1. #include "Wire.h"
  2. #include "I2Cdev.h"
  3. #include "MPU6050.h"
  4. #include "HMC5883L.h"
  5. #include "BMP085.h"

  6. MPU6050 accelgyro;
  7. HMC5883L mag;
  8. BMP085 barometer;

  9. int16_t ax, ay, az;
  10. int16_t gx, gy, gz;
  11. int16_t mx, my, mz;
  12. float temperature;
  13. float pressure;
  14. float altitude;
  15. int32_t lastMicros;

  16. #define LED_PIN 13
  17. bool blinkState = false;

  18. void setup() {
  19.   Wire.begin();
  20.   Serial.begin(38400);
  21.   Serial.println("Initializing MPU6050 devices...");
  22.   accelgyro.initialize();

  23.   Serial.println("Testing MPU6050 connections...");
  24.   Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  25.   delay(1000);
  26.   accelgyro.setIntI2CMasterEnabled(0);
  27.   accelgyro.setI2CBypassEnabled(1);
  28.   if((!accelgyro.getI2CMasterModeEnabled()) && accelgyro.getI2CBypassEnabled())
  29.     Serial.println("Set MPU6050 Bypass Mode success!");
  30.   Serial.println("Initializing HMC5883L devices...");
  31.   
  32.   mag.initialize();
  33.   Serial.println("Testing HMC5883L connections...");
  34.   Serial.println(mag.testConnection() ? "HMC5883L connection successful" : "HMC5883L connection failed");
  35.   delay(1000);

  36.   // initialize device
  37.   Serial.println("Initializing BMP085 ...");
  38.   barometer.initialize();

  39.   // verify connection
  40.   Serial.println("Testing device connections...");
  41.   Serial.println(barometer.testConnection() ? "BMP085 connection successful" : "BMP085 connection failed");
  42.   delay(1000);
  43.   pinMode(LED_PIN, OUTPUT);
  44. }

  45. void loop() {
  46.   getMPU6050Arguments();
  47.   getHMC5883LArguments();
  48.   getBMP085Arguments();
  49.   delay(500);
  50.   blinkState = !blinkState;
  51.   digitalWrite(LED_PIN, blinkState);

  52. }

  53. void getMPU6050Arguments(){
  54.   // read raw accel/gyro measurements from device
  55.   accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  56.   // display tab-separated accel/gyro x/y/z values
  57.   Serial.print("a/g:\t");
  58.   Serial.print(ax);
  59.   Serial.print("\t");
  60.   Serial.print(ay);
  61.   Serial.print("\t");
  62.   Serial.print(az);
  63.   Serial.print("\t");
  64.   Serial.print(gx);
  65.   Serial.print("\t");
  66.   Serial.print(gy);
  67.   Serial.print("\t");
  68.   Serial.println(gz);
  69. }

  70. void getHMC5883LArguments(){
  71.   mag.getHeading(&mx, &my, &mz);

  72.   // display tab-separated gyro x/y/z values
  73.   Serial.print("mag:\t");
  74.   Serial.print(mx);
  75.   Serial.print("\t");
  76.   Serial.print(my);
  77.   Serial.print("\t");
  78.   Serial.print(mz);
  79.   Serial.print("\t");

  80.   // To calculate heading in degrees. 0 degree indicates North
  81.   float heading = atan2(my, mx);
  82.   if(heading < 0)
  83.     heading += 2 * M_PI;
  84.   Serial.print("heading:\t");
  85.   Serial.println(heading * 180/M_PI);
  86. }

  87. void getBMP085Arguments(){
  88.   // request temperature
  89.   barometer.setControl(BMP085_MODE_TEMPERATURE);

  90.   // wait appropriate time for conversion (4.5ms delay)
  91.   lastMicros = micros();
  92.   while (micros() - lastMicros < barometer.getMeasureDelayMicroseconds());

  93.   // read calibrated temperature value in degrees Celsius
  94.   temperature = barometer.getTemperatureC();

  95.   // request pressure (3x oversampling mode, high detail, 23.5ms delay)
  96.   barometer.setControl(BMP085_MODE_PRESSURE_3);
  97.   while (micros() - lastMicros < barometer.getMeasureDelayMicroseconds());

  98.   // read calibrated pressure value in Pascals (Pa)
  99.   pressure = barometer.getPressure();

  100.   // calculate absolute altitude in meters based on known pressure
  101.   // (may pass a second "sea level pressure" parameter here,
  102.   // otherwise uses the standard value of 101325 Pa)
  103.   altitude = barometer.getAltitude(pressure);

  104.   // display measured values if appropriate
  105.   Serial.print("T/P/A\t");
  106.   Serial.print(temperature);
  107.   Serial.print("\t");
  108.   Serial.print(pressure);
  109.   Serial.print("\t");
  110.   Serial.print(altitude);
  111.   Serial.println("");
  112. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2013-11-4 23:21:17 | 显示全部楼层
i2c读不出来 检查可以这样来做。

芯片工作电压是否符合要求,上拉电阻是否有(有的板子有有的板子没有),地址是否正确。

问题也要描述清楚,到底问题出在哪一步,初始化有没有成功。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-5 18:19:25 | 显示全部楼层
ent173 发表于 2013-11-4 18:00
看下面的代码

十分感谢大神!!使用bypass,都能读出来了。
但是不能理解,为什么要搞个bypass模式,难道不是bypass模式的时候,MPU6050还对HMC5883L过来的数据做处理吗?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-9 06:52 , Processed in 0.039184 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表