d

Fork of HMC5883L by GM AT

Committer:
gmatjuara
Date:
Thu Dec 12 02:25:11 2013 +0000
Revision:
0:cad18db1e431
Child:
1:17f3539e91b0
coba

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gmatjuara 0:cad18db1e431 1 /**
gmatjuara 0:cad18db1e431 2 * @author Jose R. Padron
gmatjuara 0:cad18db1e431 3 * @author Used HMC5883L library developed by Aaron Berk as template
gmatjuara 0:cad18db1e431 4 * @section LICENSE
gmatjuara 0:cad18db1e431 5 *
gmatjuara 0:cad18db1e431 6 * Copyright (c) 2010 ARM Limited
gmatjuara 0:cad18db1e431 7 *
gmatjuara 0:cad18db1e431 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
gmatjuara 0:cad18db1e431 9 * of this software and associated documentation files (the "Software"), to deal
gmatjuara 0:cad18db1e431 10 * in the Software without restriction, including without limitation the rights
gmatjuara 0:cad18db1e431 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
gmatjuara 0:cad18db1e431 12 * copies of the Software, and to permit persons to whom the Software is
gmatjuara 0:cad18db1e431 13 * furnished to do so, subject to the following conditions:
gmatjuara 0:cad18db1e431 14 *
gmatjuara 0:cad18db1e431 15 * The above copyright notice and this permission notice shall be included in
gmatjuara 0:cad18db1e431 16 * all copies or substantial portions of the Software.
gmatjuara 0:cad18db1e431 17 *
gmatjuara 0:cad18db1e431 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
gmatjuara 0:cad18db1e431 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
gmatjuara 0:cad18db1e431 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
gmatjuara 0:cad18db1e431 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
gmatjuara 0:cad18db1e431 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gmatjuara 0:cad18db1e431 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
gmatjuara 0:cad18db1e431 24 * THE SOFTWARE.
gmatjuara 0:cad18db1e431 25 *
gmatjuara 0:cad18db1e431 26 * @section DESCRIPTION
gmatjuara 0:cad18db1e431 27 *
gmatjuara 0:cad18db1e431 28 * Honeywell HMC5883L digital compass.
gmatjuara 0:cad18db1e431 29 *
gmatjuara 0:cad18db1e431 30 * Datasheet:
gmatjuara 0:cad18db1e431 31 *
gmatjuara 0:cad18db1e431 32 * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5883L.pdf
gmatjuara 0:cad18db1e431 33 */
gmatjuara 0:cad18db1e431 34
gmatjuara 0:cad18db1e431 35 #ifndef HMC5883L_H
gmatjuara 0:cad18db1e431 36 #define HMC5883L_H
gmatjuara 0:cad18db1e431 37
gmatjuara 0:cad18db1e431 38 /**
gmatjuara 0:cad18db1e431 39 * Includes
gmatjuara 0:cad18db1e431 40 */
gmatjuara 0:cad18db1e431 41 #include "mbed.h"
gmatjuara 0:cad18db1e431 42
gmatjuara 0:cad18db1e431 43 /**
gmatjuara 0:cad18db1e431 44 * Defines
gmatjuara 0:cad18db1e431 45 */
gmatjuara 0:cad18db1e431 46 #define HMC5883L_I2C_ADDRESS 0x1E //7-bit address. 0x3C write, 0x3D read.
gmatjuara 0:cad18db1e431 47 #define HMC5883L_I2C_WRITE 0x3C
gmatjuara 0:cad18db1e431 48 #define HMC5883L_I2C_READ 0x3D
gmatjuara 0:cad18db1e431 49
gmatjuara 0:cad18db1e431 50 //Values Config A
gmatjuara 0:cad18db1e431 51 #define HMC5883L_0_5HZ_NORMAL 0x00
gmatjuara 0:cad18db1e431 52 #define HMC5883L_0_5HZ_POSITIVE 0x01
gmatjuara 0:cad18db1e431 53 #define HMC5883L_0_5HZ_NEGATIVE 0x02
gmatjuara 0:cad18db1e431 54
gmatjuara 0:cad18db1e431 55 #define HMC5883L_1HZ_NORMAL 0x04
gmatjuara 0:cad18db1e431 56 #define HMC5883L_1HZ_POSITIVE 0x05
gmatjuara 0:cad18db1e431 57 #define HMC5883L_1HZ_NEGATIVE 0x06
gmatjuara 0:cad18db1e431 58
gmatjuara 0:cad18db1e431 59 #define HMC5883L_2HZ_NORMAL 0x08
gmatjuara 0:cad18db1e431 60 #define HMC5883L_2HZ_POSITIVE 0x09
gmatjuara 0:cad18db1e431 61 #define HMC5883L_2HZ_NEGATIVE 0x0A
gmatjuara 0:cad18db1e431 62
gmatjuara 0:cad18db1e431 63 #define HMC5883L_5HZ_NORMAL 0x0C
gmatjuara 0:cad18db1e431 64 #define HMC5883L_5HZ_POSITIVE 0x0D
gmatjuara 0:cad18db1e431 65 #define HMC5883L_5HZ_NEGATIVE 0x0E
gmatjuara 0:cad18db1e431 66
gmatjuara 0:cad18db1e431 67 #define HMC5883L_10HZ_NORMAL 0x10
gmatjuara 0:cad18db1e431 68 #define HMC5883L_10HZ_POSITIVE 0x11
gmatjuara 0:cad18db1e431 69 #define HMC5883L_10HZ_NEGATIVE 0x12
gmatjuara 0:cad18db1e431 70
gmatjuara 0:cad18db1e431 71 #define HMC5883L_20HZ_NORMAL 0x14
gmatjuara 0:cad18db1e431 72 #define HMC5883L_20HZ_POSITIVE 0x15
gmatjuara 0:cad18db1e431 73 #define HMC5883L_20HZ_NEGATIVE 0x16
gmatjuara 0:cad18db1e431 74
gmatjuara 0:cad18db1e431 75 #define HMC5883L_50HZ_NORMAL 0x18
gmatjuara 0:cad18db1e431 76 #define HMC5883L_50HZ_POSITIVE 0x19
gmatjuara 0:cad18db1e431 77 #define HMC5883L_50HZ_NEGATIVE 0x1A
gmatjuara 0:cad18db1e431 78
gmatjuara 0:cad18db1e431 79 //Values Config B
gmatjuara 0:cad18db1e431 80 #define HMC5883L_0_7GA 0x00
gmatjuara 0:cad18db1e431 81 #define HMC5883L_1_0GA 0x20
gmatjuara 0:cad18db1e431 82 #define HMC5883L_1_5GA 0x40
gmatjuara 0:cad18db1e431 83 #define HMC5883L_2_0GA 0x60
gmatjuara 0:cad18db1e431 84 #define HMC5883L_3_2GA 0x80
gmatjuara 0:cad18db1e431 85 #define HMC5883L_3_8GA 0xA0
gmatjuara 0:cad18db1e431 86 #define HMC5883L_4_5GA 0xC0
gmatjuara 0:cad18db1e431 87 #define HMC5883L_6_5GA 0xE0
gmatjuara 0:cad18db1e431 88
gmatjuara 0:cad18db1e431 89 //Values MODE
gmatjuara 0:cad18db1e431 90 #define HMC5883L_CONTINUOUS 0x00
gmatjuara 0:cad18db1e431 91 #define HMC5883L_SINGLE 0x01
gmatjuara 0:cad18db1e431 92 #define HMC5883L_IDLE 0x02
gmatjuara 0:cad18db1e431 93 #define HMC5883L_SLEEP 0x03
gmatjuara 0:cad18db1e431 94
gmatjuara 0:cad18db1e431 95
gmatjuara 0:cad18db1e431 96
gmatjuara 0:cad18db1e431 97 #define HMC5883L_CONFIG_A 0x00
gmatjuara 0:cad18db1e431 98 #define HMC5883L_CONFIG_B 0x01
gmatjuara 0:cad18db1e431 99 #define HMC5883L_MODE 0x02
gmatjuara 0:cad18db1e431 100 #define HMC5883L_X_MSB 0x03
gmatjuara 0:cad18db1e431 101 #define HMC5883L_X_LSB 0x04
gmatjuara 0:cad18db1e431 102 #define HMC5883L_Y_MSB 0x05
gmatjuara 0:cad18db1e431 103 #define HMC5883L_Y_LSB 0x06
gmatjuara 0:cad18db1e431 104 #define HMC5883L_Z_MSB 0x07
gmatjuara 0:cad18db1e431 105 #define HMC5883L_Z_LSB 0x08
gmatjuara 0:cad18db1e431 106 #define HMC5883L_STATUS 0x09
gmatjuara 0:cad18db1e431 107 #define HMC5883L_IDENT_A 0x0A
gmatjuara 0:cad18db1e431 108 #define HMC5883L_IDENT_B 0x0B
gmatjuara 0:cad18db1e431 109 #define HMC5883L_IDENT_C 0x0C
gmatjuara 0:cad18db1e431 110
gmatjuara 0:cad18db1e431 111
gmatjuara 0:cad18db1e431 112
gmatjuara 0:cad18db1e431 113 /**
gmatjuara 0:cad18db1e431 114 * Honeywell HMC5883L digital compass.
gmatjuara 0:cad18db1e431 115 */
gmatjuara 0:cad18db1e431 116 class HMC5883L {
gmatjuara 0:cad18db1e431 117
gmatjuara 0:cad18db1e431 118 public:
gmatjuara 0:cad18db1e431 119
gmatjuara 0:cad18db1e431 120 /**
gmatjuara 0:cad18db1e431 121 * Constructor.
gmatjuara 0:cad18db1e431 122 *
gmatjuara 0:cad18db1e431 123 * @param sda mbed pin to use for SDA line of I2C interface.
gmatjuara 0:cad18db1e431 124 * @param scl mbed pin to use for SCL line of I2C interface.
gmatjuara 0:cad18db1e431 125 */
gmatjuara 0:cad18db1e431 126 HMC5883L(PinName sda, PinName scl);
gmatjuara 0:cad18db1e431 127
gmatjuara 0:cad18db1e431 128
gmatjuara 0:cad18db1e431 129 /**
gmatjuara 0:cad18db1e431 130 * Enter into sleep mode.
gmatjuara 0:cad18db1e431 131 *
gmatjuara 0:cad18db1e431 132 */
gmatjuara 0:cad18db1e431 133 void setSleepMode();
gmatjuara 0:cad18db1e431 134
gmatjuara 0:cad18db1e431 135
gmatjuara 0:cad18db1e431 136 /**
gmatjuara 0:cad18db1e431 137 * Set Device in Default Mode.
gmatjuara 0:cad18db1e431 138 * HMC5883L_CONTINUOUS, HMC5883L_10HZ_NORMAL HMC5883L_1_0GA
gmatjuara 0:cad18db1e431 139 */
gmatjuara 0:cad18db1e431 140 void setDefault();
gmatjuara 0:cad18db1e431 141
gmatjuara 0:cad18db1e431 142
gmatjuara 0:cad18db1e431 143 /**
gmatjuara 0:cad18db1e431 144 * Read the memory location on the device which contains the address.
gmatjuara 0:cad18db1e431 145 *
gmatjuara 0:cad18db1e431 146 * @param Pointer to a buffer to hold the address value
gmatjuara 0:cad18db1e431 147 * Expected H, 4 and 3.
gmatjuara 0:cad18db1e431 148 */
gmatjuara 0:cad18db1e431 149 void getAddress(char * address);
gmatjuara 0:cad18db1e431 150
gmatjuara 0:cad18db1e431 151
gmatjuara 0:cad18db1e431 152
gmatjuara 0:cad18db1e431 153 /**
gmatjuara 0:cad18db1e431 154 * Set the operation mode.
gmatjuara 0:cad18db1e431 155 *
gmatjuara 0:cad18db1e431 156 * @param mode 0x00 -> Continuous
gmatjuara 0:cad18db1e431 157 * 0x01 -> Single
gmatjuara 0:cad18db1e431 158 * 0x02 -> Idle
gmatjuara 0:cad18db1e431 159 * @param ConfigA values
gmatjuara 0:cad18db1e431 160 * @param ConfigB values
gmatjuara 0:cad18db1e431 161 */
gmatjuara 0:cad18db1e431 162 void setOpMode(int mode, int ConfigA, int ConfigB);
gmatjuara 0:cad18db1e431 163
gmatjuara 0:cad18db1e431 164 /**
gmatjuara 0:cad18db1e431 165 * Write to on the device.
gmatjuara 0:cad18db1e431 166 *
gmatjuara 0:cad18db1e431 167 * @param address Address to write to.
gmatjuara 0:cad18db1e431 168 * @param data Data to write.
gmatjuara 0:cad18db1e431 169 */
gmatjuara 0:cad18db1e431 170
gmatjuara 0:cad18db1e431 171 void write(int address, int data);
gmatjuara 0:cad18db1e431 172
gmatjuara 0:cad18db1e431 173 /**
gmatjuara 0:cad18db1e431 174 * Get the output of all three axes.
gmatjuara 0:cad18db1e431 175 *
gmatjuara 0:cad18db1e431 176 * @param Pointer to a buffer to hold the magnetics value for the
gmatjuara 0:cad18db1e431 177 * x-axis, y-axis and z-axis [in that order].
gmatjuara 0:cad18db1e431 178 */
gmatjuara 0:cad18db1e431 179 void readData(int* readings);
gmatjuara 0:cad18db1e431 180
gmatjuara 0:cad18db1e431 181 /**
gmatjuara 0:cad18db1e431 182 * Get the output of X axis.
gmatjuara 0:cad18db1e431 183 *
gmatjuara 0:cad18db1e431 184 * @return x-axis magnetic value
gmatjuara 0:cad18db1e431 185 */
gmatjuara 0:cad18db1e431 186 int getMx();
gmatjuara 0:cad18db1e431 187
gmatjuara 0:cad18db1e431 188 /**
gmatjuara 0:cad18db1e431 189 * Get the output of Y axis.
gmatjuara 0:cad18db1e431 190 *
gmatjuara 0:cad18db1e431 191 * @return y-axis magnetic value
gmatjuara 0:cad18db1e431 192 */
gmatjuara 0:cad18db1e431 193 int getMy();
gmatjuara 0:cad18db1e431 194
gmatjuara 0:cad18db1e431 195 /**
gmatjuara 0:cad18db1e431 196 * Get the output of Z axis.
gmatjuara 0:cad18db1e431 197 *
gmatjuara 0:cad18db1e431 198 * @return z-axis magnetic value
gmatjuara 0:cad18db1e431 199 */
gmatjuara 0:cad18db1e431 200 int getMz();
gmatjuara 0:cad18db1e431 201
gmatjuara 0:cad18db1e431 202
gmatjuara 0:cad18db1e431 203 /**
gmatjuara 0:cad18db1e431 204 * Get the current operation mode.
gmatjuara 0:cad18db1e431 205 *
gmatjuara 0:cad18db1e431 206 * @return Status register values
gmatjuara 0:cad18db1e431 207 */
gmatjuara 0:cad18db1e431 208 int getStatus(void);
gmatjuara 0:cad18db1e431 209
gmatjuara 0:cad18db1e431 210
gmatjuara 0:cad18db1e431 211
gmatjuara 0:cad18db1e431 212 I2C* i2c_;
gmatjuara 0:cad18db1e431 213
gmatjuara 0:cad18db1e431 214
gmatjuara 0:cad18db1e431 215
gmatjuara 0:cad18db1e431 216 };
gmatjuara 0:cad18db1e431 217
gmatjuara 0:cad18db1e431 218 #endif /* HMC5883L_H */
gmatjuara 0:cad18db1e431 219