Farid 发表于 2011-11-17 11:15:08

【讨论】hmc5883L 读出的数据不对

//以下是arduino程序源码
#include <Wire.h>

#define HMC5883_WriteAddress 0x1E //i.e 0x3C >> 1
#define HMC5883_ModeRegisterAddress 0x02
#define HMC5883_ContinuousModeCommand 0x00
#define HMC5883_DataOutputXMSBAddress0x03


int outputData;

void setup()
{
    //SerialUSB.begin(9600);
    Wire.begin();       //Initiate the Wire library and join the I2C bus as a master
    //初始化部分
    Wire.beginTransmission(HMC5883_WriteAddress); //Initiate a transmission with HMC5883 (Write address).
    Wire.send(HMC5883_ModeRegisterAddress);       //Place the Mode Register Address in send-buffer.
    Wire.send(HMC5883_ContinuousModeCommand);   //Place the command for Continuous operation Mode in send-buffer.
    Wire.endTransmission();                     //Send the send-buffer to HMC5883 and end the I2C transmission.
    delay(100);
}

void loop() {

    int i,x,y,z;
    double angle;

    Wire.beginTransmission(HMC5883_WriteAddress);//Initiate a transmission with HMC5883 (Write address).          
    Wire.send(HMC5883_DataOutputXMSBAddress); //select register 3, X MSB register
    Wire.endTransmission();

    Wire.requestFrom(HMC5883_WriteAddress,6);      //Request 6 bytes of data from the address specified,2 registers per axis.
    delay(500);

    //Read the value of magnetic components X,Y and Z

    if(6 <= Wire.available()) // If the number of bytes available for reading be <=6.
    {
      for(i=0;i<6;i++)
      {
            outputData=Wire.receive();//Store the data in outputData buffer
      }
    }

    x=outputData << 8 | outputData; //Combine MSB and LSB of X Data output register
    z=outputData << 8 | outputData; //Combine MSB and LSB of Z Data output register
    y=outputData << 8 | outputData; //Combine MSB and LSB of Y Data output register
       
SerialUSB.print("X=");
        SerialUSB.print(x);
        SerialUSB.print("Y=");
        SerialUSB.print(y);
        SerialUSB.print("Z=");
        SerialUSB.print(z);


疑问:1.我将上述初始化部分代码放到setup里面,读出来的数据没有变化,而只有将初始化的部分放到loop里面,每次程序运行都初始化的时候 数据才有变化,请问这个是怎么回事呢?初始化代码就应该是放到setup里的呀。
2.我的读书如下:

X=65279.00Y=65535.00Z=65534.00
X=65279.00Y=65535.00Z=65534.00
X=65279.00Y=65535.00Z=65534.00
X=65279.00Y=65280.00Z=254.00
X=65535.00Y=65280.00Z=255.00
X=65535.00Y=65280.00Z=255.00
X=65535.00Y=65281.00Z=511.00
X=255.00Y=65281.00Z=256.00
X=255.00Y=65280.00Z=0.00
X=255.00Y=65280.00Z=0.00
X=511.00Y=65280.00Z=1.00
X=511.00Y=65535.00Z=65281.00
X=511.00Y=65535.00Z=65281.00
X=511.00Y=65535.00Z=65281.00
X=511.00Y=65535.00Z=65281.00
X=511.00Y=65534.00Z=65025.00
X=255.00Y=65534.00Z=65024.00
X=255.00Y=65534.00Z=65024.00
X=65535.00Y=65534.00Z=65279.00
X=65535.00Y=65534.00Z=65279.00
X=255.00Y=65534.00Z=65024.00
X=65535.00Y=65534.00Z=65279.00
X=65535.00Y=65534.00Z=65279.00
X=65279.00Y=65535.00Z=65534.00
X=65279.00Y=65535.00Z=65534.00
X=65279.00Y=65535.00Z=65534.00
X=65279.00Y=65280.00Z=254.00
X=65535.00Y=65280.00Z=255.00
X=65535.00Y=65280.00Z=255.00
X=65535.00Y=65281.00Z=511.00
从这个数据上看 应该不对。请问这个是怎么回事呢?

谢谢!

Farid 发表于 2011-11-17 11:40:15

回复【楼主位】Farid
-----------------------------------------------------------------------

另外 我的识别寄存器10, 11, 12 读出来的都是0 是不是硬件损坏了呢?

aishiqi1992 发表于 2011-11-17 11:54:26

和我的情况一模一样!同是天涯沦落人啊……你看看我的帖子

Farid 发表于 2011-11-17 17:17:15

回复【2楼】aishiqi1992小鸡
-----------------------------------------------------------------------

你的识别寄存器10, 11, 12 读出来的也都是0 ?

aishiqi1992 发表于 2011-11-19 19:32:28

回复【3楼】Farid
-----------------------------------------------------------------------

那倒不是,我就这一点比你好!!

Farid 发表于 2011-11-22 14:10:55

回复【4楼】aishiqi1992小鸡
-----------------------------------------------------------------------

小鸡,你的问题解决了么?我的还是没有……纠结

aishiqi1992 发表于 2011-11-23 20:28:40

回复【5楼】Farid
回复【4楼】aishiqi1992小鸡
-----------------------------------------------------------------------
小鸡,你的问题解决了么?我的还是没有……纠结
-----------------------------------------------------------------------

还没有,我又在网上买了两套,他要是再不行,我就愤怒的买热风枪了!

Farid 发表于 2011-11-25 15:37:20

回复【6楼】aishiqi1992小鸡
-----------------------------------------------------------------------

你的新板子到了?有没有试行不行?我的还是无解。打算换了……

aishiqi1992 发表于 2011-11-27 15:34:57

回复【7楼】Farid
-----------------------------------------------------------------------

今天刚到,,我更无语了。。竟然什么都读不出来。。。难道是我焊接的问题,两个都读不出来,我崩溃了。。

StephenCui 发表于 2011-11-27 15:45:39

HMC5883L对焊接温度很敏感,很可能是焊接时搞坏了。还有那个4.7uf的电容,要用质量好的,串联阻抗要小。

aishiqi1992 发表于 2011-11-28 08:04:07

回复【7楼】Farid
-----------------------------------------------------------------------

有转机了……有一个板子能用了,果然是焊接的问题!太难焊了,很多引脚不知道有没有焊上。我用刀头焊的。

Farid 发表于 2011-12-12 11:38:44

回复【10楼】aishiqi1992小鸡
-----------------------------------------------------------------------

你的现在能用了么?

aishiqi1992 发表于 2011-12-14 11:23:59

回复【11楼】Farid
回复【10楼】aishiqi1992小鸡
-----------------------------------------------------------------------
你的现在能用了么?
-----------------------------------------------------------------------

有一个已经能用啦!激动呦

yue435 发表于 2012-1-31 20:05:33

唉,这个。。。同纠结啊,貌似我的可以读,但是最后算出来的数据不对。。。。是参考淘宝的程序。。。

Name_006 发表于 2012-2-15 10:58:14

我有这方面的程序 是L3g4200d HMC5883LADXL345 综合起来的 可以参考下我的帖子算法也正在研究

济南电子爱好者 发表于 2012-4-15 13:12:40

不知道楼主的问题解决没有,因为数据在传感器内是补码形似存放的,输出的大于60000的数据其实是负值 ,需要在原数据上加负号再输出

zhailine 发表于 2012-4-23 13:21:39


怎么我的数据是这么大呢?
x            y          z
14847,-4354,-23554;
14591,-4354,-23298;
14591,-3074,-22786;
14335,-7426,-23042;
14847,-4610,-22786;
15103,-4098,-22786;

xiwangyangguang 发表于 2012-5-22 14:53:31

Name_006 发表于 2012-2-15 10:58 static/image/common/back.gif
我有这方面的程序 是L3g4200d HMC5883LADXL345 综合起来的 可以参考下我的帖子算法也正在研究 ...

能把你的程序贴出来,学习学习不?

Name_006 发表于 2012-5-22 17:27:41

本帖最后由 Name_006 于 2012-5-22 17:28 编辑

xiwangyangguang 发表于 2012-5-22 14:53 static/image/common/back.gif
能把你的程序贴出来,学习学习不?

http://www.ourdev.cn/forum.php?mod=viewthread&tid=5426588&highlight=L3g4200d搜索下就有啦   貌似下载不了到淘宝找我QQ   或者加那个群 群里有

hanxiao55 发表于 2012-8-30 00:31:20

Farid 发表于 2011-11-17 17:17 static/image/common/back.gif
回复【2楼】aishiqi1992小鸡
----------------------------------------------------------------------- ...

您好我的HMC5883L情况和您的一样识别寄存器读书都是0   请问您是如何解决的?求分享多谢你了
页: [1]
查看完整版本: 【讨论】hmc5883L 读出的数据不对