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:
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 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 HMC58X3::HMC58X3():i2c(I2C_SDA,I2C_SCL){
tyftyftyf 2:c5ac16c88514 63 }
tyftyftyf 2:c5ac16c88514 64
pommzorz 1:72ecf7399250 65 //HMC58X3::HMC58X3(PinName sda, PinName scl): i2c(sda, scl)
pommzorz 1:72ecf7399250 66 HMC58X3::HMC58X3(I2C i2c_):i2c(i2c_)
pommzorz 0:d84e1c7e4e86 67 {
pommzorz 1:72ecf7399250 68 //this->i2c= i2c_;
pommzorz 0:d84e1c7e4e86 69 x_scale=1.0F;
pommzorz 0:d84e1c7e4e86 70 y_scale=1.0F;
pommzorz 0:d84e1c7e4e86 71 z_scale=1.0F;
pommzorz 0:d84e1c7e4e86 72
pommzorz 0:d84e1c7e4e86 73 }
pommzorz 0:d84e1c7e4e86 74
pommzorz 0:d84e1c7e4e86 75
pommzorz 0:d84e1c7e4e86 76 void HMC58X3::init(bool setmode)
pommzorz 0:d84e1c7e4e86 77 {
pommzorz 0:d84e1c7e4e86 78 // note that we don't initialize Wire here.
pommzorz 0:d84e1c7e4e86 79 // You'll have to do that in setup() in your Arduino program
tyftyftyf 2:c5ac16c88514 80 wait_ms(10); // you need to wait at least 5ms after power on to initialize
pommzorz 0:d84e1c7e4e86 81 if (setmode) {
pommzorz 0:d84e1c7e4e86 82 setMode(0);
pommzorz 0:d84e1c7e4e86 83 }
pommzorz 0:d84e1c7e4e86 84
pommzorz 0:d84e1c7e4e86 85
pommzorz 0:d84e1c7e4e86 86
pommzorz 0:d84e1c7e4e86 87 writeReg(HMC58X3_R_CONFA, 0x70); // 8 samples averaged, 75Hz frequency, no artificial bias.
pommzorz 0:d84e1c7e4e86 88 writeReg(HMC58X3_R_CONFB, 0xA0);
pommzorz 0:d84e1c7e4e86 89 writeReg(HMC58X3_R_MODE, 0x00);
pommzorz 0:d84e1c7e4e86 90 }
pommzorz 0:d84e1c7e4e86 91
pommzorz 0:d84e1c7e4e86 92
pommzorz 0:d84e1c7e4e86 93 void HMC58X3::setMode(unsigned char mode)
pommzorz 0:d84e1c7e4e86 94 {
pommzorz 0:d84e1c7e4e86 95 if (mode > 2) {
pommzorz 0:d84e1c7e4e86 96 return;
pommzorz 0:d84e1c7e4e86 97 }
pommzorz 0:d84e1c7e4e86 98
pommzorz 0:d84e1c7e4e86 99 writeReg(HMC58X3_R_MODE, mode);
pommzorz 0:d84e1c7e4e86 100 wait_ms(100);
pommzorz 0:d84e1c7e4e86 101 }
pommzorz 0:d84e1c7e4e86 102
pommzorz 0:d84e1c7e4e86 103 /*
pommzorz 0:d84e1c7e4e86 104 Calibrate which has a few weaknesses.
pommzorz 0:d84e1c7e4e86 105 1. Uses wrong gain for first reading.
pommzorz 0:d84e1c7e4e86 106 2. Uses max instead of max of average when normalizing the axis to one another.
pommzorz 0:d84e1c7e4e86 107 3. Doesn't use neg bias. (possible improvement in measurement).
pommzorz 0:d84e1c7e4e86 108 */
pommzorz 0:d84e1c7e4e86 109 void HMC58X3::calibrate(unsigned char gain)
pommzorz 0:d84e1c7e4e86 110 {
pommzorz 0:d84e1c7e4e86 111 x_scale=1; // get actual values
pommzorz 0:d84e1c7e4e86 112 y_scale=1;
pommzorz 0:d84e1c7e4e86 113 z_scale=1;
pommzorz 0:d84e1c7e4e86 114 writeReg(HMC58X3_R_CONFA, 0x010 + HMC_POS_BIAS); // Reg A DOR=0x010 + MS1,MS0 set to pos bias
pommzorz 0:d84e1c7e4e86 115 setGain(gain);
pommzorz 0:d84e1c7e4e86 116 float x, y, z, mx=0, my=0, mz=0, t=10;
pommzorz 0:d84e1c7e4e86 117
pommzorz 0:d84e1c7e4e86 118 for (int i=0; i<(int)t; i++) {
pommzorz 0:d84e1c7e4e86 119 setMode(1);
pommzorz 0:d84e1c7e4e86 120 getValues(&x,&y,&z);
pommzorz 0:d84e1c7e4e86 121 if (x>mx) mx=x;
pommzorz 0:d84e1c7e4e86 122 if (y>my) my=y;
pommzorz 0:d84e1c7e4e86 123 if (z>mz) mz=z;
pommzorz 0:d84e1c7e4e86 124 }
pommzorz 0:d84e1c7e4e86 125
pommzorz 0:d84e1c7e4e86 126 float max=0;
pommzorz 0:d84e1c7e4e86 127 if (mx>max) max=mx;
pommzorz 0:d84e1c7e4e86 128 if (my>max) max=my;
pommzorz 0:d84e1c7e4e86 129 if (mz>max) max=mz;
pommzorz 0:d84e1c7e4e86 130 x_max=mx;
pommzorz 0:d84e1c7e4e86 131 y_max=my;
pommzorz 0:d84e1c7e4e86 132 z_max=mz;
pommzorz 0:d84e1c7e4e86 133 x_scale=max/mx; // calc scales
pommzorz 0:d84e1c7e4e86 134 y_scale=max/my;
pommzorz 0:d84e1c7e4e86 135 z_scale=max/mz;
pommzorz 0:d84e1c7e4e86 136
pommzorz 0:d84e1c7e4e86 137 writeReg(HMC58X3_R_CONFA, 0x010); // set RegA/DOR back to default
pommzorz 0:d84e1c7e4e86 138 } // calibrate().
pommzorz 0:d84e1c7e4e86 139
pommzorz 0:d84e1c7e4e86 140 /*!
pommzorz 0:d84e1c7e4e86 141 \brief Calibrate using the self test operation.
pommzorz 0:d84e1c7e4e86 142
pommzorz 0:d84e1c7e4e86 143 Average the values using bias mode to obtain the scale factors.
pommzorz 0:d84e1c7e4e86 144
pommzorz 0:d84e1c7e4e86 145 \param gain [in] Gain setting for the sensor. See data sheet.
pommzorz 0:d84e1c7e4e86 146 \param n_samples [in] Number of samples to average together while applying the positive and negative bias.
pommzorz 0:d84e1c7e4e86 147 \return Returns false if any of the following occurs:
pommzorz 0:d84e1c7e4e86 148 # Invalid input parameters. (gain>7 or n_samples=0).
pommzorz 0:d84e1c7e4e86 149 # Id registers are wrong for the compiled device. Unfortunately, we can't distinguish between HMC5843 and HMC5883L.
pommzorz 0:d84e1c7e4e86 150 # Calibration saturates during the positive or negative bias on any of the readings.
pommzorz 0:d84e1c7e4e86 151 # Readings are outside of the expected range for bias current.
pommzorz 0:d84e1c7e4e86 152 */
pommzorz 0:d84e1c7e4e86 153 bool HMC58X3::calibrate(unsigned char gain,unsigned int n_samples)
pommzorz 0:d84e1c7e4e86 154 {
tyftyftyf 2:c5ac16c88514 155 int16_t xyz[3]; // 16 bit integer values for each axis.
tyftyftyf 2:c5ac16c88514 156 long xyz_total[3]= {0,0,0}; // 32 bit totals so they won't overflow.
pommzorz 0:d84e1c7e4e86 157 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 158 char id[3]; // Three identification registers should return 'H43'.
tyftyftyf 2:c5ac16c88514 159 long low_limit, high_limit;
pommzorz 0:d84e1c7e4e86 160 /*
pommzorz 0:d84e1c7e4e86 161 Make sure we are talking to the correct device.
pommzorz 0:d84e1c7e4e86 162 Hard to believe Honeywell didn't change the identifier.
pommzorz 0:d84e1c7e4e86 163 */
pommzorz 0:d84e1c7e4e86 164 if ((8>gain) && (0<n_samples)) { // Notice this allows gain setting of 7 which the data sheet warns against.
pommzorz 0:d84e1c7e4e86 165 getID(id);
pommzorz 0:d84e1c7e4e86 166 if (('H' == id[0]) && ('4' == id[1]) && ('3' == id[2])) {
pommzorz 0:d84e1c7e4e86 167 /*
pommzorz 0:d84e1c7e4e86 168 Use the positive bias current to impose a known field on each axis.
pommzorz 0:d84e1c7e4e86 169 This field depends on the device and the axis.
pommzorz 0:d84e1c7e4e86 170 */
pommzorz 0:d84e1c7e4e86 171 writeReg(HMC58X3_R_CONFA, 0x010 + HMC_POS_BIAS); // Reg A DOR=0x010 + MS1,MS0 set to pos bias
pommzorz 0:d84e1c7e4e86 172 /*
pommzorz 0:d84e1c7e4e86 173 Note that the very first measurement after a gain change maintains the same gain as the previous setting.
pommzorz 0:d84e1c7e4e86 174 The new gain setting is effective from the second measurement and on.
pommzorz 0:d84e1c7e4e86 175 */
pommzorz 0:d84e1c7e4e86 176 setGain(gain);
pommzorz 0:d84e1c7e4e86 177 setMode(1); // Change to single measurement mode.
pommzorz 0:d84e1c7e4e86 178 getRaw(&xyz[0],&xyz[1],&xyz[2]); // Get the raw values and ignore since this reading may use previous gain.
pommzorz 0:d84e1c7e4e86 179
pommzorz 0:d84e1c7e4e86 180 for (unsigned int i=0; i<n_samples; i++) {
pommzorz 0:d84e1c7e4e86 181 setMode(1);
pommzorz 0:d84e1c7e4e86 182 getRaw(&xyz[0],&xyz[1],&xyz[2]); // Get the raw values in case the scales have already been changed.
pommzorz 0:d84e1c7e4e86 183 /*
pommzorz 0:d84e1c7e4e86 184 Since the measurements are noisy, they should be averaged rather than taking the max.
pommzorz 0:d84e1c7e4e86 185 */
pommzorz 0:d84e1c7e4e86 186 xyz_total[0]+=xyz[0];
pommzorz 0:d84e1c7e4e86 187 xyz_total[1]+=xyz[1];
pommzorz 0:d84e1c7e4e86 188 xyz_total[2]+=xyz[2];
pommzorz 0:d84e1c7e4e86 189 /*
pommzorz 0:d84e1c7e4e86 190 Detect saturation.
pommzorz 0:d84e1c7e4e86 191 */
pommzorz 0:d84e1c7e4e86 192 if (-(1<<12) >= min(xyz[0],min(xyz[1],xyz[2]))) {
pommzorz 0:d84e1c7e4e86 193 DEBUG_PRINT("HMC58x3 Self test saturated. Increase range.");
pommzorz 0:d84e1c7e4e86 194 bret=false;
pommzorz 0:d84e1c7e4e86 195 break; // Breaks out of the for loop. No sense in continuing if we saturated.
pommzorz 0:d84e1c7e4e86 196 }
pommzorz 0:d84e1c7e4e86 197 }
pommzorz 0:d84e1c7e4e86 198 /*
pommzorz 0:d84e1c7e4e86 199 Apply the negative bias. (Same gain)
pommzorz 0:d84e1c7e4e86 200 */
pommzorz 0:d84e1c7e4e86 201 writeReg(HMC58X3_R_CONFA, 0x010 + HMC_NEG_BIAS); // Reg A DOR=0x010 + MS1,MS0 set to negative bias.
pommzorz 0:d84e1c7e4e86 202 for (unsigned int i=0; i<n_samples; i++) {
pommzorz 0:d84e1c7e4e86 203 setMode(1);
pommzorz 0:d84e1c7e4e86 204 getRaw(&xyz[0],&xyz[1],&xyz[2]); // Get the raw values in case the scales have already been changed.
pommzorz 0:d84e1c7e4e86 205 /*
pommzorz 0:d84e1c7e4e86 206 Since the measurements are noisy, they should be averaged.
pommzorz 0:d84e1c7e4e86 207 */
pommzorz 0:d84e1c7e4e86 208 xyz_total[0]-=xyz[0];
pommzorz 0:d84e1c7e4e86 209 xyz_total[1]-=xyz[1];
pommzorz 0:d84e1c7e4e86 210 xyz_total[2]-=xyz[2];
pommzorz 0:d84e1c7e4e86 211 /*
pommzorz 0:d84e1c7e4e86 212 Detect saturation.
pommzorz 0:d84e1c7e4e86 213 */
pommzorz 0:d84e1c7e4e86 214 if (-(1<<12) >= min(xyz[0],min(xyz[1],xyz[2]))) {
pommzorz 0:d84e1c7e4e86 215 DEBUG_PRINT("HMC58x3 Self test saturated. Increase range.");
pommzorz 0:d84e1c7e4e86 216 bret=false;
pommzorz 0:d84e1c7e4e86 217 break; // Breaks out of the for loop. No sense in continuing if we saturated.
pommzorz 0:d84e1c7e4e86 218 }
pommzorz 0:d84e1c7e4e86 219 }
pommzorz 0:d84e1c7e4e86 220 /*
pommzorz 0:d84e1c7e4e86 221 Compare the values against the expected self test bias gauss.
pommzorz 0:d84e1c7e4e86 222 Notice, the same limits are applied to all axis.
pommzorz 0:d84e1c7e4e86 223 */
pommzorz 0:d84e1c7e4e86 224 low_limit =SELF_TEST_LOW_LIMIT *counts_per_milligauss[gain]*2*n_samples;
pommzorz 0:d84e1c7e4e86 225 high_limit=SELF_TEST_HIGH_LIMIT*counts_per_milligauss[gain]*2*n_samples;
pommzorz 0:d84e1c7e4e86 226
pommzorz 0:d84e1c7e4e86 227 if ((true==bret) &&
pommzorz 0:d84e1c7e4e86 228 (low_limit <= xyz_total[0]) && (high_limit >= xyz_total[0]) &&
pommzorz 0:d84e1c7e4e86 229 (low_limit <= xyz_total[1]) && (high_limit >= xyz_total[1]) &&
pommzorz 0:d84e1c7e4e86 230 (low_limit <= xyz_total[2]) && (high_limit >= xyz_total[2]) ) {
pommzorz 0:d84e1c7e4e86 231 /*
pommzorz 0:d84e1c7e4e86 232 Successful calibration.
pommzorz 0:d84e1c7e4e86 233 Normalize the scale factors so all axis return the same range of values for the bias field.
pommzorz 0:d84e1c7e4e86 234 Factor of 2 is from summation of total of n_samples from both positive and negative bias.
pommzorz 0:d84e1c7e4e86 235 */
pommzorz 0:d84e1c7e4e86 236 x_scale=(counts_per_milligauss[gain]*(HMC58X3_X_SELF_TEST_GAUSS*2))/(xyz_total[0]/n_samples);
pommzorz 0:d84e1c7e4e86 237 y_scale=(counts_per_milligauss[gain]*(HMC58X3_Y_SELF_TEST_GAUSS*2))/(xyz_total[1]/n_samples);
pommzorz 0:d84e1c7e4e86 238 z_scale=(counts_per_milligauss[gain]*(HMC58X3_Z_SELF_TEST_GAUSS*2))/(xyz_total[2]/n_samples);
pommzorz 0:d84e1c7e4e86 239 } else {
pommzorz 0:d84e1c7e4e86 240 DEBUG_PRINT("HMC58x3 Self test out of range.");
pommzorz 0:d84e1c7e4e86 241 bret=false;
pommzorz 0:d84e1c7e4e86 242 }
pommzorz 0:d84e1c7e4e86 243 writeReg(HMC58X3_R_CONFA, 0x010); // set RegA/DOR back to default.
pommzorz 0:d84e1c7e4e86 244 } else {
pommzorz 0:d84e1c7e4e86 245 #if defined(ISHMC5843)
pommzorz 0:d84e1c7e4e86 246 DEBUG_PRINT("HMC5843 failed id check.");
pommzorz 0:d84e1c7e4e86 247 #else
pommzorz 0:d84e1c7e4e86 248 DEBUG_PRINT("HMC5883L failed id check.");
pommzorz 0:d84e1c7e4e86 249 #endif
pommzorz 0:d84e1c7e4e86 250 bret=false;
pommzorz 0:d84e1c7e4e86 251 }
pommzorz 0:d84e1c7e4e86 252 } else {
pommzorz 0:d84e1c7e4e86 253 /*
pommzorz 0:d84e1c7e4e86 254 Bad input parameters.
pommzorz 0:d84e1c7e4e86 255 */
pommzorz 0:d84e1c7e4e86 256 DEBUG_PRINT("HMC58x3 Bad parameters.");
pommzorz 0:d84e1c7e4e86 257 bret=false;
pommzorz 0:d84e1c7e4e86 258 }
pommzorz 0:d84e1c7e4e86 259 return(bret);
pommzorz 0:d84e1c7e4e86 260 } // calibrate().
pommzorz 0:d84e1c7e4e86 261
pommzorz 0:d84e1c7e4e86 262 // set data output rate
pommzorz 0:d84e1c7e4e86 263 // 0-6, 4 default, normal operation assumed
pommzorz 0:d84e1c7e4e86 264 void HMC58X3::setDOR(unsigned char DOR)
pommzorz 0:d84e1c7e4e86 265 {
pommzorz 0:d84e1c7e4e86 266 if (DOR>6) return;
pommzorz 0:d84e1c7e4e86 267 writeReg(HMC58X3_R_CONFA,DOR<<2);
pommzorz 0:d84e1c7e4e86 268 }
pommzorz 0:d84e1c7e4e86 269
pommzorz 0:d84e1c7e4e86 270
pommzorz 0:d84e1c7e4e86 271 void HMC58X3::setGain(unsigned char gain)
pommzorz 0:d84e1c7e4e86 272 {
pommzorz 0:d84e1c7e4e86 273 // 0-7, 1 default
pommzorz 0:d84e1c7e4e86 274 if (gain > 7) return;
pommzorz 0:d84e1c7e4e86 275 writeReg(HMC58X3_R_CONFB, gain << 5);
pommzorz 0:d84e1c7e4e86 276 }
pommzorz 0:d84e1c7e4e86 277
pommzorz 0:d84e1c7e4e86 278
pommzorz 0:d84e1c7e4e86 279 void HMC58X3::writeReg(unsigned char reg, unsigned char val)
pommzorz 0:d84e1c7e4e86 280 {
pommzorz 0:d84e1c7e4e86 281 i2c.start();
tyftyftyf 2:c5ac16c88514 282 i2c.write(HMC58X3_ADDR<<1);
pommzorz 0:d84e1c7e4e86 283 i2c.write(reg); // send register address
pommzorz 0:d84e1c7e4e86 284 i2c.write(val); // send value to write
pommzorz 0:d84e1c7e4e86 285 i2c.stop(); //end transmission
pommzorz 0:d84e1c7e4e86 286 }
pommzorz 0:d84e1c7e4e86 287
pommzorz 0:d84e1c7e4e86 288
tyftyftyf 2:c5ac16c88514 289 void HMC58X3::getValues(int16_t *x,int16_t *y,int16_t *z)
pommzorz 0:d84e1c7e4e86 290 {
pommzorz 0:d84e1c7e4e86 291 float fx,fy,fz;
pommzorz 0:d84e1c7e4e86 292 getValues(&fx,&fy,&fz);
tyftyftyf 2:c5ac16c88514 293 *x= (int16_t) (fx + 0.5);
tyftyftyf 2:c5ac16c88514 294 *y= (int16_t) (fy + 0.5);
tyftyftyf 2:c5ac16c88514 295 *z= (int16_t) (fz + 0.5);
pommzorz 0:d84e1c7e4e86 296 }
pommzorz 0:d84e1c7e4e86 297
pommzorz 0:d84e1c7e4e86 298
pommzorz 0:d84e1c7e4e86 299 void HMC58X3::getValues(float *x,float *y,float *z)
pommzorz 0:d84e1c7e4e86 300 {
tyftyftyf 2:c5ac16c88514 301 int16_t xr,yr,zr;
pommzorz 0:d84e1c7e4e86 302
pommzorz 0:d84e1c7e4e86 303 getRaw(&xr, &yr, &zr);
pommzorz 0:d84e1c7e4e86 304 *x= ((float) xr) / x_scale;
pommzorz 0:d84e1c7e4e86 305 *y = ((float) yr) / y_scale;
pommzorz 0:d84e1c7e4e86 306 *z = ((float) zr) / z_scale;
pommzorz 0:d84e1c7e4e86 307 }
pommzorz 0:d84e1c7e4e86 308
pommzorz 0:d84e1c7e4e86 309
tyftyftyf 2:c5ac16c88514 310 void HMC58X3::getRaw(int16_t *x,int16_t *y,int16_t *z)
pommzorz 0:d84e1c7e4e86 311 {
pommzorz 0:d84e1c7e4e86 312
pommzorz 0:d84e1c7e4e86 313 char cmd[2];
pommzorz 0:d84e1c7e4e86 314 char data[6];
pommzorz 0:d84e1c7e4e86 315 cmd[0] = 0x03;
pommzorz 0:d84e1c7e4e86 316
tyftyftyf 2:c5ac16c88514 317 i2c.write(HMC58X3_ADDR<<1, cmd, 1, true); // set the pointer to the start of x
tyftyftyf 2:c5ac16c88514 318 i2c.read((HMC58X3_ADDR<<1)+1, data, 6, false);
pommzorz 0:d84e1c7e4e86 319
pommzorz 0:d84e1c7e4e86 320 // read out the 3 values, 2 bytes each.
pommzorz 0:d84e1c7e4e86 321 *x = int16_t(((unsigned char)data[0] << 8) | (unsigned char)data[1]);
pommzorz 0:d84e1c7e4e86 322 #ifdef ISHMC5843
pommzorz 0:d84e1c7e4e86 323 *y = int16_t(((unsigned char)data[1*2] << 8) | (unsigned char)data[1*2+1]);
pommzorz 0:d84e1c7e4e86 324 *z = int16_t(((unsigned char)data[2*2] << 8) | (unsigned char)data[2*2+1]);
pommzorz 0:d84e1c7e4e86 325 #else // the Z registers comes before the Y registers in the HMC5883L
pommzorz 0:d84e1c7e4e86 326 *z = int16_t(((unsigned char)data[1*2] << 8) | (unsigned char)data[1*2+1]);
pommzorz 0:d84e1c7e4e86 327 *y = int16_t(((unsigned char)data[2*2] << 8) | (unsigned char)data[2*2+1]);
pommzorz 0:d84e1c7e4e86 328 #endif
pommzorz 0:d84e1c7e4e86 329 // the HMC58X3 will automatically wrap around on the next request
pommzorz 0:d84e1c7e4e86 330
pommzorz 0:d84e1c7e4e86 331
pommzorz 0:d84e1c7e4e86 332 }
pommzorz 0:d84e1c7e4e86 333
pommzorz 0:d84e1c7e4e86 334
pommzorz 0:d84e1c7e4e86 335 void HMC58X3::getValues(float *xyz)
pommzorz 0:d84e1c7e4e86 336 {
pommzorz 0:d84e1c7e4e86 337 getValues(&xyz[0], &xyz[1], &xyz[2]);
pommzorz 0:d84e1c7e4e86 338 }
pommzorz 0:d84e1c7e4e86 339
pommzorz 0:d84e1c7e4e86 340 /*!
pommzorz 0:d84e1c7e4e86 341 \brief Retrieve the value of the three ID registers.
pommzorz 0:d84e1c7e4e86 342
pommzorz 0:d84e1c7e4e86 343 Note: Both the HMC5843 and HMC5883L have the same 'H43' identification register values. (Looks like someone at Honeywell screwed up.)
pommzorz 0:d84e1c7e4e86 344
pommzorz 0:d84e1c7e4e86 345 \param id [out] Returns the three id register values.
pommzorz 0:d84e1c7e4e86 346 */
pommzorz 0:d84e1c7e4e86 347 void HMC58X3::getID(char id[3])
pommzorz 0:d84e1c7e4e86 348 {
pommzorz 0:d84e1c7e4e86 349 i2c.start();
tyftyftyf 2:c5ac16c88514 350 i2c.write(HMC58X3_ADDR<<1);
pommzorz 0:d84e1c7e4e86 351 i2c.write(HMC58X3_R_IDA); // Will start reading registers starting from Identification Register A.
pommzorz 0:d84e1c7e4e86 352
tyftyftyf 2:c5ac16c88514 353 i2c.start();
tyftyftyf 2:c5ac16c88514 354 i2c.write((HMC58X3_ADDR<<1)+1);
pommzorz 0:d84e1c7e4e86 355 id[0] = i2c.read(0);
pommzorz 0:d84e1c7e4e86 356 id[1] = i2c.read(0);
pommzorz 0:d84e1c7e4e86 357 id[2] = i2c.read(0);
pommzorz 0:d84e1c7e4e86 358
pommzorz 0:d84e1c7e4e86 359 i2c.stop();
pommzorz 0:d84e1c7e4e86 360 } // getID().
pommzorz 0:d84e1c7e4e86 361
pommzorz 0:d84e1c7e4e86 362 int HMC58X3::min (int a, int b)
pommzorz 0:d84e1c7e4e86 363 {
pommzorz 0:d84e1c7e4e86 364 return !(b<a)?a:b; // or: return !comp(b,a)?a:b; for version (2)
pommzorz 0:d84e1c7e4e86 365 }