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:
Tue Nov 05 11:30:31 2013 +0000
Revision:
3:1e0e0c47287a
Parent:
2:c5ac16c88514
Child:
4:8eb12adc8368
No empty constructor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pommzorz 0:d84e1c7e4e86 1 /*
pommzorz 0:d84e1c7e4e86 2 HMC58X3.cpp - Interface a Honeywell HMC58X3 or HMC5883L magnetometer to an mbed via i2c
pommzorz 0:d84e1c7e4e86 3 Copyright (C) 2011 Fabio Varesano <fvaresano@yahoo.it>
pommzorz 0:d84e1c7e4e86 4 ported for mbed by Aloïs Wolff (wolffalois@gmail.com)
pommzorz 0:d84e1c7e4e86 5
pommzorz 0:d84e1c7e4e86 6 Based on:
pommzorz 0:d84e1c7e4e86 7 http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1274748346
pommzorz 0:d84e1c7e4e86 8 Modification/extension of the following by E.J.Muller
pommzorz 0:d84e1c7e4e86 9 http://eclecti.cc/hardware/hmc5843-magnetometer-library-for-arduino
pommzorz 0:d84e1c7e4e86 10 Copyright (c) 2009 Nirav Patel,
pommzorz 0:d84e1c7e4e86 11
pommzorz 0:d84e1c7e4e86 12 The above were based on:
pommzorz 0:d84e1c7e4e86 13 http://www.sparkfun.com/datasheets/Sensors/Magneto/HMC58X3-v11.c
pommzorz 0:d84e1c7e4e86 14 http://www.atmel.com/dyn/resources/prod_documents/doc2545.pdf
pommzorz 0:d84e1c7e4e86 15
pommzorz 0:d84e1c7e4e86 16
pommzorz 0:d84e1c7e4e86 17 This program is free software: you can redistribute it and/or modify
pommzorz 0:d84e1c7e4e86 18 it under the terms of the version 3 GNU General Public License as
pommzorz 0:d84e1c7e4e86 19 published by the Free Software Foundation.
pommzorz 0:d84e1c7e4e86 20
pommzorz 0:d84e1c7e4e86 21 This program is distributed in the hope that it will be useful,
pommzorz 0:d84e1c7e4e86 22 but WITHOUT ANY WARRANTY; without even the implied warranty of
pommzorz 0:d84e1c7e4e86 23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
pommzorz 0:d84e1c7e4e86 24 GNU General Public License for more details.
pommzorz 0:d84e1c7e4e86 25
pommzorz 0:d84e1c7e4e86 26 You should have received a copy of the GNU General Public License
pommzorz 0:d84e1c7e4e86 27 along with this program. If not, see <http://www.gnu.org/licenses/>.
pommzorz 0:d84e1c7e4e86 28
pommzorz 0:d84e1c7e4e86 29 */
pommzorz 0:d84e1c7e4e86 30
pommzorz 0:d84e1c7e4e86 31 //#define DEBUG (1)
pommzorz 0:d84e1c7e4e86 32
pommzorz 0:d84e1c7e4e86 33 #include "mbed.h"
pommzorz 0:d84e1c7e4e86 34 #include "HMC58X3.h"
pommzorz 0:d84e1c7e4e86 35 #include <new>
pommzorz 0:d84e1c7e4e86 36 //#include <DebugUtils.h>
pommzorz 0:d84e1c7e4e86 37 #define DEBUG_PRINT
pommzorz 0:d84e1c7e4e86 38
pommzorz 0:d84e1c7e4e86 39
pommzorz 0:d84e1c7e4e86 40
pommzorz 0:d84e1c7e4e86 41
pommzorz 0:d84e1c7e4e86 42 /*!
pommzorz 0:d84e1c7e4e86 43 Counts/milli-gauss per gain for the self test bias current.
pommzorz 0:d84e1c7e4e86 44 */
tyftyftyf 2:c5ac16c88514 45
pommzorz 0:d84e1c7e4e86 46 const int counts_per_milligauss[8]= {
pommzorz 0:d84e1c7e4e86 47 1370,
pommzorz 0:d84e1c7e4e86 48 1090,
pommzorz 0:d84e1c7e4e86 49 820,
pommzorz 0:d84e1c7e4e86 50 660,
pommzorz 0:d84e1c7e4e86 51 440,
pommzorz 0:d84e1c7e4e86 52 390,
pommzorz 0:d84e1c7e4e86 53 330,
pommzorz 0:d84e1c7e4e86 54 230
pommzorz 0:d84e1c7e4e86 55 };
tyftyftyf 2:c5ac16c88514 56
pommzorz 0:d84e1c7e4e86 57
pommzorz 0:d84e1c7e4e86 58
pommzorz 0:d84e1c7e4e86 59
pommzorz 0:d84e1c7e4e86 60 /* PUBLIC METHODS */
pommzorz 0:d84e1c7e4e86 61
tyftyftyf 2:c5ac16c88514 62
pommzorz 1:72ecf7399250 63 //HMC58X3::HMC58X3(PinName sda, PinName scl): i2c(sda, scl)
pommzorz 1:72ecf7399250 64 HMC58X3::HMC58X3(I2C i2c_):i2c(i2c_)
pommzorz 0:d84e1c7e4e86 65 {
pommzorz 1:72ecf7399250 66 //this->i2c= i2c_;
pommzorz 0:d84e1c7e4e86 67 x_scale=1.0F;
pommzorz 0:d84e1c7e4e86 68 y_scale=1.0F;
pommzorz 0:d84e1c7e4e86 69 z_scale=1.0F;
pommzorz 0:d84e1c7e4e86 70
pommzorz 0:d84e1c7e4e86 71 }
pommzorz 0:d84e1c7e4e86 72
pommzorz 0:d84e1c7e4e86 73
pommzorz 0:d84e1c7e4e86 74 void HMC58X3::init(bool setmode)
pommzorz 0:d84e1c7e4e86 75 {
pommzorz 0:d84e1c7e4e86 76 // note that we don't initialize Wire here.
pommzorz 0:d84e1c7e4e86 77 // You'll have to do that in setup() in your Arduino program
tyftyftyf 2:c5ac16c88514 78 wait_ms(10); // you need to wait at least 5ms after power on to initialize
pommzorz 0:d84e1c7e4e86 79 if (setmode) {
pommzorz 0:d84e1c7e4e86 80 setMode(0);
pommzorz 0:d84e1c7e4e86 81 }
pommzorz 0:d84e1c7e4e86 82
pommzorz 0:d84e1c7e4e86 83
pommzorz 0:d84e1c7e4e86 84
pommzorz 0:d84e1c7e4e86 85 writeReg(HMC58X3_R_CONFA, 0x70); // 8 samples averaged, 75Hz frequency, no artificial bias.
pommzorz 0:d84e1c7e4e86 86 writeReg(HMC58X3_R_CONFB, 0xA0);
pommzorz 0:d84e1c7e4e86 87 writeReg(HMC58X3_R_MODE, 0x00);
pommzorz 0:d84e1c7e4e86 88 }
pommzorz 0:d84e1c7e4e86 89
pommzorz 0:d84e1c7e4e86 90
pommzorz 0:d84e1c7e4e86 91 void HMC58X3::setMode(unsigned char mode)
pommzorz 0:d84e1c7e4e86 92 {
pommzorz 0:d84e1c7e4e86 93 if (mode > 2) {
pommzorz 0:d84e1c7e4e86 94 return;
pommzorz 0:d84e1c7e4e86 95 }
pommzorz 0:d84e1c7e4e86 96
pommzorz 0:d84e1c7e4e86 97 writeReg(HMC58X3_R_MODE, mode);
pommzorz 0:d84e1c7e4e86 98 wait_ms(100);
pommzorz 0:d84e1c7e4e86 99 }
pommzorz 0:d84e1c7e4e86 100
pommzorz 0:d84e1c7e4e86 101 /*
pommzorz 0:d84e1c7e4e86 102 Calibrate which has a few weaknesses.
pommzorz 0:d84e1c7e4e86 103 1. Uses wrong gain for first reading.
pommzorz 0:d84e1c7e4e86 104 2. Uses max instead of max of average when normalizing the axis to one another.
pommzorz 0:d84e1c7e4e86 105 3. Doesn't use neg bias. (possible improvement in measurement).
pommzorz 0:d84e1c7e4e86 106 */
pommzorz 0:d84e1c7e4e86 107 void HMC58X3::calibrate(unsigned char gain)
pommzorz 0:d84e1c7e4e86 108 {
pommzorz 0:d84e1c7e4e86 109 x_scale=1; // get actual values
pommzorz 0:d84e1c7e4e86 110 y_scale=1;
pommzorz 0:d84e1c7e4e86 111 z_scale=1;
pommzorz 0:d84e1c7e4e86 112 writeReg(HMC58X3_R_CONFA, 0x010 + HMC_POS_BIAS); // Reg A DOR=0x010 + MS1,MS0 set to pos bias
pommzorz 0:d84e1c7e4e86 113 setGain(gain);
pommzorz 0:d84e1c7e4e86 114 float x, y, z, mx=0, my=0, mz=0, t=10;
pommzorz 0:d84e1c7e4e86 115
pommzorz 0:d84e1c7e4e86 116 for (int i=0; i<(int)t; i++) {
pommzorz 0:d84e1c7e4e86 117 setMode(1);
pommzorz 0:d84e1c7e4e86 118 getValues(&x,&y,&z);
pommzorz 0:d84e1c7e4e86 119 if (x>mx) mx=x;
pommzorz 0:d84e1c7e4e86 120 if (y>my) my=y;
pommzorz 0:d84e1c7e4e86 121 if (z>mz) mz=z;
pommzorz 0:d84e1c7e4e86 122 }
pommzorz 0:d84e1c7e4e86 123
pommzorz 0:d84e1c7e4e86 124 float max=0;
pommzorz 0:d84e1c7e4e86 125 if (mx>max) max=mx;
pommzorz 0:d84e1c7e4e86 126 if (my>max) max=my;
pommzorz 0:d84e1c7e4e86 127 if (mz>max) max=mz;
pommzorz 0:d84e1c7e4e86 128 x_max=mx;
pommzorz 0:d84e1c7e4e86 129 y_max=my;
pommzorz 0:d84e1c7e4e86 130 z_max=mz;
pommzorz 0:d84e1c7e4e86 131 x_scale=max/mx; // calc scales
pommzorz 0:d84e1c7e4e86 132 y_scale=max/my;
pommzorz 0:d84e1c7e4e86 133 z_scale=max/mz;
pommzorz 0:d84e1c7e4e86 134
pommzorz 0:d84e1c7e4e86 135 writeReg(HMC58X3_R_CONFA, 0x010); // set RegA/DOR back to default
pommzorz 0:d84e1c7e4e86 136 } // calibrate().
pommzorz 0:d84e1c7e4e86 137
pommzorz 0:d84e1c7e4e86 138 /*!
pommzorz 0:d84e1c7e4e86 139 \brief Calibrate using the self test operation.
pommzorz 0:d84e1c7e4e86 140
pommzorz 0:d84e1c7e4e86 141 Average the values using bias mode to obtain the scale factors.
pommzorz 0:d84e1c7e4e86 142
pommzorz 0:d84e1c7e4e86 143 \param gain [in] Gain setting for the sensor. See data sheet.
pommzorz 0:d84e1c7e4e86 144 \param n_samples [in] Number of samples to average together while applying the positive and negative bias.
pommzorz 0:d84e1c7e4e86 145 \return Returns false if any of the following occurs:
pommzorz 0:d84e1c7e4e86 146 # Invalid input parameters. (gain>7 or n_samples=0).
pommzorz 0:d84e1c7e4e86 147 # Id registers are wrong for the compiled device. Unfortunately, we can't distinguish between HMC5843 and HMC5883L.
pommzorz 0:d84e1c7e4e86 148 # Calibration saturates during the positive or negative bias on any of the readings.
pommzorz 0:d84e1c7e4e86 149 # Readings are outside of the expected range for bias current.
pommzorz 0:d84e1c7e4e86 150 */
pommzorz 0:d84e1c7e4e86 151 bool HMC58X3::calibrate(unsigned char gain,unsigned int n_samples)
pommzorz 0:d84e1c7e4e86 152 {
tyftyftyf 2:c5ac16c88514 153 int16_t xyz[3]; // 16 bit integer values for each axis.
tyftyftyf 2:c5ac16c88514 154 long xyz_total[3]= {0,0,0}; // 32 bit totals so they won't overflow.
pommzorz 0:d84e1c7e4e86 155 bool bret=true; // Function return value. Will return false if the wrong identifier is returned, saturation is detected or response is out of range to self test bias.
pommzorz 0:d84e1c7e4e86 156 char id[3]; // Three identification registers should return 'H43'.
tyftyftyf 2:c5ac16c88514 157 long low_limit, high_limit;
pommzorz 0:d84e1c7e4e86 158 /*
pommzorz 0:d84e1c7e4e86 159 Make sure we are talking to the correct device.
pommzorz 0:d84e1c7e4e86 160 Hard to believe Honeywell didn't change the identifier.
pommzorz 0:d84e1c7e4e86 161 */
pommzorz 0:d84e1c7e4e86 162 if ((8>gain) && (0<n_samples)) { // Notice this allows gain setting of 7 which the data sheet warns against.
pommzorz 0:d84e1c7e4e86 163 getID(id);
pommzorz 0:d84e1c7e4e86 164 if (('H' == id[0]) && ('4' == id[1]) && ('3' == id[2])) {
pommzorz 0:d84e1c7e4e86 165 /*
pommzorz 0:d84e1c7e4e86 166 Use the positive bias current to impose a known field on each axis.
pommzorz 0:d84e1c7e4e86 167 This field depends on the device and the axis.
pommzorz 0:d84e1c7e4e86 168 */
pommzorz 0:d84e1c7e4e86 169 writeReg(HMC58X3_R_CONFA, 0x010 + HMC_POS_BIAS); // Reg A DOR=0x010 + MS1,MS0 set to pos bias
pommzorz 0:d84e1c7e4e86 170 /*
pommzorz 0:d84e1c7e4e86 171 Note that the very first measurement after a gain change maintains the same gain as the previous setting.
pommzorz 0:d84e1c7e4e86 172 The new gain setting is effective from the second measurement and on.
pommzorz 0:d84e1c7e4e86 173 */
pommzorz 0:d84e1c7e4e86 174 setGain(gain);
pommzorz 0:d84e1c7e4e86 175 setMode(1); // Change to single measurement mode.
pommzorz 0:d84e1c7e4e86 176 getRaw(&xyz[0],&xyz[1],&xyz[2]); // Get the raw values and ignore since this reading may use previous gain.
pommzorz 0:d84e1c7e4e86 177
pommzorz 0:d84e1c7e4e86 178 for (unsigned int i=0; i<n_samples; i++) {
pommzorz 0:d84e1c7e4e86 179 setMode(1);
pommzorz 0:d84e1c7e4e86 180 getRaw(&xyz[0],&xyz[1],&xyz[2]); // Get the raw values in case the scales have already been changed.
pommzorz 0:d84e1c7e4e86 181 /*
pommzorz 0:d84e1c7e4e86 182 Since the measurements are noisy, they should be averaged rather than taking the max.
pommzorz 0:d84e1c7e4e86 183 */
pommzorz 0:d84e1c7e4e86 184 xyz_total[0]+=xyz[0];
pommzorz 0:d84e1c7e4e86 185 xyz_total[1]+=xyz[1];
pommzorz 0:d84e1c7e4e86 186 xyz_total[2]+=xyz[2];
pommzorz 0:d84e1c7e4e86 187 /*
pommzorz 0:d84e1c7e4e86 188 Detect saturation.
pommzorz 0:d84e1c7e4e86 189 */
pommzorz 0:d84e1c7e4e86 190 if (-(1<<12) >= min(xyz[0],min(xyz[1],xyz[2]))) {
pommzorz 0:d84e1c7e4e86 191 DEBUG_PRINT("HMC58x3 Self test saturated. Increase range.");
pommzorz 0:d84e1c7e4e86 192 bret=false;
pommzorz 0:d84e1c7e4e86 193 break; // Breaks out of the for loop. No sense in continuing if we saturated.
pommzorz 0:d84e1c7e4e86 194 }
pommzorz 0:d84e1c7e4e86 195 }
pommzorz 0:d84e1c7e4e86 196 /*
pommzorz 0:d84e1c7e4e86 197 Apply the negative bias. (Same gain)
pommzorz 0:d84e1c7e4e86 198 */
pommzorz 0:d84e1c7e4e86 199 writeReg(HMC58X3_R_CONFA, 0x010 + HMC_NEG_BIAS); // Reg A DOR=0x010 + MS1,MS0 set to negative bias.
pommzorz 0:d84e1c7e4e86 200 for (unsigned int i=0; i<n_samples; i++) {
pommzorz 0:d84e1c7e4e86 201 setMode(1);
pommzorz 0:d84e1c7e4e86 202 getRaw(&xyz[0],&xyz[1],&xyz[2]); // Get the raw values in case the scales have already been changed.
pommzorz 0:d84e1c7e4e86 203 /*
pommzorz 0:d84e1c7e4e86 204 Since the measurements are noisy, they should be averaged.
pommzorz 0:d84e1c7e4e86 205 */
pommzorz 0:d84e1c7e4e86 206 xyz_total[0]-=xyz[0];
pommzorz 0:d84e1c7e4e86 207 xyz_total[1]-=xyz[1];
pommzorz 0:d84e1c7e4e86 208 xyz_total[2]-=xyz[2];
pommzorz 0:d84e1c7e4e86 209 /*
pommzorz 0:d84e1c7e4e86 210 Detect saturation.
pommzorz 0:d84e1c7e4e86 211 */
pommzorz 0:d84e1c7e4e86 212 if (-(1<<12) >= min(xyz[0],min(xyz[1],xyz[2]))) {
pommzorz 0:d84e1c7e4e86 213 DEBUG_PRINT("HMC58x3 Self test saturated. Increase range.");
pommzorz 0:d84e1c7e4e86 214 bret=false;
pommzorz 0:d84e1c7e4e86 215 break; // Breaks out of the for loop. No sense in continuing if we saturated.
pommzorz 0:d84e1c7e4e86 216 }
pommzorz 0:d84e1c7e4e86 217 }
pommzorz 0:d84e1c7e4e86 218 /*
pommzorz 0:d84e1c7e4e86 219 Compare the values against the expected self test bias gauss.
pommzorz 0:d84e1c7e4e86 220 Notice, the same limits are applied to all axis.
pommzorz 0:d84e1c7e4e86 221 */
pommzorz 0:d84e1c7e4e86 222 low_limit =SELF_TEST_LOW_LIMIT *counts_per_milligauss[gain]*2*n_samples;
pommzorz 0:d84e1c7e4e86 223 high_limit=SELF_TEST_HIGH_LIMIT*counts_per_milligauss[gain]*2*n_samples;
pommzorz 0:d84e1c7e4e86 224
pommzorz 0:d84e1c7e4e86 225 if ((true==bret) &&
pommzorz 0:d84e1c7e4e86 226 (low_limit <= xyz_total[0]) && (high_limit >= xyz_total[0]) &&
pommzorz 0:d84e1c7e4e86 227 (low_limit <= xyz_total[1]) && (high_limit >= xyz_total[1]) &&
pommzorz 0:d84e1c7e4e86 228 (low_limit <= xyz_total[2]) && (high_limit >= xyz_total[2]) ) {
pommzorz 0:d84e1c7e4e86 229 /*
pommzorz 0:d84e1c7e4e86 230 Successful calibration.
pommzorz 0:d84e1c7e4e86 231 Normalize the scale factors so all axis return the same range of values for the bias field.
pommzorz 0:d84e1c7e4e86 232 Factor of 2 is from summation of total of n_samples from both positive and negative bias.
pommzorz 0:d84e1c7e4e86 233 */
pommzorz 0:d84e1c7e4e86 234 x_scale=(counts_per_milligauss[gain]*(HMC58X3_X_SELF_TEST_GAUSS*2))/(xyz_total[0]/n_samples);
pommzorz 0:d84e1c7e4e86 235 y_scale=(counts_per_milligauss[gain]*(HMC58X3_Y_SELF_TEST_GAUSS*2))/(xyz_total[1]/n_samples);
pommzorz 0:d84e1c7e4e86 236 z_scale=(counts_per_milligauss[gain]*(HMC58X3_Z_SELF_TEST_GAUSS*2))/(xyz_total[2]/n_samples);
pommzorz 0:d84e1c7e4e86 237 } else {
pommzorz 0:d84e1c7e4e86 238 DEBUG_PRINT("HMC58x3 Self test out of range.");
pommzorz 0:d84e1c7e4e86 239 bret=false;
pommzorz 0:d84e1c7e4e86 240 }
pommzorz 0:d84e1c7e4e86 241 writeReg(HMC58X3_R_CONFA, 0x010); // set RegA/DOR back to default.
pommzorz 0:d84e1c7e4e86 242 } else {
pommzorz 0:d84e1c7e4e86 243 #if defined(ISHMC5843)
pommzorz 0:d84e1c7e4e86 244 DEBUG_PRINT("HMC5843 failed id check.");
pommzorz 0:d84e1c7e4e86 245 #else
pommzorz 0:d84e1c7e4e86 246 DEBUG_PRINT("HMC5883L failed id check.");
pommzorz 0:d84e1c7e4e86 247 #endif
pommzorz 0:d84e1c7e4e86 248 bret=false;
pommzorz 0:d84e1c7e4e86 249 }
pommzorz 0:d84e1c7e4e86 250 } else {
pommzorz 0:d84e1c7e4e86 251 /*
pommzorz 0:d84e1c7e4e86 252 Bad input parameters.
pommzorz 0:d84e1c7e4e86 253 */
pommzorz 0:d84e1c7e4e86 254 DEBUG_PRINT("HMC58x3 Bad parameters.");
pommzorz 0:d84e1c7e4e86 255 bret=false;
pommzorz 0:d84e1c7e4e86 256 }
pommzorz 0:d84e1c7e4e86 257 return(bret);
pommzorz 0:d84e1c7e4e86 258 } // calibrate().
pommzorz 0:d84e1c7e4e86 259
pommzorz 0:d84e1c7e4e86 260 // set data output rate
pommzorz 0:d84e1c7e4e86 261 // 0-6, 4 default, normal operation assumed
pommzorz 0:d84e1c7e4e86 262 void HMC58X3::setDOR(unsigned char DOR)
pommzorz 0:d84e1c7e4e86 263 {
pommzorz 0:d84e1c7e4e86 264 if (DOR>6) return;
pommzorz 0:d84e1c7e4e86 265 writeReg(HMC58X3_R_CONFA,DOR<<2);
pommzorz 0:d84e1c7e4e86 266 }
pommzorz 0:d84e1c7e4e86 267
pommzorz 0:d84e1c7e4e86 268
pommzorz 0:d84e1c7e4e86 269 void HMC58X3::setGain(unsigned char gain)
pommzorz 0:d84e1c7e4e86 270 {
pommzorz 0:d84e1c7e4e86 271 // 0-7, 1 default
pommzorz 0:d84e1c7e4e86 272 if (gain > 7) return;
pommzorz 0:d84e1c7e4e86 273 writeReg(HMC58X3_R_CONFB, gain << 5);
pommzorz 0:d84e1c7e4e86 274 }
pommzorz 0:d84e1c7e4e86 275
pommzorz 0:d84e1c7e4e86 276
pommzorz 0:d84e1c7e4e86 277 void HMC58X3::writeReg(unsigned char reg, unsigned char val)
pommzorz 0:d84e1c7e4e86 278 {
pommzorz 0:d84e1c7e4e86 279 i2c.start();
tyftyftyf 2:c5ac16c88514 280 i2c.write(HMC58X3_ADDR<<1);
pommzorz 0:d84e1c7e4e86 281 i2c.write(reg); // send register address
pommzorz 0:d84e1c7e4e86 282 i2c.write(val); // send value to write
pommzorz 0:d84e1c7e4e86 283 i2c.stop(); //end transmission
pommzorz 0:d84e1c7e4e86 284 }
pommzorz 0:d84e1c7e4e86 285
pommzorz 0:d84e1c7e4e86 286
tyftyftyf 2:c5ac16c88514 287 void HMC58X3::getValues(int16_t *x,int16_t *y,int16_t *z)
pommzorz 0:d84e1c7e4e86 288 {
pommzorz 0:d84e1c7e4e86 289 float fx,fy,fz;
pommzorz 0:d84e1c7e4e86 290 getValues(&fx,&fy,&fz);
tyftyftyf 2:c5ac16c88514 291 *x= (int16_t) (fx + 0.5);
tyftyftyf 2:c5ac16c88514 292 *y= (int16_t) (fy + 0.5);
tyftyftyf 2:c5ac16c88514 293 *z= (int16_t) (fz + 0.5);
pommzorz 0:d84e1c7e4e86 294 }
pommzorz 0:d84e1c7e4e86 295
pommzorz 0:d84e1c7e4e86 296
pommzorz 0:d84e1c7e4e86 297 void HMC58X3::getValues(float *x,float *y,float *z)
pommzorz 0:d84e1c7e4e86 298 {
tyftyftyf 2:c5ac16c88514 299 int16_t xr,yr,zr;
pommzorz 0:d84e1c7e4e86 300
pommzorz 0:d84e1c7e4e86 301 getRaw(&xr, &yr, &zr);
pommzorz 0:d84e1c7e4e86 302 *x= ((float) xr) / x_scale;
pommzorz 0:d84e1c7e4e86 303 *y = ((float) yr) / y_scale;
pommzorz 0:d84e1c7e4e86 304 *z = ((float) zr) / z_scale;
pommzorz 0:d84e1c7e4e86 305 }
pommzorz 0:d84e1c7e4e86 306
pommzorz 0:d84e1c7e4e86 307
tyftyftyf 2:c5ac16c88514 308 void HMC58X3::getRaw(int16_t *x,int16_t *y,int16_t *z)
pommzorz 0:d84e1c7e4e86 309 {
pommzorz 0:d84e1c7e4e86 310
pommzorz 0:d84e1c7e4e86 311 char cmd[2];
pommzorz 0:d84e1c7e4e86 312 char data[6];
pommzorz 0:d84e1c7e4e86 313 cmd[0] = 0x03;
pommzorz 0:d84e1c7e4e86 314
tyftyftyf 2:c5ac16c88514 315 i2c.write(HMC58X3_ADDR<<1, cmd, 1, true); // set the pointer to the start of x
tyftyftyf 2:c5ac16c88514 316 i2c.read((HMC58X3_ADDR<<1)+1, data, 6, false);
pommzorz 0:d84e1c7e4e86 317
pommzorz 0:d84e1c7e4e86 318 // read out the 3 values, 2 bytes each.
pommzorz 0:d84e1c7e4e86 319 *x = int16_t(((unsigned char)data[0] << 8) | (unsigned char)data[1]);
pommzorz 0:d84e1c7e4e86 320 #ifdef ISHMC5843
pommzorz 0:d84e1c7e4e86 321 *y = int16_t(((unsigned char)data[1*2] << 8) | (unsigned char)data[1*2+1]);
pommzorz 0:d84e1c7e4e86 322 *z = int16_t(((unsigned char)data[2*2] << 8) | (unsigned char)data[2*2+1]);
pommzorz 0:d84e1c7e4e86 323 #else // the Z registers comes before the Y registers in the HMC5883L
pommzorz 0:d84e1c7e4e86 324 *z = int16_t(((unsigned char)data[1*2] << 8) | (unsigned char)data[1*2+1]);
pommzorz 0:d84e1c7e4e86 325 *y = int16_t(((unsigned char)data[2*2] << 8) | (unsigned char)data[2*2+1]);
pommzorz 0:d84e1c7e4e86 326 #endif
pommzorz 0:d84e1c7e4e86 327 // the HMC58X3 will automatically wrap around on the next request
pommzorz 0:d84e1c7e4e86 328
pommzorz 0:d84e1c7e4e86 329
pommzorz 0:d84e1c7e4e86 330 }
pommzorz 0:d84e1c7e4e86 331
pommzorz 0:d84e1c7e4e86 332
pommzorz 0:d84e1c7e4e86 333 void HMC58X3::getValues(float *xyz)
pommzorz 0:d84e1c7e4e86 334 {
pommzorz 0:d84e1c7e4e86 335 getValues(&xyz[0], &xyz[1], &xyz[2]);
pommzorz 0:d84e1c7e4e86 336 }
pommzorz 0:d84e1c7e4e86 337
pommzorz 0:d84e1c7e4e86 338 /*!
pommzorz 0:d84e1c7e4e86 339 \brief Retrieve the value of the three ID registers.
pommzorz 0:d84e1c7e4e86 340
pommzorz 0:d84e1c7e4e86 341 Note: Both the HMC5843 and HMC5883L have the same 'H43' identification register values. (Looks like someone at Honeywell screwed up.)
pommzorz 0:d84e1c7e4e86 342
pommzorz 0:d84e1c7e4e86 343 \param id [out] Returns the three id register values.
pommzorz 0:d84e1c7e4e86 344 */
pommzorz 0:d84e1c7e4e86 345 void HMC58X3::getID(char id[3])
pommzorz 0:d84e1c7e4e86 346 {
pommzorz 0:d84e1c7e4e86 347 i2c.start();
tyftyftyf 2:c5ac16c88514 348 i2c.write(HMC58X3_ADDR<<1);
pommzorz 0:d84e1c7e4e86 349 i2c.write(HMC58X3_R_IDA); // Will start reading registers starting from Identification Register A.
pommzorz 0:d84e1c7e4e86 350
tyftyftyf 2:c5ac16c88514 351 i2c.start();
tyftyftyf 2:c5ac16c88514 352 i2c.write((HMC58X3_ADDR<<1)+1);
pommzorz 0:d84e1c7e4e86 353 id[0] = i2c.read(0);
pommzorz 0:d84e1c7e4e86 354 id[1] = i2c.read(0);
pommzorz 0:d84e1c7e4e86 355 id[2] = i2c.read(0);
pommzorz 0:d84e1c7e4e86 356
pommzorz 0:d84e1c7e4e86 357 i2c.stop();
pommzorz 0:d84e1c7e4e86 358 } // getID().
pommzorz 0:d84e1c7e4e86 359
pommzorz 0:d84e1c7e4e86 360 int HMC58X3::min (int a, int b)
pommzorz 0:d84e1c7e4e86 361 {
pommzorz 0:d84e1c7e4e86 362 return !(b<a)?a:b; // or: return !comp(b,a)?a:b; for version (2)
pommzorz 0:d84e1c7e4e86 363 }