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