Library for the HMC5883L with 5 pins

Dependents:   projet_st_connect

Fork of QMC5883L by z jx

Committer:
raminou
Date:
Mon Nov 12 10:24:29 2018 +0000
Revision:
7:4136b94b5cb9
Parent:
6:37d4c4e18227
nc

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
BaserK 0:e5f8da308b60 32 #define PI 3.14159265359
BaserK 0:e5f8da308b60 33 #define GAUSS_TO_MICROTESLA 100
sarahbest 3:6aac221b613d 34 #define QMC5883L_ADDRESS 0x1A//0x18//
BaserK 0:e5f8da308b60 35
BaserK 0:e5f8da308b60 36 /* Register Definitions */
sarahbest 3:6aac221b613d 37 #define OUT_X_LSB 0x00
sarahbest 3:6aac221b613d 38 #define OUT_X_MSB 0x01
sarahbest 3:6aac221b613d 39 #define OUT_Y_LSB 0x02
sarahbest 3:6aac221b613d 40 #define OUT_Y_MSB 0x03
sarahbest 3:6aac221b613d 41 #define OUT_Z_LSB 0x04
BaserK 0:e5f8da308b60 42 #define OUT_Z_MSB 0x05
sarahbest 3:6aac221b613d 43 #define STATUS 0x06
sarahbest 3:6aac221b613d 44 #define TEMP_LSB 0x07
sarahbest 3:6aac221b613d 45 #define TEMP_MSB 0x08
sarahbest 3:6aac221b613d 46 #define CONTROL_A 0x09
sarahbest 3:6aac221b613d 47 #define CONTROL_B 0x0A
sarahbest 3:6aac221b613d 48 #define SET_RESET 0x0B
sarahbest 3:6aac221b613d 49 #define CHIP_ID 0x0D
BaserK 0:e5f8da308b60 50
BaserK 0:e5f8da308b60 51 /* Magnetometer Gain Settings */
raminou 7:4136b94b5cb9 52 enum MagScale
BaserK 0:e5f8da308b60 53 {
raminou 7:4136b94b5cb9 54 MagScale_2G = 0x00, // +/- 2 Ga
raminou 7:4136b94b5cb9 55 MagScale_8G = 0x10, // +/- 8 Ga
BaserK 0:e5f8da308b60 56 };
BaserK 0:e5f8da308b60 57
sarahbest 3:6aac221b613d 58 class QMC5883L
BaserK 0:e5f8da308b60 59 {
BaserK 0:e5f8da308b60 60 public:
raminou 4:2620ae5391a6 61 QMC5883L(PinName sda, PinName scl);
sarahbest 3:6aac221b613d 62 void init();
sarahbest 3:6aac221b613d 63 double getHeading();
sarahbest 3:6aac221b613d 64 void readMagData(float* dest);
sarahbest 3:6aac221b613d 65 int16_t getMagXvalue();
sarahbest 3:6aac221b613d 66 int16_t getMagYvalue();
sarahbest 3:6aac221b613d 67 int16_t getMagZvalue();
sarahbest 3:6aac221b613d 68 int16_t getMagTemp();
sarahbest 3:6aac221b613d 69 void ChipID();
BaserK 0:e5f8da308b60 70 private:
raminou 7:4136b94b5cb9 71 float setMagRange(MagScale Mscale);
raminou 4:2620ae5391a6 72 uint8_t QMC5883L_ReadByte(uint8_t QMC5883L_reg);
raminou 4:2620ae5391a6 73 void QMC5883L_WriteByte(uint8_t QMC5883L_reg, uint8_t QMC5883L_data);
raminou 4:2620ae5391a6 74 I2C QMC5883L_i2c;
BaserK 0:e5f8da308b60 75 };
BaserK 0:e5f8da308b60 76
raminou 4:2620ae5391a6 77 #endif