Aloïs Wolff / HMC58X3
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HMC58X3.h Source File

HMC58X3.h

Go to the documentation of this file.
00001 /**
00002 @file HMC58X3.h - Interface a Honeywell HMC58X3 magnetometer to an Arduino via i2c.
00003 
00004 @author Fabio Varesano <fvaresano@yahoo.it>
00005 Copyright (C) 2011 Fabio Varesano <fvaresano@yahoo.it>
00006 ported for Mbed by Aloïs Wolff
00007 
00008 Based on:
00009 http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1274748346
00010   Modification/extension of the following by E.J.Muller
00011 http://eclecti.cc/hardware/hmc5843-magnetometer-library-for-arduino
00012   Copyright (c) 2009 Nirav Patel,
00013 
00014 The above were based on:
00015 http://www.sparkfun.com/datasheets/Sensors/Magneto/HMC58X3-v11.c
00016 http://www.atmel.com/dyn/resources/prod_documents/doc2545.pdf
00017 
00018 This program is free software: you can redistribute it and/or modify
00019 it under the terms of the version 3 GNU General Public License as
00020 published by the Free Software Foundation.
00021 
00022 This program is distributed in the hope that it will be useful,
00023 but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 GNU General Public License for more details.
00026 
00027 You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
00028 */
00029 
00030 //#define ISHMC5843 (1) // Uncomment this following line if you are using this library with the HMC5843.
00031 
00032 
00033 
00034 #ifndef HMC58X3_h
00035 #define HMC58X3_h
00036 
00037 #define HMC58X3_ADDR 0x1E // 7 bit address of the HMC58X3 used with the Wire library
00038 #define HMC_POS_BIAS 1
00039 #define HMC_NEG_BIAS 2
00040 
00041 // HMC58X3 register map. For details see HMC58X3 datasheet
00042 #define HMC58X3_R_CONFA 0
00043 #define HMC58X3_R_CONFB 1
00044 #define HMC58X3_R_MODE 2
00045 #define HMC58X3_R_XM 3
00046 #define HMC58X3_R_XL 4
00047 
00048 #ifdef ISHMC5843
00049 #define HMC58X3_R_YM (5)    //!< Register address for YM.
00050 #define HMC58X3_R_YL (6)    //!< Register address for YL.
00051 #define HMC58X3_R_ZM (7)    //!< Register address for ZM.
00052 #define HMC58X3_R_ZL (8)    //!< Register address for ZL.
00053 
00054 #define HMC58X3_X_SELF_TEST_GAUSS (+0.55)                       //!< X axis level when bias current is applied.
00055 #define HMC58X3_Y_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS)   //!< Y axis level when bias current is applied.
00056 #define HMC58X3_Z_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS)   //!< Y axis level when bias current is applied.
00057 
00058 /*
00059     This is my best guess at the LOW, HIGH limit.  The data sheet does not have these values.
00060 */
00061 #define SELF_TEST_LOW_LIMIT  (HMC58X3_X_SELF_TEST_GAUSS*0.53)   //!< Low limit 53% of expected value.
00062 #define SELF_TEST_HIGH_LIMIT (HMC58X3_X_SELF_TEST_GAUSS*1.36)   //!< High limit 136% of expected values.
00063 #else // HMC5883L
00064 #define HMC58X3_R_YM (7)  //!< Register address for YM.
00065 #define HMC58X3_R_YL (8)  //!< Register address for YL.
00066 #define HMC58X3_R_ZM (5)  //!< Register address for ZM.
00067 #define HMC58X3_R_ZL (6)  //!< Register address for ZL.
00068 
00069 #define HMC58X3_X_SELF_TEST_GAUSS (+1.16)                       //!< X axis level when bias current is applied.
00070 #define HMC58X3_Y_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS)   //!< Y axis level when bias current is applied.
00071 #define HMC58X3_Z_SELF_TEST_GAUSS (+1.08)                       //!< Y axis level when bias current is applied.
00072 
00073 #define SELF_TEST_LOW_LIMIT  (243.0/390.0)   //!< Low limit when gain is 5.
00074 #define SELF_TEST_HIGH_LIMIT (575.0/390.0)   //!< High limit when gain is 5.
00075 #endif
00076 
00077 #define HMC58X3_R_STATUS 9
00078 #define HMC58X3_R_IDA 10
00079 #define HMC58X3_R_IDB 11
00080 #define HMC58X3_R_IDC 12
00081 
00082 class HMC58X3
00083 {
00084 
00085 
00086 public:
00087     //HMC58X3(PinName sda, PinName scl);
00088     HMC58X3(I2C i2c_);
00089     void init(bool setmode);
00090     void init(int address, bool setmode);
00091     void getValues(int *x,int *y,int *z);
00092     void getValues(float *x,float *y,float *z);
00093     void getValues(float *xyz);
00094     void getRaw(int *x,int *y,int *z);
00095     void getRaw(int *xyz);
00096     void calibrate(unsigned char gain);     // Original calibrate with a few weaknesses.
00097     bool calibrate(unsigned char gain,unsigned int n_samples);
00098     void setMode(unsigned char mode);
00099     void setDOR(unsigned char DOR);
00100     void setGain(unsigned char gain);
00101     void getID(char id[3]);
00102 
00103     static const int I2C_ADDRESS = 0x3D;
00104 
00105 private:
00106     I2C i2c;
00107     void writeReg(unsigned char reg, unsigned char val);
00108     float x_scale,y_scale,z_scale,x_max,y_max,z_max;
00109     int min(int a, int b);
00110 };
00111 
00112 #endif // HMC58X3_h
00113