HMC58X3 Library

Fork of HMC58X3 by Yifei Teng

Committer:
tyftyftyf
Date:
Sat Nov 02 17:23:23 2013 +0000
Revision:
2:c5ac16c88514
Parent:
1:72ecf7399250
Child:
3:1e0e0c47287a
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pommzorz 0:d84e1c7e4e86 1 /**
pommzorz 0:d84e1c7e4e86 2 @file HMC58X3.h - Interface a Honeywell HMC58X3 magnetometer to an Arduino via i2c.
pommzorz 0:d84e1c7e4e86 3
pommzorz 0:d84e1c7e4e86 4 @author Fabio Varesano <fvaresano@yahoo.it>
pommzorz 0:d84e1c7e4e86 5 Copyright (C) 2011 Fabio Varesano <fvaresano@yahoo.it>
pommzorz 0:d84e1c7e4e86 6 ported for Mbed by Aloïs Wolff
pommzorz 0:d84e1c7e4e86 7
pommzorz 0:d84e1c7e4e86 8 Based on:
pommzorz 0:d84e1c7e4e86 9 http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1274748346
pommzorz 0:d84e1c7e4e86 10 Modification/extension of the following by E.J.Muller
pommzorz 0:d84e1c7e4e86 11 http://eclecti.cc/hardware/hmc5843-magnetometer-library-for-arduino
pommzorz 0:d84e1c7e4e86 12 Copyright (c) 2009 Nirav Patel,
pommzorz 0:d84e1c7e4e86 13
pommzorz 0:d84e1c7e4e86 14 The above were based on:
pommzorz 0:d84e1c7e4e86 15 http://www.sparkfun.com/datasheets/Sensors/Magneto/HMC58X3-v11.c
pommzorz 0:d84e1c7e4e86 16 http://www.atmel.com/dyn/resources/prod_documents/doc2545.pdf
pommzorz 0:d84e1c7e4e86 17
pommzorz 0:d84e1c7e4e86 18 This program is free software: you can redistribute it and/or modify
pommzorz 0:d84e1c7e4e86 19 it under the terms of the version 3 GNU General Public License as
pommzorz 0:d84e1c7e4e86 20 published by the Free Software Foundation.
pommzorz 0:d84e1c7e4e86 21
pommzorz 0:d84e1c7e4e86 22 This program is distributed in the hope that it will be useful,
pommzorz 0:d84e1c7e4e86 23 but WITHOUT ANY WARRANTY; without even the implied warranty of
pommzorz 0:d84e1c7e4e86 24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
pommzorz 0:d84e1c7e4e86 25 GNU General Public License for more details.
pommzorz 0:d84e1c7e4e86 26
pommzorz 0:d84e1c7e4e86 27 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
pommzorz 0:d84e1c7e4e86 28 */
pommzorz 0:d84e1c7e4e86 29
pommzorz 0:d84e1c7e4e86 30 //#define ISHMC5843 (1) // Uncomment this following line if you are using this library with the HMC5843.
pommzorz 0:d84e1c7e4e86 31
pommzorz 0:d84e1c7e4e86 32
pommzorz 0:d84e1c7e4e86 33
pommzorz 0:d84e1c7e4e86 34 #ifndef HMC58X3_h
pommzorz 0:d84e1c7e4e86 35 #define HMC58X3_h
pommzorz 0:d84e1c7e4e86 36
pommzorz 0:d84e1c7e4e86 37 #define HMC58X3_ADDR 0x1E // 7 bit address of the HMC58X3 used with the Wire library
pommzorz 0:d84e1c7e4e86 38 #define HMC_POS_BIAS 1
pommzorz 0:d84e1c7e4e86 39 #define HMC_NEG_BIAS 2
pommzorz 0:d84e1c7e4e86 40
pommzorz 0:d84e1c7e4e86 41 // HMC58X3 register map. For details see HMC58X3 datasheet
pommzorz 0:d84e1c7e4e86 42 #define HMC58X3_R_CONFA 0
pommzorz 0:d84e1c7e4e86 43 #define HMC58X3_R_CONFB 1
pommzorz 0:d84e1c7e4e86 44 #define HMC58X3_R_MODE 2
pommzorz 0:d84e1c7e4e86 45 #define HMC58X3_R_XM 3
pommzorz 0:d84e1c7e4e86 46 #define HMC58X3_R_XL 4
pommzorz 0:d84e1c7e4e86 47
pommzorz 0:d84e1c7e4e86 48 #ifdef ISHMC5843
pommzorz 0:d84e1c7e4e86 49 #define HMC58X3_R_YM (5) //!< Register address for YM.
pommzorz 0:d84e1c7e4e86 50 #define HMC58X3_R_YL (6) //!< Register address for YL.
pommzorz 0:d84e1c7e4e86 51 #define HMC58X3_R_ZM (7) //!< Register address for ZM.
pommzorz 0:d84e1c7e4e86 52 #define HMC58X3_R_ZL (8) //!< Register address for ZL.
pommzorz 0:d84e1c7e4e86 53
pommzorz 0:d84e1c7e4e86 54 #define HMC58X3_X_SELF_TEST_GAUSS (+0.55) //!< X axis level when bias current is applied.
pommzorz 0:d84e1c7e4e86 55 #define HMC58X3_Y_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS) //!< Y axis level when bias current is applied.
pommzorz 0:d84e1c7e4e86 56 #define HMC58X3_Z_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS) //!< Y axis level when bias current is applied.
pommzorz 0:d84e1c7e4e86 57
pommzorz 0:d84e1c7e4e86 58 /*
pommzorz 0:d84e1c7e4e86 59 This is my best guess at the LOW, HIGH limit. The data sheet does not have these values.
pommzorz 0:d84e1c7e4e86 60 */
pommzorz 0:d84e1c7e4e86 61 #define SELF_TEST_LOW_LIMIT (HMC58X3_X_SELF_TEST_GAUSS*0.53) //!< Low limit 53% of expected value.
pommzorz 0:d84e1c7e4e86 62 #define SELF_TEST_HIGH_LIMIT (HMC58X3_X_SELF_TEST_GAUSS*1.36) //!< High limit 136% of expected values.
pommzorz 0:d84e1c7e4e86 63 #else // HMC5883L
pommzorz 0:d84e1c7e4e86 64 #define HMC58X3_R_YM (7) //!< Register address for YM.
pommzorz 0:d84e1c7e4e86 65 #define HMC58X3_R_YL (8) //!< Register address for YL.
pommzorz 0:d84e1c7e4e86 66 #define HMC58X3_R_ZM (5) //!< Register address for ZM.
pommzorz 0:d84e1c7e4e86 67 #define HMC58X3_R_ZL (6) //!< Register address for ZL.
pommzorz 0:d84e1c7e4e86 68
pommzorz 0:d84e1c7e4e86 69 #define HMC58X3_X_SELF_TEST_GAUSS (+1.16) //!< X axis level when bias current is applied.
pommzorz 0:d84e1c7e4e86 70 #define HMC58X3_Y_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS) //!< Y axis level when bias current is applied.
pommzorz 0:d84e1c7e4e86 71 #define HMC58X3_Z_SELF_TEST_GAUSS (+1.08) //!< Y axis level when bias current is applied.
pommzorz 0:d84e1c7e4e86 72
pommzorz 0:d84e1c7e4e86 73 #define SELF_TEST_LOW_LIMIT (243.0/390.0) //!< Low limit when gain is 5.
pommzorz 0:d84e1c7e4e86 74 #define SELF_TEST_HIGH_LIMIT (575.0/390.0) //!< High limit when gain is 5.
pommzorz 0:d84e1c7e4e86 75 #endif
pommzorz 0:d84e1c7e4e86 76
pommzorz 0:d84e1c7e4e86 77 #define HMC58X3_R_STATUS 9
pommzorz 0:d84e1c7e4e86 78 #define HMC58X3_R_IDA 10
pommzorz 0:d84e1c7e4e86 79 #define HMC58X3_R_IDB 11
pommzorz 0:d84e1c7e4e86 80 #define HMC58X3_R_IDC 12
pommzorz 0:d84e1c7e4e86 81
tyftyftyf 2:c5ac16c88514 82 #define I2C_SDA p28
tyftyftyf 2:c5ac16c88514 83 #define I2C_SCL p27
tyftyftyf 2:c5ac16c88514 84
pommzorz 0:d84e1c7e4e86 85 class HMC58X3
pommzorz 0:d84e1c7e4e86 86 {
pommzorz 0:d84e1c7e4e86 87
pommzorz 0:d84e1c7e4e86 88
pommzorz 0:d84e1c7e4e86 89 public:
pommzorz 1:72ecf7399250 90 //HMC58X3(PinName sda, PinName scl);
tyftyftyf 2:c5ac16c88514 91 HMC58X3();
pommzorz 1:72ecf7399250 92 HMC58X3(I2C i2c_);
pommzorz 0:d84e1c7e4e86 93 void init(bool setmode);
pommzorz 0:d84e1c7e4e86 94 void init(int address, bool setmode);
tyftyftyf 2:c5ac16c88514 95 void getValues(int16_t *x,int16_t *y,int16_t *z);
pommzorz 0:d84e1c7e4e86 96 void getValues(float *x,float *y,float *z);
pommzorz 0:d84e1c7e4e86 97 void getValues(float *xyz);
tyftyftyf 2:c5ac16c88514 98 void getRaw(int16_t *x,int16_t *y,int16_t *z);
tyftyftyf 2:c5ac16c88514 99 void getRaw(int16_t *xyz);
pommzorz 0:d84e1c7e4e86 100 void calibrate(unsigned char gain); // Original calibrate with a few weaknesses.
pommzorz 0:d84e1c7e4e86 101 bool calibrate(unsigned char gain,unsigned int n_samples);
pommzorz 0:d84e1c7e4e86 102 void setMode(unsigned char mode);
pommzorz 0:d84e1c7e4e86 103 void setDOR(unsigned char DOR);
pommzorz 0:d84e1c7e4e86 104 void setGain(unsigned char gain);
pommzorz 0:d84e1c7e4e86 105 void getID(char id[3]);
pommzorz 0:d84e1c7e4e86 106
pommzorz 0:d84e1c7e4e86 107 static const int I2C_ADDRESS = 0x3D;
pommzorz 0:d84e1c7e4e86 108
pommzorz 0:d84e1c7e4e86 109 private:
pommzorz 0:d84e1c7e4e86 110 I2C i2c;
pommzorz 0:d84e1c7e4e86 111 void writeReg(unsigned char reg, unsigned char val);
pommzorz 0:d84e1c7e4e86 112 float x_scale,y_scale,z_scale,x_max,y_max,z_max;
pommzorz 0:d84e1c7e4e86 113 int min(int a, int b);
pommzorz 0:d84e1c7e4e86 114 };
pommzorz 0:d84e1c7e4e86 115
pommzorz 0:d84e1c7e4e86 116 #endif // HMC58X3_h
pommzorz 0:d84e1c7e4e86 117