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

Committer:
tyftyftyf
Date:
Wed Apr 18 20:24:06 2018 +0000
Revision:
8:ca00fa66673a
Parent:
7:5279fb447af1
change to I2C 1

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
tyftyftyf 7:5279fb447af1 37 #include "mbed.h"
tyftyftyf 7:5279fb447af1 38 #include "MODI2C.h"
tyftyftyf 7:5279fb447af1 39
tyftyftyf 4:8eb12adc8368 40 #ifndef I2C_SDA
tyftyftyf 8:ca00fa66673a 41 #define I2C_SDA p9
tyftyftyf 8:ca00fa66673a 42 #define I2C_SCL p10
tyftyftyf 4:8eb12adc8368 43 #endif
tyftyftyf 4:8eb12adc8368 44
pommzorz 0:d84e1c7e4e86 45 #define HMC58X3_ADDR 0x1E // 7 bit address of the HMC58X3 used with the Wire library
pommzorz 0:d84e1c7e4e86 46 #define HMC_POS_BIAS 1
pommzorz 0:d84e1c7e4e86 47 #define HMC_NEG_BIAS 2
pommzorz 0:d84e1c7e4e86 48
pommzorz 0:d84e1c7e4e86 49 // HMC58X3 register map. For details see HMC58X3 datasheet
pommzorz 0:d84e1c7e4e86 50 #define HMC58X3_R_CONFA 0
pommzorz 0:d84e1c7e4e86 51 #define HMC58X3_R_CONFB 1
pommzorz 0:d84e1c7e4e86 52 #define HMC58X3_R_MODE 2
pommzorz 0:d84e1c7e4e86 53 #define HMC58X3_R_XM 3
pommzorz 0:d84e1c7e4e86 54 #define HMC58X3_R_XL 4
pommzorz 0:d84e1c7e4e86 55
pommzorz 0:d84e1c7e4e86 56 #ifdef ISHMC5843
pommzorz 0:d84e1c7e4e86 57 #define HMC58X3_R_YM (5) //!< Register address for YM.
pommzorz 0:d84e1c7e4e86 58 #define HMC58X3_R_YL (6) //!< Register address for YL.
pommzorz 0:d84e1c7e4e86 59 #define HMC58X3_R_ZM (7) //!< Register address for ZM.
pommzorz 0:d84e1c7e4e86 60 #define HMC58X3_R_ZL (8) //!< Register address for ZL.
pommzorz 0:d84e1c7e4e86 61
pommzorz 0:d84e1c7e4e86 62 #define HMC58X3_X_SELF_TEST_GAUSS (+0.55) //!< X axis level when bias current is applied.
pommzorz 0:d84e1c7e4e86 63 #define HMC58X3_Y_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS) //!< Y axis level when bias current is applied.
pommzorz 0:d84e1c7e4e86 64 #define HMC58X3_Z_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS) //!< Y axis level when bias current is applied.
pommzorz 0:d84e1c7e4e86 65
pommzorz 0:d84e1c7e4e86 66 /*
pommzorz 0:d84e1c7e4e86 67 This is my best guess at the LOW, HIGH limit. The data sheet does not have these values.
pommzorz 0:d84e1c7e4e86 68 */
pommzorz 0:d84e1c7e4e86 69 #define SELF_TEST_LOW_LIMIT (HMC58X3_X_SELF_TEST_GAUSS*0.53) //!< Low limit 53% of expected value.
pommzorz 0:d84e1c7e4e86 70 #define SELF_TEST_HIGH_LIMIT (HMC58X3_X_SELF_TEST_GAUSS*1.36) //!< High limit 136% of expected values.
pommzorz 0:d84e1c7e4e86 71 #else // HMC5883L
pommzorz 0:d84e1c7e4e86 72 #define HMC58X3_R_YM (7) //!< Register address for YM.
pommzorz 0:d84e1c7e4e86 73 #define HMC58X3_R_YL (8) //!< Register address for YL.
pommzorz 0:d84e1c7e4e86 74 #define HMC58X3_R_ZM (5) //!< Register address for ZM.
pommzorz 0:d84e1c7e4e86 75 #define HMC58X3_R_ZL (6) //!< Register address for ZL.
pommzorz 0:d84e1c7e4e86 76
pommzorz 0:d84e1c7e4e86 77 #define HMC58X3_X_SELF_TEST_GAUSS (+1.16) //!< X axis level when bias current is applied.
pommzorz 0:d84e1c7e4e86 78 #define HMC58X3_Y_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS) //!< Y axis level when bias current is applied.
pommzorz 0:d84e1c7e4e86 79 #define HMC58X3_Z_SELF_TEST_GAUSS (+1.08) //!< Y axis level when bias current is applied.
pommzorz 0:d84e1c7e4e86 80
pommzorz 0:d84e1c7e4e86 81 #define SELF_TEST_LOW_LIMIT (243.0/390.0) //!< Low limit when gain is 5.
pommzorz 0:d84e1c7e4e86 82 #define SELF_TEST_HIGH_LIMIT (575.0/390.0) //!< High limit when gain is 5.
pommzorz 0:d84e1c7e4e86 83 #endif
pommzorz 0:d84e1c7e4e86 84
pommzorz 0:d84e1c7e4e86 85 #define HMC58X3_R_STATUS 9
tyftyftyf 4:8eb12adc8368 86 //#define HMC58X3_R_IDA 10
pommzorz 0:d84e1c7e4e86 87 #define HMC58X3_R_IDB 11
pommzorz 0:d84e1c7e4e86 88 #define HMC58X3_R_IDC 12
pommzorz 0:d84e1c7e4e86 89
pommzorz 0:d84e1c7e4e86 90 class HMC58X3
pommzorz 0:d84e1c7e4e86 91 {
pommzorz 0:d84e1c7e4e86 92
pommzorz 0:d84e1c7e4e86 93
pommzorz 0:d84e1c7e4e86 94 public:
tyftyftyf 4:8eb12adc8368 95 HMC58X3();
pommzorz 0:d84e1c7e4e86 96 void init(bool setmode);
pommzorz 0:d84e1c7e4e86 97 void init(int address, bool setmode);
tyftyftyf 2:c5ac16c88514 98 void getValues(int16_t *x,int16_t *y,int16_t *z);
pommzorz 0:d84e1c7e4e86 99 void getValues(float *x,float *y,float *z);
pommzorz 0:d84e1c7e4e86 100 void getValues(float *xyz);
tyftyftyf 2:c5ac16c88514 101 void getRaw(int16_t *x,int16_t *y,int16_t *z);
tyftyftyf 2:c5ac16c88514 102 void getRaw(int16_t *xyz);
pommzorz 0:d84e1c7e4e86 103 void calibrate(unsigned char gain); // Original calibrate with a few weaknesses.
pommzorz 0:d84e1c7e4e86 104 bool calibrate(unsigned char gain,unsigned int n_samples);
pommzorz 0:d84e1c7e4e86 105 void setMode(unsigned char mode);
pommzorz 0:d84e1c7e4e86 106 void setDOR(unsigned char DOR);
pommzorz 0:d84e1c7e4e86 107 void setGain(unsigned char gain);
pommzorz 0:d84e1c7e4e86 108 void getID(char id[3]);
tyftyftyf 4:8eb12adc8368 109
tyftyftyf 4:8eb12adc8368 110 int16_t cache_x, cache_y, cache_z;
tyftyftyf 4:8eb12adc8368 111
tyftyftyf 4:8eb12adc8368 112 static void samplingthread_stub(void const *p);
tyftyftyf 4:8eb12adc8368 113
tyftyftyf 4:8eb12adc8368 114 void samplingthread();
tyftyftyf 4:8eb12adc8368 115 void start_sampling();
pommzorz 0:d84e1c7e4e86 116
pommzorz 0:d84e1c7e4e86 117 static const int I2C_ADDRESS = 0x3D;
tyftyftyf 4:8eb12adc8368 118 char HMC58X3_R_IDA;
tyftyftyf 4:8eb12adc8368 119 MODI2C i2c;
tyftyftyf 4:8eb12adc8368 120
tyftyftyf 4:8eb12adc8368 121 Thread _thread;
tyftyftyf 4:8eb12adc8368 122
tyftyftyf 4:8eb12adc8368 123 Semaphore sem;
pommzorz 0:d84e1c7e4e86 124
pommzorz 0:d84e1c7e4e86 125 private:
pommzorz 0:d84e1c7e4e86 126 void writeReg(unsigned char reg, unsigned char val);
pommzorz 0:d84e1c7e4e86 127 float x_scale,y_scale,z_scale,x_max,y_max,z_max;
pommzorz 0:d84e1c7e4e86 128 int min(int a, int b);
pommzorz 0:d84e1c7e4e86 129 };
pommzorz 0:d84e1c7e4e86 130
pommzorz 0:d84e1c7e4e86 131 #endif // HMC58X3_h