Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of HMC5883L by
HMC5883L.h@0:e5f8da308b60, 2015-08-05 (annotated)
- Committer:
- BaserK
- Date:
- Wed Aug 05 12:59:07 2015 +0000
- Revision:
- 0:e5f8da308b60
- Child:
- 1:4c295f793d46
first working library of compass
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BaserK | 0:e5f8da308b60 | 1 | /* |
BaserK | 0:e5f8da308b60 | 2 | * Copyright (c) 2015, Baser Kandehir, baser.kandehir@ieee.metu.edu.tr |
BaserK | 0:e5f8da308b60 | 3 | * |
BaserK | 0:e5f8da308b60 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
BaserK | 0:e5f8da308b60 | 5 | * of this software and associated documentation files (the "Software"), to deal |
BaserK | 0:e5f8da308b60 | 6 | * in the Software without restriction, including without limitation the rights |
BaserK | 0:e5f8da308b60 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
BaserK | 0:e5f8da308b60 | 8 | * copies of the Software, and to permit persons to whom the Software is |
BaserK | 0:e5f8da308b60 | 9 | * furnished to do so, subject to the following conditions: |
BaserK | 0:e5f8da308b60 | 10 | * |
BaserK | 0:e5f8da308b60 | 11 | * The above copyright notice and this permission notice shall be included in |
BaserK | 0:e5f8da308b60 | 12 | * all copies or substantial portions of the Software. |
BaserK | 0:e5f8da308b60 | 13 | * |
BaserK | 0:e5f8da308b60 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
BaserK | 0:e5f8da308b60 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
BaserK | 0:e5f8da308b60 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
BaserK | 0:e5f8da308b60 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
BaserK | 0:e5f8da308b60 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
BaserK | 0:e5f8da308b60 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
BaserK | 0:e5f8da308b60 | 20 | * THE SOFTWARE. |
BaserK | 0:e5f8da308b60 | 21 | * |
BaserK | 0:e5f8da308b60 | 22 | */ |
BaserK | 0:e5f8da308b60 | 23 | |
BaserK | 0:e5f8da308b60 | 24 | // Some part of the code is adapted from Adafruit HMC5883 library |
BaserK | 0:e5f8da308b60 | 25 | |
BaserK | 0:e5f8da308b60 | 26 | #ifndef HMC5883L_H |
BaserK | 0:e5f8da308b60 | 27 | #define HMC5883L_H |
BaserK | 0:e5f8da308b60 | 28 | |
BaserK | 0:e5f8da308b60 | 29 | #include "mbed.h" |
BaserK | 0:e5f8da308b60 | 30 | #include "math.h" |
BaserK | 0:e5f8da308b60 | 31 | #include "ledControl.h" |
BaserK | 0:e5f8da308b60 | 32 | |
BaserK | 0:e5f8da308b60 | 33 | #define PI 3.14159265359 |
BaserK | 0:e5f8da308b60 | 34 | #define GAUSS_TO_MICROTESLA 100 |
BaserK | 0:e5f8da308b60 | 35 | #define HMC5883L_ADDRESS 0x3C |
BaserK | 0:e5f8da308b60 | 36 | |
BaserK | 0:e5f8da308b60 | 37 | /* Register Definitions */ |
BaserK | 0:e5f8da308b60 | 38 | #define CONFIG_A 0x00 |
BaserK | 0:e5f8da308b60 | 39 | #define CONFIG_B 0x01 |
BaserK | 0:e5f8da308b60 | 40 | #define MODE 0x02 |
BaserK | 0:e5f8da308b60 | 41 | #define OUT_X_MSB 0x03 |
BaserK | 0:e5f8da308b60 | 42 | #define OUT_X_LSB 0x04 |
BaserK | 0:e5f8da308b60 | 43 | #define OUT_Z_MSB 0x05 |
BaserK | 0:e5f8da308b60 | 44 | #define OUT_Z_LSB 0x06 |
BaserK | 0:e5f8da308b60 | 45 | #define OUT_Y_MSB 0x07 |
BaserK | 0:e5f8da308b60 | 46 | #define OUT_Y_LSB 0x08 |
BaserK | 0:e5f8da308b60 | 47 | #define STATUS 0x09 |
BaserK | 0:e5f8da308b60 | 48 | #define ID_A 0x0A |
BaserK | 0:e5f8da308b60 | 49 | #define ID_B 0x0B |
BaserK | 0:e5f8da308b60 | 50 | #define ID_C 0x0C |
BaserK | 0:e5f8da308b60 | 51 | |
BaserK | 0:e5f8da308b60 | 52 | /* Magnetometer Gain Settings */ |
BaserK | 0:e5f8da308b60 | 53 | enum MagGain |
BaserK | 0:e5f8da308b60 | 54 | { |
BaserK | 0:e5f8da308b60 | 55 | MagGain_088 = 0x00, // +/- 0.88 Ga |
BaserK | 0:e5f8da308b60 | 56 | MagGain_13 = 0x20, // +/- 1.3 Ga |
BaserK | 0:e5f8da308b60 | 57 | MagGain_19 = 0x40, // +/- 1.9 Ga |
BaserK | 0:e5f8da308b60 | 58 | MagGain_25 = 0x60, // +/- 2.5 Ga |
BaserK | 0:e5f8da308b60 | 59 | MagGain_40 = 0x80, // +/- 4.0 Ga |
BaserK | 0:e5f8da308b60 | 60 | MagGain_47 = 0xA0, // +/- 4.7 Ga |
BaserK | 0:e5f8da308b60 | 61 | MagGain_56 = 0xC0, // +/- 5.6 Ga |
BaserK | 0:e5f8da308b60 | 62 | MagGain_81 = 0xE0 // +/- 8.1 Ga |
BaserK | 0:e5f8da308b60 | 63 | }; |
BaserK | 0:e5f8da308b60 | 64 | |
BaserK | 0:e5f8da308b60 | 65 | class HMC5883L |
BaserK | 0:e5f8da308b60 | 66 | { |
BaserK | 0:e5f8da308b60 | 67 | public: |
BaserK | 0:e5f8da308b60 | 68 | void init(); |
BaserK | 0:e5f8da308b60 | 69 | double getHeading(); |
BaserK | 0:e5f8da308b60 | 70 | private: |
BaserK | 0:e5f8da308b60 | 71 | void setMagGain(MagGain gain); |
BaserK | 0:e5f8da308b60 | 72 | void writeByte(uint8_t address, uint8_t regAddress, uint8_t data); |
BaserK | 0:e5f8da308b60 | 73 | char readByte(uint8_t address, uint8_t regAddress); |
BaserK | 0:e5f8da308b60 | 74 | void readBytes(uint8_t address, uint8_t regAddress, uint8_t byteNum, uint8_t* dest); |
BaserK | 0:e5f8da308b60 | 75 | void readMagData(float* dest); |
BaserK | 0:e5f8da308b60 | 76 | }; |
BaserK | 0:e5f8da308b60 | 77 | |
BaserK | 0:e5f8da308b60 | 78 | #endif |