Forked from Jose R. Padron and Aaron Berk's library, customized for my specific application using 9DoF-Stick by Sparkfun.

Fork of HMC5843 by Jose R Padron

HMC5843 is triple axis, digital interface compass (geomagnetic sensor).

This library is forked from Jose R. Padron and Aaron Berk's work.

This library is for specific application using 9DoF-Stick.

Datasheet:

http://www.sparkfun.com/datasheets/Sensors/Magneto/HMC5843.pdf

HMC5843 は3軸のデジタルインターフェースを備えたコンパス(地磁気センサ)です。

このライブラリは 9DoF-Stick を使用した特定の企画のために保守しています。

mbed IDEが日本語をサポートするまでは英語でコメントを書いていきますが、サポートした後もきっと英語で書いていくでしょう。

Committer:
gltest26
Date:
Sat Sep 29 14:18:48 2012 +0000
Revision:
6:05aa3555fce6
Parent:
5:a83508250db7
Added a new constructor to accept an external I2C interface object.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elrafapadron 0:ae7d06398888 1 /**
gltest26 4:7a90125eedcf 2 * @file HMC5843.h
elrafapadron 0:ae7d06398888 3 * @author Jose R. Padron
elrafapadron 2:fdab96fc6fff 4 * @author Used HMC5843 library developed by Aaron Berk as template
elrafapadron 0:ae7d06398888 5 * @section LICENSE
elrafapadron 0:ae7d06398888 6 *
elrafapadron 0:ae7d06398888 7 * Copyright (c) 2010 ARM Limited
elrafapadron 0:ae7d06398888 8 *
elrafapadron 0:ae7d06398888 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
elrafapadron 0:ae7d06398888 10 * of this software and associated documentation files (the "Software"), to deal
elrafapadron 0:ae7d06398888 11 * in the Software without restriction, including without limitation the rights
elrafapadron 0:ae7d06398888 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
elrafapadron 0:ae7d06398888 13 * copies of the Software, and to permit persons to whom the Software is
elrafapadron 0:ae7d06398888 14 * furnished to do so, subject to the following conditions:
elrafapadron 0:ae7d06398888 15 *
elrafapadron 0:ae7d06398888 16 * The above copyright notice and this permission notice shall be included in
elrafapadron 0:ae7d06398888 17 * all copies or substantial portions of the Software.
elrafapadron 0:ae7d06398888 18 *
elrafapadron 0:ae7d06398888 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
elrafapadron 0:ae7d06398888 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
elrafapadron 0:ae7d06398888 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
elrafapadron 0:ae7d06398888 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
elrafapadron 0:ae7d06398888 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
elrafapadron 0:ae7d06398888 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
elrafapadron 0:ae7d06398888 25 * THE SOFTWARE.
elrafapadron 0:ae7d06398888 26 *
elrafapadron 0:ae7d06398888 27 * @section DESCRIPTION
elrafapadron 0:ae7d06398888 28 *
elrafapadron 0:ae7d06398888 29 * Honeywell HMC5843 digital compass.
elrafapadron 0:ae7d06398888 30 *
elrafapadron 0:ae7d06398888 31 * Datasheet:
elrafapadron 0:ae7d06398888 32 *
elrafapadron 0:ae7d06398888 33 * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5843.pdf
elrafapadron 0:ae7d06398888 34 */
elrafapadron 0:ae7d06398888 35
elrafapadron 0:ae7d06398888 36 #ifndef HMC5843_H
elrafapadron 0:ae7d06398888 37 #define HMC5843_H
elrafapadron 0:ae7d06398888 38
elrafapadron 0:ae7d06398888 39 #include "mbed.h"
elrafapadron 0:ae7d06398888 40
gltest26 4:7a90125eedcf 41
elrafapadron 0:ae7d06398888 42 #define HMC5843_I2C_ADDRESS 0x1E //7-bit address. 0x3C write, 0x3D read.
elrafapadron 0:ae7d06398888 43 #define HMC5843_I2C_WRITE 0x3C
elrafapadron 0:ae7d06398888 44 #define HMC5843_I2C_READ 0x3D
elrafapadron 0:ae7d06398888 45
elrafapadron 0:ae7d06398888 46 //Values Config A
elrafapadron 0:ae7d06398888 47 #define HMC5843_0_5HZ_NORMAL 0x00
elrafapadron 0:ae7d06398888 48 #define HMC5843_0_5HZ_POSITIVE 0x01
elrafapadron 0:ae7d06398888 49 #define HMC5843_0_5HZ_NEGATIVE 0x02
elrafapadron 0:ae7d06398888 50
elrafapadron 0:ae7d06398888 51 #define HMC5843_1HZ_NORMAL 0x04
elrafapadron 0:ae7d06398888 52 #define HMC5843_1HZ_POSITIVE 0x05
elrafapadron 0:ae7d06398888 53 #define HMC5843_1HZ_NEGATIVE 0x06
elrafapadron 0:ae7d06398888 54
elrafapadron 0:ae7d06398888 55 #define HMC5843_2HZ_NORMAL 0x08
elrafapadron 0:ae7d06398888 56 #define HMC5843_2HZ_POSITIVE 0x09
elrafapadron 0:ae7d06398888 57 #define HMC5843_2HZ_NEGATIVE 0x0A
elrafapadron 0:ae7d06398888 58
elrafapadron 0:ae7d06398888 59 #define HMC5843_5HZ_NORMAL 0x0C
elrafapadron 0:ae7d06398888 60 #define HMC5843_5HZ_POSITIVE 0x0D
elrafapadron 0:ae7d06398888 61 #define HMC5843_5HZ_NEGATIVE 0x0E
elrafapadron 0:ae7d06398888 62
elrafapadron 0:ae7d06398888 63 #define HMC5843_10HZ_NORMAL 0x10
elrafapadron 0:ae7d06398888 64 #define HMC5843_10HZ_POSITIVE 0x11
elrafapadron 0:ae7d06398888 65 #define HMC5843_10HZ_NEGATIVE 0x12
elrafapadron 0:ae7d06398888 66
elrafapadron 0:ae7d06398888 67 #define HMC5843_20HZ_NORMAL 0x14
elrafapadron 0:ae7d06398888 68 #define HMC5843_20HZ_POSITIVE 0x15
elrafapadron 0:ae7d06398888 69 #define HMC5843_20HZ_NEGATIVE 0x16
elrafapadron 0:ae7d06398888 70
elrafapadron 0:ae7d06398888 71 #define HMC5843_50HZ_NORMAL 0x18
elrafapadron 0:ae7d06398888 72 #define HMC5843_50HZ_POSITIVE 0x19
elrafapadron 0:ae7d06398888 73 #define HMC5843_50HZ_NEGATIVE 0x1A
elrafapadron 0:ae7d06398888 74
elrafapadron 0:ae7d06398888 75 //Values Config B
elrafapadron 0:ae7d06398888 76 #define HMC5843_0_7GA 0x00
elrafapadron 0:ae7d06398888 77 #define HMC5843_1_0GA 0x20
elrafapadron 0:ae7d06398888 78 #define HMC5843_1_5GA 0x40
elrafapadron 0:ae7d06398888 79 #define HMC5843_2_0GA 0x60
elrafapadron 0:ae7d06398888 80 #define HMC5843_3_2GA 0x80
elrafapadron 0:ae7d06398888 81 #define HMC5843_3_8GA 0xA0
elrafapadron 0:ae7d06398888 82 #define HMC5843_4_5GA 0xC0
elrafapadron 0:ae7d06398888 83 #define HMC5843_6_5GA 0xE0
elrafapadron 0:ae7d06398888 84
elrafapadron 0:ae7d06398888 85 //Values MODE
elrafapadron 0:ae7d06398888 86 #define HMC5843_CONTINUOUS 0x00
elrafapadron 0:ae7d06398888 87 #define HMC5843_SINGLE 0x01
elrafapadron 0:ae7d06398888 88 #define HMC5843_IDLE 0x02
elrafapadron 0:ae7d06398888 89 #define HMC5843_SLEEP 0x03
elrafapadron 0:ae7d06398888 90
elrafapadron 0:ae7d06398888 91
elrafapadron 0:ae7d06398888 92
elrafapadron 0:ae7d06398888 93 #define HMC5843_CONFIG_A 0x00
elrafapadron 0:ae7d06398888 94 #define HMC5843_CONFIG_B 0x01
elrafapadron 0:ae7d06398888 95 #define HMC5843_MODE 0x02
elrafapadron 0:ae7d06398888 96 #define HMC5843_X_MSB 0x03
elrafapadron 0:ae7d06398888 97 #define HMC5843_X_LSB 0x04
elrafapadron 0:ae7d06398888 98 #define HMC5843_Y_MSB 0x05
elrafapadron 0:ae7d06398888 99 #define HMC5843_Y_LSB 0x06
elrafapadron 0:ae7d06398888 100 #define HMC5843_Z_MSB 0x07
elrafapadron 0:ae7d06398888 101 #define HMC5843_Z_LSB 0x08
elrafapadron 0:ae7d06398888 102 #define HMC5843_STATUS 0x09
elrafapadron 0:ae7d06398888 103 #define HMC5843_IDENT_A 0x0A
elrafapadron 0:ae7d06398888 104 #define HMC5843_IDENT_B 0x0B
elrafapadron 0:ae7d06398888 105 #define HMC5843_IDENT_C 0x0C
elrafapadron 0:ae7d06398888 106
elrafapadron 0:ae7d06398888 107
elrafapadron 0:ae7d06398888 108
elrafapadron 0:ae7d06398888 109 /**
elrafapadron 0:ae7d06398888 110 * Honeywell HMC5843 digital compass.
elrafapadron 0:ae7d06398888 111 */
elrafapadron 0:ae7d06398888 112 class HMC5843 {
elrafapadron 0:ae7d06398888 113
elrafapadron 0:ae7d06398888 114 public:
elrafapadron 0:ae7d06398888 115
elrafapadron 0:ae7d06398888 116 /**
gltest26 5:a83508250db7 117 * The I2C address that can be passed directly to i2c object (it's already shifted 1 bit left).
gltest26 5:a83508250db7 118 *
gltest26 5:a83508250db7 119 * You don't need to manually set or clear the LSB when calling I2C::read() or I2C::write(),
gltest26 5:a83508250db7 120 * the library takes care of it. We just always clear the LSB.
gltest26 5:a83508250db7 121 */
gltest26 5:a83508250db7 122 static const int I2C_ADDRESS = HMC5843_I2C_WRITE;
gltest26 5:a83508250db7 123
gltest26 5:a83508250db7 124 /**
elrafapadron 0:ae7d06398888 125 * Constructor.
elrafapadron 0:ae7d06398888 126 *
elrafapadron 0:ae7d06398888 127 * @param sda mbed pin to use for SDA line of I2C interface.
elrafapadron 0:ae7d06398888 128 * @param scl mbed pin to use for SCL line of I2C interface.
elrafapadron 0:ae7d06398888 129 */
elrafapadron 0:ae7d06398888 130 HMC5843(PinName sda, PinName scl);
gltest26 6:05aa3555fce6 131
gltest26 6:05aa3555fce6 132 /**
gltest26 6:05aa3555fce6 133 * Constructor that accepts external i2c interface object.
gltest26 6:05aa3555fce6 134 *
gltest26 6:05aa3555fce6 135 * @param i2c The I2C interface object to use.
gltest26 6:05aa3555fce6 136 */
gltest26 6:05aa3555fce6 137 HMC5843(I2C &i2c) : i2c_(i2c), myI2c(NULL){}
gltest26 6:05aa3555fce6 138
gltest26 6:05aa3555fce6 139 /**
gltest26 6:05aa3555fce6 140 * Destructor that frees self-allocated I2C object.
gltest26 6:05aa3555fce6 141 */
gltest26 6:05aa3555fce6 142 ~HMC5843(){
gltest26 6:05aa3555fce6 143 delete myI2c;
gltest26 6:05aa3555fce6 144 }
elrafapadron 0:ae7d06398888 145
elrafapadron 0:ae7d06398888 146
elrafapadron 0:ae7d06398888 147 /**
elrafapadron 0:ae7d06398888 148 * Enter into sleep mode.
elrafapadron 0:ae7d06398888 149 *
elrafapadron 0:ae7d06398888 150 */
elrafapadron 0:ae7d06398888 151 void setSleepMode();
elrafapadron 0:ae7d06398888 152
elrafapadron 0:ae7d06398888 153
elrafapadron 0:ae7d06398888 154 /**
elrafapadron 0:ae7d06398888 155 * Set Device in Default Mode.
elrafapadron 0:ae7d06398888 156 * HMC5843_CONTINUOUS, HMC5843_10HZ_NORMAL HMC5843_1_0GA
elrafapadron 0:ae7d06398888 157 */
elrafapadron 0:ae7d06398888 158 void setDefault();
elrafapadron 0:ae7d06398888 159
elrafapadron 2:fdab96fc6fff 160
elrafapadron 0:ae7d06398888 161 /**
elrafapadron 0:ae7d06398888 162 * Read the memory location on the device which contains the address.
elrafapadron 0:ae7d06398888 163 *
elrafapadron 0:ae7d06398888 164 * @param Pointer to a buffer to hold the address value
elrafapadron 0:ae7d06398888 165 * Expected H, 4 and 3.
elrafapadron 0:ae7d06398888 166 */
elrafapadron 0:ae7d06398888 167 void getAddress(char * address);
elrafapadron 0:ae7d06398888 168
elrafapadron 0:ae7d06398888 169
elrafapadron 0:ae7d06398888 170
elrafapadron 0:ae7d06398888 171 /**
elrafapadron 0:ae7d06398888 172 * Set the operation mode.
elrafapadron 0:ae7d06398888 173 *
elrafapadron 0:ae7d06398888 174 * @param mode 0x00 -> Continuous
elrafapadron 0:ae7d06398888 175 * 0x01 -> Single
elrafapadron 0:ae7d06398888 176 * 0x02 -> Idle
elrafapadron 0:ae7d06398888 177 * @param ConfigA values
elrafapadron 0:ae7d06398888 178 * @param ConfigB values
elrafapadron 0:ae7d06398888 179 */
elrafapadron 0:ae7d06398888 180 void setOpMode(int mode, int ConfigA, int ConfigB);
elrafapadron 0:ae7d06398888 181
elrafapadron 0:ae7d06398888 182 /**
elrafapadron 0:ae7d06398888 183 * Write to on the device.
elrafapadron 0:ae7d06398888 184 *
elrafapadron 2:fdab96fc6fff 185 * @param address Address to write to.
elrafapadron 0:ae7d06398888 186 * @param data Data to write.
elrafapadron 0:ae7d06398888 187 */
elrafapadron 2:fdab96fc6fff 188
elrafapadron 0:ae7d06398888 189 void write(int address, int data);
elrafapadron 0:ae7d06398888 190
elrafapadron 2:fdab96fc6fff 191 /**
elrafapadron 2:fdab96fc6fff 192 * Get the output of all three axes.
elrafapadron 0:ae7d06398888 193 *
elrafapadron 0:ae7d06398888 194 * @param Pointer to a buffer to hold the magnetics value for the
elrafapadron 0:ae7d06398888 195 * x-axis, y-axis and z-axis [in that order].
elrafapadron 0:ae7d06398888 196 */
elrafapadron 0:ae7d06398888 197 void readData(int* readings);
elrafapadron 0:ae7d06398888 198
elrafapadron 2:fdab96fc6fff 199 /**
elrafapadron 2:fdab96fc6fff 200 * Get the output of X axis.
elrafapadron 2:fdab96fc6fff 201 *
elrafapadron 2:fdab96fc6fff 202 * @return x-axis magnetic value
elrafapadron 2:fdab96fc6fff 203 */
gltest26 5:a83508250db7 204 int getMx(){ return getWord(HMC5843_X_MSB); }
elrafapadron 2:fdab96fc6fff 205
elrafapadron 2:fdab96fc6fff 206 /**
elrafapadron 2:fdab96fc6fff 207 * Get the output of Y axis.
elrafapadron 0:ae7d06398888 208 *
elrafapadron 2:fdab96fc6fff 209 * @return y-axis magnetic value
elrafapadron 2:fdab96fc6fff 210 */
gltest26 5:a83508250db7 211 int getMy(){ return getWord(HMC5843_Y_MSB); }
elrafapadron 2:fdab96fc6fff 212
elrafapadron 2:fdab96fc6fff 213 /**
elrafapadron 2:fdab96fc6fff 214 * Get the output of Z axis.
elrafapadron 2:fdab96fc6fff 215 *
elrafapadron 2:fdab96fc6fff 216 * @return z-axis magnetic value
elrafapadron 2:fdab96fc6fff 217 */
gltest26 5:a83508250db7 218 int getMz(){ return getWord(HMC5843_Z_MSB); }
elrafapadron 2:fdab96fc6fff 219
elrafapadron 2:fdab96fc6fff 220
elrafapadron 2:fdab96fc6fff 221 /**
elrafapadron 2:fdab96fc6fff 222 * Get the current operation mode.
elrafapadron 2:fdab96fc6fff 223 *
elrafapadron 2:fdab96fc6fff 224 * @return Status register values
elrafapadron 0:ae7d06398888 225 */
elrafapadron 0:ae7d06398888 226 int getStatus(void);
elrafapadron 0:ae7d06398888 227
gltest26 5:a83508250db7 228
gltest26 5:a83508250db7 229 protected:
gltest26 5:a83508250db7 230
gltest26 5:a83508250db7 231 /**
gltest26 5:a83508250db7 232 * Reads a word (2 bytes) from the sensor via I2C bus.
gltest26 5:a83508250db7 233 *
gltest26 5:a83508250db7 234 * The queried value is assumed big-endian, 2's complement value.
gltest26 5:a83508250db7 235 *
gltest26 5:a83508250db7 236 * This protected function is added because we shouldn't write getMx(), getMy() and getMz()
gltest26 5:a83508250db7 237 * independently, but collect common codes.
gltest26 5:a83508250db7 238 *
gltest26 5:a83508250db7 239 * @param regi Register address to be read.
gltest26 5:a83508250db7 240 */
gltest26 5:a83508250db7 241 int getWord(int regi);
elrafapadron 0:ae7d06398888 242
gltest26 4:7a90125eedcf 243 private:
elrafapadron 0:ae7d06398888 244
gltest26 4:7a90125eedcf 245 /**
gltest26 4:7a90125eedcf 246 * Internal I2C object for communicating with the device.
gltest26 4:7a90125eedcf 247 *
gltest26 4:7a90125eedcf 248 * Changed from a pointer (new allocated object) from embedded object.
gltest26 4:7a90125eedcf 249 * The I2C class object is not meant to be allocated in the heap, but
gltest26 4:7a90125eedcf 250 * in the global storage.
gltest26 4:7a90125eedcf 251 * There's no point dynamically allocating an I2C object in this class,
gltest26 4:7a90125eedcf 252 * at least you should write destructor to release it!
gltest26 4:7a90125eedcf 253 */
gltest26 6:05aa3555fce6 254 I2C &i2c_;
gltest26 6:05aa3555fce6 255
gltest26 6:05aa3555fce6 256 I2C *myI2c;
elrafapadron 0:ae7d06398888 257
gltest26 5:a83508250db7 258 /**
gltest26 5:a83508250db7 259 * Converts big-endian 2's complement byte pair to native byte order of
gltest26 5:a83508250db7 260 * the CPU and then sign extend it to the CPU's register size.
gltest26 5:a83508250db7 261 *
gltest26 5:a83508250db7 262 * Implemented here to make the compiler inline expand it.
gltest26 5:a83508250db7 263 */
gltest26 5:a83508250db7 264 int swapExtend(const char rx[2]){
gltest26 5:a83508250db7 265 // Readings are expressed in 16bit 2's complement, so we must first
gltest26 5:a83508250db7 266 // concatenate two bytes to make a word and sign extend it to obtain
gltest26 5:a83508250db7 267 // correct negative values.
gltest26 5:a83508250db7 268 // ARMCC compiles char as unsigned, which means no sign extension is
gltest26 5:a83508250db7 269 // performed during bitwise operations to chars. But we should make sure
gltest26 5:a83508250db7 270 // that lower byte won't extend its sign past upper byte for other
gltest26 5:a83508250db7 271 // compilers if we want to keep it portable.
gltest26 5:a83508250db7 272 return int16_t(((unsigned char)rx[0] << 8) | (unsigned char)rx[1]);
gltest26 5:a83508250db7 273 }
elrafapadron 0:ae7d06398888 274
elrafapadron 0:ae7d06398888 275 };
elrafapadron 0:ae7d06398888 276
elrafapadron 0:ae7d06398888 277 #endif /* HMC5843_H */