l710025 发表于 2014-4-22 16:32:53

用过HMC5883L的交流,谢谢

/*
HMC5883L_Example.pde - Example sketch for integration with an HMC5883L triple axis magnetomerwe.
Copyright (C) 2011 Love Electronics (loveelectronics.co.uk)

This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.If not, see <http://www.gnu.org/licenses/>.

*/

// Reference the I2C Library
#include <Wire.h>
// Reference the HMC5883L Compass Library
#include <HMC5883L.h>

// Store our compass as a variable.
HMC5883L compass;
// Record any errors that may occur in the compass.
int error = 0;

// Out setup routine, here we will configure the microcontroller and compass.
void setup()
{
// Initialize the serial port.
Serial.begin(9600);

Serial.println("Starting the I2C interface.");
Wire.begin(); // Start the I2C interface.

Serial.println("Constructing new HMC5883L");
compass = HMC5883L(); // Construct a new HMC5883 compass.

Serial.println("Setting scale to +/- 1.3 Ga");
error = compass.SetScale(1.3); // Set the scale of the compass.
if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));

Serial.println("Setting measurement mode to continous.");
error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));
}

// Our main program loop.
void loop()
{
// Retrive the raw values from the compass (not scaled).
MagnetometerRaw raw = compass.ReadRawAxis();
// Retrived the scaled values from the compass (scaled to the configured scale).
MagnetometerScaled scaled = compass.ReadScaledAxis();

// Values are accessed like so:
int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)

// Calculate heading when the magnetometer is level, then correct for signs of axis.
float heading = atan2(scaled.YAxis, scaled.XAxis);

// Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
// Find yours here: http://www.magnetic-declination.com/
// Mine is: 2� 37' W, which is 2.617 Degrees, or (which we need) 0.0456752665 radians, I will use 0.0457
// If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
float declinationAngle = 0.0457;
heading += declinationAngle;

// Correct for when signs are reversed.
if(heading < 0)
    heading += 2*PI;

// Check for wrap due to addition of declination.
if(heading > 2*PI)
    heading -= 2*PI;

// Convert radians to degrees for readability.
float headingDegrees = heading * 180/M_PI;

// Output the data via the serial port.
Output(raw, scaled, heading, headingDegrees);

// Normally we would delay the application by 66ms to allow the loop
// to run at 15Hz (default bandwidth for the HMC5883L).
// However since we have a long serial out (104ms at 9600) we will let
// it run at its natural speed.
// delay(66);
}

// Output the data down the serial port.
void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float headingDegrees)
{
   Serial.print("Raw:\t");
   Serial.print(raw.XAxis);
   Serial.print("   ");   
   Serial.print(raw.YAxis);
   Serial.print("   ");   
   Serial.print(raw.ZAxis);
   Serial.print("   \tScaled:\t");

   Serial.print(scaled.XAxis);
   Serial.print("   ");   
   Serial.print(scaled.YAxis);
   Serial.print("   ");   
   Serial.print(scaled.ZAxis);

   Serial.print("   \tHeading:\t");
   Serial.print(heading);
   Serial.print(" Radians   \t");
   Serial.print(headingDegrees);
   Serial.println(" Degrees   \t");
}


这是网上例程,arduino环境系可以编译,可是其中提到的error输出是怎么显示的,怎么不管是设置比例还是设置模式,error是一样的。还有,模式的定义在哪个文件里,压缩包中只有HMC5883L.cpp和HMC5883L.h文件,我就搞不懂了,也找不到了,恳请高手指点交流,谢谢!

lauren 发表于 2014-4-22 16:39:32

你用的硬件平台是啥?。

l710025 发表于 2014-4-22 16:55:10

arduino IDE

l710025 发表于 2014-4-22 16:57:15

硬件就是HMC5883L,软件编译是arduino IDE

何处不江南 发表于 2014-4-22 18:06:39

定义一般在.h文件中,函数都在.cpp文件中,

Youyou 发表于 2014-4-24 10:27:07

目测您的库可能过旧或者Arduino IDE版本过旧造成的ERROR,我用下来没问题。您可以用我附件中的库试试看,样例代码在库中。用Arduino IDE1.0.5。
模式定义和地址定义可以在 .h 文件中找到:
#define HMC5883L_Address 0x1E
#define ConfigurationRegisterA 0x00
#define ConfigurationRegisterB 0x01
#define ModeRegister 0x02
#define DataRegisterBegin 0x03

#define Measurement_Continuous 0x00
#define Measurement_SingleShot 0x01
#define Measurement_Idle 0x03

l710025 发表于 2014-4-24 17:15:50

#define HMC5883L_Address 0x1E
为什么这么定义啊?

Youyou 发表于 2014-4-24 17:38:29

本帖最后由 Youyou 于 2014-4-25 01:31 编辑

l710025 发表于 2014-4-24 17:15
#define HMC5883L_Address 0x1E
为什么这么定义啊?
这个定义应该是从机地址,在I2C通讯时需要这个从机地址,具体的地址取决于器件的出厂设置还有地址管脚的设置。至于为什么要这样定义,你可以查看下这芯片的数据手册是怎么定义地址的,再结合HMC5883L的原理图,就可以得出是不是0x1E了。怎么得出的,请参考楼下附件中的PPT。再者,我个人认为这样定义是一种良好的编程习惯,方便测试或者修改。

Youyou 发表于 2014-4-24 17:41:27

本帖最后由 Youyou 于 2014-4-25 01:26 编辑

I2C通信协可以参考这个帖子:https://www.dfrobot.com.cn/commun ... =viewthread&tid=789
推荐阅读附件中的PPT,看完后你将对I2C通信有一个很完整的了解。

页: [1]
查看完整版本: 用过HMC5883L的交流,谢谢