A quick adaptation of a library made for Arduino by Fabio Varesano Interface a Honeywell HMC58X3 magnetometer to a mbed via i2c.

Dependents:   FreeIMU FreeIMU

Fork of HMC58X3 by Aloïs Wolff

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 #include "mbed.h"
00038 #include "MODI2C.h"
00039 
00040 #ifndef I2C_SDA
00041     #define I2C_SDA p9
00042     #define I2C_SCL p10
00043 #endif
00044 
00045 #define HMC58X3_ADDR 0x1E // 7 bit address of the HMC58X3 used with the Wire library
00046 #define HMC_POS_BIAS 1
00047 #define HMC_NEG_BIAS 2
00048 
00049 // HMC58X3 register map. For details see HMC58X3 datasheet
00050 #define HMC58X3_R_CONFA 0
00051 #define HMC58X3_R_CONFB 1
00052 #define HMC58X3_R_MODE 2
00053 #define HMC58X3_R_XM 3
00054 #define HMC58X3_R_XL 4
00055 
00056 #ifdef ISHMC5843
00057 #define HMC58X3_R_YM (5)    //!< Register address for YM.
00058 #define HMC58X3_R_YL (6)    //!< Register address for YL.
00059 #define HMC58X3_R_ZM (7)    //!< Register address for ZM.
00060 #define HMC58X3_R_ZL (8)    //!< Register address for ZL.
00061 
00062 #define HMC58X3_X_SELF_TEST_GAUSS (+0.55)                       //!< X axis level when bias current is applied.
00063 #define HMC58X3_Y_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS)   //!< Y axis level when bias current is applied.
00064 #define HMC58X3_Z_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS)   //!< Y axis level when bias current is applied.
00065 
00066 /*
00067     This is my best guess at the LOW, HIGH limit.  The data sheet does not have these values.
00068 */
00069 #define SELF_TEST_LOW_LIMIT  (HMC58X3_X_SELF_TEST_GAUSS*0.53)   //!< Low limit 53% of expected value.
00070 #define SELF_TEST_HIGH_LIMIT (HMC58X3_X_SELF_TEST_GAUSS*1.36)   //!< High limit 136% of expected values.
00071 #else // HMC5883L
00072 #define HMC58X3_R_YM (7)  //!< Register address for YM.
00073 #define HMC58X3_R_YL (8)  //!< Register address for YL.
00074 #define HMC58X3_R_ZM (5)  //!< Register address for ZM.
00075 #define HMC58X3_R_ZL (6)  //!< Register address for ZL.
00076 
00077 #define HMC58X3_X_SELF_TEST_GAUSS (+1.16)                       //!< X axis level when bias current is applied.
00078 #define HMC58X3_Y_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS)   //!< Y axis level when bias current is applied.
00079 #define HMC58X3_Z_SELF_TEST_GAUSS (+1.08)                       //!< Y axis level when bias current is applied.
00080 
00081 #define SELF_TEST_LOW_LIMIT  (243.0/390.0)   //!< Low limit when gain is 5.
00082 #define SELF_TEST_HIGH_LIMIT (575.0/390.0)   //!< High limit when gain is 5.
00083 #endif
00084 
00085 #define HMC58X3_R_STATUS 9
00086 //#define HMC58X3_R_IDA 10
00087 #define HMC58X3_R_IDB 11
00088 #define HMC58X3_R_IDC 12
00089 
00090 class HMC58X3
00091 {
00092 
00093 
00094 public:
00095     HMC58X3();
00096     void init(bool setmode);
00097     void init(int address, bool setmode);
00098     void getValues(int16_t *x,int16_t *y,int16_t *z);
00099     void getValues(float *x,float *y,float *z);
00100     void getValues(float *xyz);
00101     void getRaw(int16_t *x,int16_t *y,int16_t *z);
00102     void getRaw(int16_t *xyz);
00103     void calibrate(unsigned char gain);     // Original calibrate with a few weaknesses.
00104     bool calibrate(unsigned char gain,unsigned int n_samples);
00105     void setMode(unsigned char mode);
00106     void setDOR(unsigned char DOR);
00107     void setGain(unsigned char gain);
00108     void getID(char id[3]);
00109     
00110     int16_t cache_x, cache_y, cache_z;
00111     
00112     static void samplingthread_stub(void const *p);
00113     
00114     void samplingthread();
00115     void start_sampling();
00116 
00117     static const int I2C_ADDRESS = 0x3D;
00118     char HMC58X3_R_IDA;
00119     MODI2C i2c;
00120     
00121     Thread _thread;
00122     
00123     Semaphore sem;
00124 
00125 private:
00126     void writeReg(unsigned char reg, unsigned char val);
00127     float x_scale,y_scale,z_scale,x_max,y_max,z_max;
00128     int min(int a, int b);
00129 };
00130 
00131 #endif // HMC58X3_h