HMC5883L Digital Compass Library

Fork of HMC5883L by Baser Kandehir

Committer:
brunohorta
Date:
Wed May 30 19:14:58 2018 +0000
Revision:
3:f14ad02770e4
Parent:
1:4c295f793d46
ble

Who changed what in which revision?

UserRevisionLine numberNew 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"
brunohorta 3:f14ad02770e4 31 #define M_PI 3.14159265359
BaserK 0:e5f8da308b60 32 #define PI 3.14159265359
BaserK 0:e5f8da308b60 33 #define GAUSS_TO_MICROTESLA 100
BaserK 0:e5f8da308b60 34 #define HMC5883L_ADDRESS 0x3C
BaserK 0:e5f8da308b60 35
BaserK 0:e5f8da308b60 36 /* Register Definitions */
BaserK 0:e5f8da308b60 37 #define CONFIG_A 0x00
BaserK 0:e5f8da308b60 38 #define CONFIG_B 0x01
BaserK 0:e5f8da308b60 39 #define MODE 0x02
BaserK 0:e5f8da308b60 40 #define OUT_X_MSB 0x03
BaserK 0:e5f8da308b60 41 #define OUT_X_LSB 0x04
BaserK 0:e5f8da308b60 42 #define OUT_Z_MSB 0x05
BaserK 0:e5f8da308b60 43 #define OUT_Z_LSB 0x06
BaserK 0:e5f8da308b60 44 #define OUT_Y_MSB 0x07
BaserK 0:e5f8da308b60 45 #define OUT_Y_LSB 0x08
BaserK 0:e5f8da308b60 46 #define STATUS 0x09
BaserK 0:e5f8da308b60 47 #define ID_A 0x0A
BaserK 0:e5f8da308b60 48 #define ID_B 0x0B
BaserK 0:e5f8da308b60 49 #define ID_C 0x0C
brunohorta 3:f14ad02770e4 50 #define COMPASS_SCALE_088 0x00 << 2
brunohorta 3:f14ad02770e4 51 #define COMPASS_SCALE_130 0x01 << 2
brunohorta 3:f14ad02770e4 52 #define COMPASS_SCALE_190 0x02 << 2
brunohorta 3:f14ad02770e4 53 #define COMPASS_SCALE_250 0x03 << 2
brunohorta 3:f14ad02770e4 54 #define COMPASS_SCALE_400 0x04 << 2
brunohorta 3:f14ad02770e4 55 #define COMPASS_SCALE_470 0x05 << 2
brunohorta 3:f14ad02770e4 56 #define COMPASS_SCALE_560 0x06 << 2
brunohorta 3:f14ad02770e4 57 #define COMPASS_SCALE_810 0x07 << 2
BaserK 0:e5f8da308b60 58
brunohorta 3:f14ad02770e4 59 // xxXXXYYYZZZxxxxx
brunohorta 3:f14ad02770e4 60 // ORIENTATION:
brunohorta 3:f14ad02770e4 61 #define COMPASS_NORTH 0x00
brunohorta 3:f14ad02770e4 62 #define COMPASS_SOUTH 0x01
brunohorta 3:f14ad02770e4 63 #define COMPASS_WEST 0x02
brunohorta 3:f14ad02770e4 64 #define COMPASS_EAST 0x03
brunohorta 3:f14ad02770e4 65 #define COMPASS_UP 0x04
brunohorta 3:f14ad02770e4 66 #define COMPASS_DOWN 0x05
brunohorta 3:f14ad02770e4 67 #define COMPASS_CONTINUOUS 0x00
brunohorta 3:f14ad02770e4 68 #define COMPASS_SINGLE 0x01
brunohorta 3:f14ad02770e4 69 #define COMPASS_IDLE 0x02
brunohorta 3:f14ad02770e4 70
brunohorta 3:f14ad02770e4 71
brunohorta 3:f14ad02770e4 72 // When "pointing" north, define the direction of each of the silkscreen'd arrows
brunohorta 3:f14ad02770e4 73 // (imagine the Z arrow points out of the top of the device) only N/S/E/W are allowed
brunohorta 3:f14ad02770e4 74 #define COMPASS_HORIZONTAL_X_NORTH ( (COMPASS_NORTH << 6) | (COMPASS_WEST << 3) | COMPASS_UP ) << 5
brunohorta 3:f14ad02770e4 75 #define COMPASS_HORIZONTAL_Y_NORTH ( (COMPASS_EAST << 6) | (COMPASS_NORTH << 3) | COMPASS_UP ) << 5
brunohorta 3:f14ad02770e4 76 #define COMPASS_VERTICAL_X_EAST ( (COMPASS_EAST << 6) | (COMPASS_UP << 3) | COMPASS_SOUTH ) << 5
brunohorta 3:f14ad02770e4 77 #define COMPASS_VERTICAL_Y_WEST ( (COMPASS_UP << 6) | (COMPASS_WEST << 3) | COMPASS_SOUTH ) << 5
BaserK 0:e5f8da308b60 78 /* Magnetometer Gain Settings */
brunohorta 3:f14ad02770e4 79
brunohorta 3:f14ad02770e4 80
BaserK 0:e5f8da308b60 81 enum MagGain
BaserK 0:e5f8da308b60 82 {
BaserK 0:e5f8da308b60 83 MagGain_088 = 0x00, // +/- 0.88 Ga
BaserK 0:e5f8da308b60 84 MagGain_13 = 0x20, // +/- 1.3 Ga
BaserK 0:e5f8da308b60 85 MagGain_19 = 0x40, // +/- 1.9 Ga
BaserK 0:e5f8da308b60 86 MagGain_25 = 0x60, // +/- 2.5 Ga
BaserK 0:e5f8da308b60 87 MagGain_40 = 0x80, // +/- 4.0 Ga
BaserK 0:e5f8da308b60 88 MagGain_47 = 0xA0, // +/- 4.7 Ga
BaserK 0:e5f8da308b60 89 MagGain_56 = 0xC0, // +/- 5.6 Ga
BaserK 0:e5f8da308b60 90 MagGain_81 = 0xE0 // +/- 8.1 Ga
BaserK 0:e5f8da308b60 91 };
BaserK 0:e5f8da308b60 92
BaserK 0:e5f8da308b60 93 class HMC5883L
BaserK 0:e5f8da308b60 94 {
BaserK 0:e5f8da308b60 95 public:
BaserK 0:e5f8da308b60 96 void init();
BaserK 0:e5f8da308b60 97 double getHeading();
BaserK 1:4c295f793d46 98 void readMagData(float* dest);
brunohorta 3:f14ad02770e4 99 void setScale(uint16_t sampling_mode );
brunohorta 3:f14ad02770e4 100 void setOrientation(uint16_t sampling_mode );
BaserK 0:e5f8da308b60 101 private:
brunohorta 3:f14ad02770e4 102
BaserK 0:e5f8da308b60 103 void writeByte(uint8_t address, uint8_t regAddress, uint8_t data);
BaserK 0:e5f8da308b60 104 char readByte(uint8_t address, uint8_t regAddress);
BaserK 0:e5f8da308b60 105 void readBytes(uint8_t address, uint8_t regAddress, uint8_t byteNum, uint8_t* dest);
BaserK 0:e5f8da308b60 106 };
BaserK 0:e5f8da308b60 107
BaserK 0:e5f8da308b60 108 #endif