Library for the HMC5883L with 5 pins

Dependents:   projet_st_connect

Fork of QMC5883L by z jx

Committer:
raminou
Date:
Tue Nov 13 14:37:19 2018 +0000
Revision:
5:d046009bc7c3
Parent:
4:2620ae5391a6
Add standby mode, reset

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
sarahbest 3:6aac221b613d 26 #ifndef QMC5883L_H
sarahbest 3:6aac221b613d 27 #define QMC5883L_H
BaserK 0:e5f8da308b60 28
BaserK 0:e5f8da308b60 29 #include "mbed.h"
BaserK 0:e5f8da308b60 30 #include "math.h"
BaserK 0:e5f8da308b60 31
raminou 5:d046009bc7c3 32 // #define DEBUG 1
BaserK 0:e5f8da308b60 33 #define PI 3.14159265359
BaserK 0:e5f8da308b60 34 #define GAUSS_TO_MICROTESLA 100
sarahbest 3:6aac221b613d 35 #define QMC5883L_ADDRESS 0x1A//0x18//
BaserK 0:e5f8da308b60 36
BaserK 0:e5f8da308b60 37 /* Register Definitions */
sarahbest 3:6aac221b613d 38 #define OUT_X_LSB 0x00
sarahbest 3:6aac221b613d 39 #define OUT_X_MSB 0x01
sarahbest 3:6aac221b613d 40 #define OUT_Y_LSB 0x02
sarahbest 3:6aac221b613d 41 #define OUT_Y_MSB 0x03
sarahbest 3:6aac221b613d 42 #define OUT_Z_LSB 0x04
BaserK 0:e5f8da308b60 43 #define OUT_Z_MSB 0x05
sarahbest 3:6aac221b613d 44 #define STATUS 0x06
sarahbest 3:6aac221b613d 45 #define TEMP_LSB 0x07
sarahbest 3:6aac221b613d 46 #define TEMP_MSB 0x08
sarahbest 3:6aac221b613d 47 #define CONTROL_A 0x09
sarahbest 3:6aac221b613d 48 #define CONTROL_B 0x0A
sarahbest 3:6aac221b613d 49 #define SET_RESET 0x0B
sarahbest 3:6aac221b613d 50 #define CHIP_ID 0x0D
BaserK 0:e5f8da308b60 51
BaserK 0:e5f8da308b60 52 /* Magnetometer Gain Settings */
sarahbest 3:6aac221b613d 53 enum MagScale
BaserK 0:e5f8da308b60 54 {
raminou 5:d046009bc7c3 55 MagScale_2G = 0x0, // +/- 2 Ga
raminou 5:d046009bc7c3 56 MagScale_8G = 0x1, // +/- 8 Ga
raminou 5:d046009bc7c3 57 };
raminou 5:d046009bc7c3 58
raminou 5:d046009bc7c3 59 enum OSR{
raminou 5:d046009bc7c3 60 osr512 = 0x0,
raminou 5:d046009bc7c3 61 osr256 = 0x1,
raminou 5:d046009bc7c3 62 osr128 = 0x2,
raminou 5:d046009bc7c3 63 osr64 = 0x3
raminou 5:d046009bc7c3 64 };
raminou 5:d046009bc7c3 65
raminou 5:d046009bc7c3 66 enum Mode{
raminou 5:d046009bc7c3 67 mode_standby = 0x0,
raminou 5:d046009bc7c3 68 mode_continuous = 0x1
raminou 5:d046009bc7c3 69 };
raminou 5:d046009bc7c3 70
raminou 5:d046009bc7c3 71 enum ODR{
raminou 5:d046009bc7c3 72 odr_1Ohz = 0x0,
raminou 5:d046009bc7c3 73 odr_50hz = 0x1,
raminou 5:d046009bc7c3 74 odr_100hz = 0x2,
raminou 5:d046009bc7c3 75 odr_200hz = 0x3
BaserK 0:e5f8da308b60 76 };
BaserK 0:e5f8da308b60 77
sarahbest 3:6aac221b613d 78 class QMC5883L
BaserK 0:e5f8da308b60 79 {
BaserK 0:e5f8da308b60 80 public:
raminou 4:2620ae5391a6 81 QMC5883L(PinName sda, PinName scl);
sarahbest 3:6aac221b613d 82 void init();
raminou 5:d046009bc7c3 83 void changeState(OSR osr, MagScale rng, ODR odr, Mode mode);
raminou 5:d046009bc7c3 84 void standby();
raminou 5:d046009bc7c3 85 void reset();
sarahbest 3:6aac221b613d 86 double getHeading();
sarahbest 3:6aac221b613d 87 void readMagData(float* dest);
sarahbest 3:6aac221b613d 88 int16_t getMagXvalue();
sarahbest 3:6aac221b613d 89 int16_t getMagYvalue();
sarahbest 3:6aac221b613d 90 int16_t getMagZvalue();
sarahbest 3:6aac221b613d 91 int16_t getMagTemp();
sarahbest 3:6aac221b613d 92 void ChipID();
BaserK 0:e5f8da308b60 93 private:
sarahbest 3:6aac221b613d 94 float setMagRange(MagScale Mscale);
raminou 4:2620ae5391a6 95 uint8_t QMC5883L_ReadByte(uint8_t QMC5883L_reg);
raminou 4:2620ae5391a6 96 void QMC5883L_WriteByte(uint8_t QMC5883L_reg, uint8_t QMC5883L_data);
raminou 4:2620ae5391a6 97 I2C QMC5883L_i2c;
BaserK 0:e5f8da308b60 98 };
BaserK 0:e5f8da308b60 99
raminou 4:2620ae5391a6 100 #endif