ok
Fork of HMC5843 by
HMC5843.h@2:fdab96fc6fff, 2010-11-09 (annotated)
- Committer:
- elrafapadron
- Date:
- Tue Nov 09 23:18:46 2010 +0000
- Revision:
- 2:fdab96fc6fff
- Parent:
- 0:ae7d06398888
Add functions to get Mx, My and Mz.
Correct function declaration; readData function wasn\t visible
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elrafapadron | 0:ae7d06398888 | 1 | /** |
elrafapadron | 0:ae7d06398888 | 2 | * @author Jose R. Padron |
elrafapadron | 2:fdab96fc6fff | 3 | * @author Used HMC5843 library developed by Aaron Berk as template |
elrafapadron | 0:ae7d06398888 | 4 | * @section LICENSE |
elrafapadron | 0:ae7d06398888 | 5 | * |
elrafapadron | 0:ae7d06398888 | 6 | * Copyright (c) 2010 ARM Limited |
elrafapadron | 0:ae7d06398888 | 7 | * |
elrafapadron | 0:ae7d06398888 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
elrafapadron | 0:ae7d06398888 | 9 | * of this software and associated documentation files (the "Software"), to deal |
elrafapadron | 0:ae7d06398888 | 10 | * in the Software without restriction, including without limitation the rights |
elrafapadron | 0:ae7d06398888 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
elrafapadron | 0:ae7d06398888 | 12 | * copies of the Software, and to permit persons to whom the Software is |
elrafapadron | 0:ae7d06398888 | 13 | * furnished to do so, subject to the following conditions: |
elrafapadron | 0:ae7d06398888 | 14 | * |
elrafapadron | 0:ae7d06398888 | 15 | * The above copyright notice and this permission notice shall be included in |
elrafapadron | 0:ae7d06398888 | 16 | * all copies or substantial portions of the Software. |
elrafapadron | 0:ae7d06398888 | 17 | * |
elrafapadron | 0:ae7d06398888 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
elrafapadron | 0:ae7d06398888 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
elrafapadron | 0:ae7d06398888 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
elrafapadron | 0:ae7d06398888 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
elrafapadron | 0:ae7d06398888 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
elrafapadron | 0:ae7d06398888 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
elrafapadron | 0:ae7d06398888 | 24 | * THE SOFTWARE. |
elrafapadron | 0:ae7d06398888 | 25 | * |
elrafapadron | 0:ae7d06398888 | 26 | * @section DESCRIPTION |
elrafapadron | 0:ae7d06398888 | 27 | * |
elrafapadron | 0:ae7d06398888 | 28 | * Honeywell HMC5843 digital compass. |
elrafapadron | 0:ae7d06398888 | 29 | * |
elrafapadron | 0:ae7d06398888 | 30 | * Datasheet: |
elrafapadron | 0:ae7d06398888 | 31 | * |
elrafapadron | 0:ae7d06398888 | 32 | * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5843.pdf |
elrafapadron | 0:ae7d06398888 | 33 | */ |
elrafapadron | 0:ae7d06398888 | 34 | |
elrafapadron | 0:ae7d06398888 | 35 | #ifndef HMC5843_H |
elrafapadron | 0:ae7d06398888 | 36 | #define HMC5843_H |
elrafapadron | 0:ae7d06398888 | 37 | |
elrafapadron | 0:ae7d06398888 | 38 | /** |
elrafapadron | 0:ae7d06398888 | 39 | * Includes |
elrafapadron | 0:ae7d06398888 | 40 | */ |
elrafapadron | 0:ae7d06398888 | 41 | #include "mbed.h" |
elrafapadron | 0:ae7d06398888 | 42 | |
elrafapadron | 0:ae7d06398888 | 43 | /** |
elrafapadron | 0:ae7d06398888 | 44 | * Defines |
elrafapadron | 0:ae7d06398888 | 45 | */ |
elrafapadron | 0:ae7d06398888 | 46 | #define HMC5843_I2C_ADDRESS 0x1E //7-bit address. 0x3C write, 0x3D read. |
elrafapadron | 0:ae7d06398888 | 47 | #define HMC5843_I2C_WRITE 0x3C |
elrafapadron | 0:ae7d06398888 | 48 | #define HMC5843_I2C_READ 0x3D |
elrafapadron | 0:ae7d06398888 | 49 | |
elrafapadron | 0:ae7d06398888 | 50 | //Values Config A |
elrafapadron | 0:ae7d06398888 | 51 | #define HMC5843_0_5HZ_NORMAL 0x00 |
elrafapadron | 0:ae7d06398888 | 52 | #define HMC5843_0_5HZ_POSITIVE 0x01 |
elrafapadron | 0:ae7d06398888 | 53 | #define HMC5843_0_5HZ_NEGATIVE 0x02 |
elrafapadron | 0:ae7d06398888 | 54 | |
elrafapadron | 0:ae7d06398888 | 55 | #define HMC5843_1HZ_NORMAL 0x04 |
elrafapadron | 0:ae7d06398888 | 56 | #define HMC5843_1HZ_POSITIVE 0x05 |
elrafapadron | 0:ae7d06398888 | 57 | #define HMC5843_1HZ_NEGATIVE 0x06 |
elrafapadron | 0:ae7d06398888 | 58 | |
elrafapadron | 0:ae7d06398888 | 59 | #define HMC5843_2HZ_NORMAL 0x08 |
elrafapadron | 0:ae7d06398888 | 60 | #define HMC5843_2HZ_POSITIVE 0x09 |
elrafapadron | 0:ae7d06398888 | 61 | #define HMC5843_2HZ_NEGATIVE 0x0A |
elrafapadron | 0:ae7d06398888 | 62 | |
elrafapadron | 0:ae7d06398888 | 63 | #define HMC5843_5HZ_NORMAL 0x0C |
elrafapadron | 0:ae7d06398888 | 64 | #define HMC5843_5HZ_POSITIVE 0x0D |
elrafapadron | 0:ae7d06398888 | 65 | #define HMC5843_5HZ_NEGATIVE 0x0E |
elrafapadron | 0:ae7d06398888 | 66 | |
elrafapadron | 0:ae7d06398888 | 67 | #define HMC5843_10HZ_NORMAL 0x10 |
elrafapadron | 0:ae7d06398888 | 68 | #define HMC5843_10HZ_POSITIVE 0x11 |
elrafapadron | 0:ae7d06398888 | 69 | #define HMC5843_10HZ_NEGATIVE 0x12 |
elrafapadron | 0:ae7d06398888 | 70 | |
elrafapadron | 0:ae7d06398888 | 71 | #define HMC5843_20HZ_NORMAL 0x14 |
elrafapadron | 0:ae7d06398888 | 72 | #define HMC5843_20HZ_POSITIVE 0x15 |
elrafapadron | 0:ae7d06398888 | 73 | #define HMC5843_20HZ_NEGATIVE 0x16 |
elrafapadron | 0:ae7d06398888 | 74 | |
elrafapadron | 0:ae7d06398888 | 75 | #define HMC5843_50HZ_NORMAL 0x18 |
elrafapadron | 0:ae7d06398888 | 76 | #define HMC5843_50HZ_POSITIVE 0x19 |
elrafapadron | 0:ae7d06398888 | 77 | #define HMC5843_50HZ_NEGATIVE 0x1A |
elrafapadron | 0:ae7d06398888 | 78 | |
elrafapadron | 0:ae7d06398888 | 79 | //Values Config B |
elrafapadron | 0:ae7d06398888 | 80 | #define HMC5843_0_7GA 0x00 |
elrafapadron | 0:ae7d06398888 | 81 | #define HMC5843_1_0GA 0x20 |
elrafapadron | 0:ae7d06398888 | 82 | #define HMC5843_1_5GA 0x40 |
elrafapadron | 0:ae7d06398888 | 83 | #define HMC5843_2_0GA 0x60 |
elrafapadron | 0:ae7d06398888 | 84 | #define HMC5843_3_2GA 0x80 |
elrafapadron | 0:ae7d06398888 | 85 | #define HMC5843_3_8GA 0xA0 |
elrafapadron | 0:ae7d06398888 | 86 | #define HMC5843_4_5GA 0xC0 |
elrafapadron | 0:ae7d06398888 | 87 | #define HMC5843_6_5GA 0xE0 |
elrafapadron | 0:ae7d06398888 | 88 | |
elrafapadron | 0:ae7d06398888 | 89 | //Values MODE |
elrafapadron | 0:ae7d06398888 | 90 | #define HMC5843_CONTINUOUS 0x00 |
elrafapadron | 0:ae7d06398888 | 91 | #define HMC5843_SINGLE 0x01 |
elrafapadron | 0:ae7d06398888 | 92 | #define HMC5843_IDLE 0x02 |
elrafapadron | 0:ae7d06398888 | 93 | #define HMC5843_SLEEP 0x03 |
elrafapadron | 0:ae7d06398888 | 94 | |
elrafapadron | 0:ae7d06398888 | 95 | |
elrafapadron | 0:ae7d06398888 | 96 | |
elrafapadron | 0:ae7d06398888 | 97 | #define HMC5843_CONFIG_A 0x00 |
elrafapadron | 0:ae7d06398888 | 98 | #define HMC5843_CONFIG_B 0x01 |
elrafapadron | 0:ae7d06398888 | 99 | #define HMC5843_MODE 0x02 |
elrafapadron | 0:ae7d06398888 | 100 | #define HMC5843_X_MSB 0x03 |
elrafapadron | 0:ae7d06398888 | 101 | #define HMC5843_X_LSB 0x04 |
elrafapadron | 0:ae7d06398888 | 102 | #define HMC5843_Y_MSB 0x05 |
elrafapadron | 0:ae7d06398888 | 103 | #define HMC5843_Y_LSB 0x06 |
elrafapadron | 0:ae7d06398888 | 104 | #define HMC5843_Z_MSB 0x07 |
elrafapadron | 0:ae7d06398888 | 105 | #define HMC5843_Z_LSB 0x08 |
elrafapadron | 0:ae7d06398888 | 106 | #define HMC5843_STATUS 0x09 |
elrafapadron | 0:ae7d06398888 | 107 | #define HMC5843_IDENT_A 0x0A |
elrafapadron | 0:ae7d06398888 | 108 | #define HMC5843_IDENT_B 0x0B |
elrafapadron | 0:ae7d06398888 | 109 | #define HMC5843_IDENT_C 0x0C |
elrafapadron | 0:ae7d06398888 | 110 | |
elrafapadron | 0:ae7d06398888 | 111 | |
elrafapadron | 0:ae7d06398888 | 112 | |
elrafapadron | 0:ae7d06398888 | 113 | /** |
elrafapadron | 0:ae7d06398888 | 114 | * Honeywell HMC5843 digital compass. |
elrafapadron | 0:ae7d06398888 | 115 | */ |
elrafapadron | 0:ae7d06398888 | 116 | class HMC5843 { |
elrafapadron | 0:ae7d06398888 | 117 | |
elrafapadron | 0:ae7d06398888 | 118 | public: |
elrafapadron | 0:ae7d06398888 | 119 | |
elrafapadron | 0:ae7d06398888 | 120 | /** |
elrafapadron | 0:ae7d06398888 | 121 | * Constructor. |
elrafapadron | 0:ae7d06398888 | 122 | * |
elrafapadron | 0:ae7d06398888 | 123 | * @param sda mbed pin to use for SDA line of I2C interface. |
elrafapadron | 0:ae7d06398888 | 124 | * @param scl mbed pin to use for SCL line of I2C interface. |
elrafapadron | 0:ae7d06398888 | 125 | */ |
elrafapadron | 0:ae7d06398888 | 126 | HMC5843(PinName sda, PinName scl); |
elrafapadron | 0:ae7d06398888 | 127 | |
elrafapadron | 0:ae7d06398888 | 128 | |
elrafapadron | 0:ae7d06398888 | 129 | /** |
elrafapadron | 0:ae7d06398888 | 130 | * Enter into sleep mode. |
elrafapadron | 0:ae7d06398888 | 131 | * |
elrafapadron | 0:ae7d06398888 | 132 | */ |
elrafapadron | 0:ae7d06398888 | 133 | void setSleepMode(); |
elrafapadron | 0:ae7d06398888 | 134 | |
elrafapadron | 0:ae7d06398888 | 135 | |
elrafapadron | 0:ae7d06398888 | 136 | /** |
elrafapadron | 0:ae7d06398888 | 137 | * Set Device in Default Mode. |
elrafapadron | 0:ae7d06398888 | 138 | * HMC5843_CONTINUOUS, HMC5843_10HZ_NORMAL HMC5843_1_0GA |
elrafapadron | 0:ae7d06398888 | 139 | */ |
elrafapadron | 0:ae7d06398888 | 140 | void setDefault(); |
elrafapadron | 0:ae7d06398888 | 141 | |
elrafapadron | 2:fdab96fc6fff | 142 | |
elrafapadron | 0:ae7d06398888 | 143 | /** |
elrafapadron | 0:ae7d06398888 | 144 | * Read the memory location on the device which contains the address. |
elrafapadron | 0:ae7d06398888 | 145 | * |
elrafapadron | 0:ae7d06398888 | 146 | * @param Pointer to a buffer to hold the address value |
elrafapadron | 0:ae7d06398888 | 147 | * Expected H, 4 and 3. |
elrafapadron | 0:ae7d06398888 | 148 | */ |
elrafapadron | 0:ae7d06398888 | 149 | void getAddress(char * address); |
elrafapadron | 0:ae7d06398888 | 150 | |
elrafapadron | 0:ae7d06398888 | 151 | |
elrafapadron | 0:ae7d06398888 | 152 | |
elrafapadron | 0:ae7d06398888 | 153 | /** |
elrafapadron | 0:ae7d06398888 | 154 | * Set the operation mode. |
elrafapadron | 0:ae7d06398888 | 155 | * |
elrafapadron | 0:ae7d06398888 | 156 | * @param mode 0x00 -> Continuous |
elrafapadron | 0:ae7d06398888 | 157 | * 0x01 -> Single |
elrafapadron | 0:ae7d06398888 | 158 | * 0x02 -> Idle |
elrafapadron | 0:ae7d06398888 | 159 | * @param ConfigA values |
elrafapadron | 0:ae7d06398888 | 160 | * @param ConfigB values |
elrafapadron | 0:ae7d06398888 | 161 | */ |
elrafapadron | 0:ae7d06398888 | 162 | void setOpMode(int mode, int ConfigA, int ConfigB); |
elrafapadron | 0:ae7d06398888 | 163 | |
elrafapadron | 0:ae7d06398888 | 164 | /** |
elrafapadron | 0:ae7d06398888 | 165 | * Write to on the device. |
elrafapadron | 0:ae7d06398888 | 166 | * |
elrafapadron | 2:fdab96fc6fff | 167 | * @param address Address to write to. |
elrafapadron | 0:ae7d06398888 | 168 | * @param data Data to write. |
elrafapadron | 0:ae7d06398888 | 169 | */ |
elrafapadron | 2:fdab96fc6fff | 170 | |
elrafapadron | 0:ae7d06398888 | 171 | void write(int address, int data); |
elrafapadron | 0:ae7d06398888 | 172 | |
elrafapadron | 2:fdab96fc6fff | 173 | /** |
elrafapadron | 2:fdab96fc6fff | 174 | * Get the output of all three axes. |
elrafapadron | 0:ae7d06398888 | 175 | * |
elrafapadron | 0:ae7d06398888 | 176 | * @param Pointer to a buffer to hold the magnetics value for the |
elrafapadron | 0:ae7d06398888 | 177 | * x-axis, y-axis and z-axis [in that order]. |
elrafapadron | 0:ae7d06398888 | 178 | */ |
elrafapadron | 0:ae7d06398888 | 179 | void readData(int* readings); |
elrafapadron | 0:ae7d06398888 | 180 | |
elrafapadron | 2:fdab96fc6fff | 181 | /** |
elrafapadron | 2:fdab96fc6fff | 182 | * Get the output of X axis. |
elrafapadron | 2:fdab96fc6fff | 183 | * |
elrafapadron | 2:fdab96fc6fff | 184 | * @return x-axis magnetic value |
elrafapadron | 2:fdab96fc6fff | 185 | */ |
elrafapadron | 2:fdab96fc6fff | 186 | int getMx(); |
elrafapadron | 2:fdab96fc6fff | 187 | |
elrafapadron | 2:fdab96fc6fff | 188 | /** |
elrafapadron | 2:fdab96fc6fff | 189 | * Get the output of Y axis. |
elrafapadron | 0:ae7d06398888 | 190 | * |
elrafapadron | 2:fdab96fc6fff | 191 | * @return y-axis magnetic value |
elrafapadron | 2:fdab96fc6fff | 192 | */ |
elrafapadron | 2:fdab96fc6fff | 193 | int getMy(); |
elrafapadron | 2:fdab96fc6fff | 194 | |
elrafapadron | 2:fdab96fc6fff | 195 | /** |
elrafapadron | 2:fdab96fc6fff | 196 | * Get the output of Z axis. |
elrafapadron | 2:fdab96fc6fff | 197 | * |
elrafapadron | 2:fdab96fc6fff | 198 | * @return z-axis magnetic value |
elrafapadron | 2:fdab96fc6fff | 199 | */ |
elrafapadron | 2:fdab96fc6fff | 200 | int getMz(); |
elrafapadron | 2:fdab96fc6fff | 201 | |
elrafapadron | 2:fdab96fc6fff | 202 | |
elrafapadron | 2:fdab96fc6fff | 203 | /** |
elrafapadron | 2:fdab96fc6fff | 204 | * Get the current operation mode. |
elrafapadron | 2:fdab96fc6fff | 205 | * |
elrafapadron | 2:fdab96fc6fff | 206 | * @return Status register values |
elrafapadron | 0:ae7d06398888 | 207 | */ |
elrafapadron | 0:ae7d06398888 | 208 | int getStatus(void); |
elrafapadron | 0:ae7d06398888 | 209 | |
elrafapadron | 0:ae7d06398888 | 210 | |
elrafapadron | 0:ae7d06398888 | 211 | |
elrafapadron | 0:ae7d06398888 | 212 | I2C* i2c_; |
elrafapadron | 0:ae7d06398888 | 213 | |
elrafapadron | 0:ae7d06398888 | 214 | |
elrafapadron | 0:ae7d06398888 | 215 | |
elrafapadron | 0:ae7d06398888 | 216 | }; |
elrafapadron | 0:ae7d06398888 | 217 | |
elrafapadron | 0:ae7d06398888 | 218 | #endif /* HMC5843_H */ |