Library for the HMC5883L with 5 pins

Dependents:   projet_st_connect

Fork of QMC5883L by z jx

Committer:
raminou
Date:
Tue Nov 06 16:40:11 2018 +0000
Revision:
6:37d4c4e18227
Parent:
4:2620ae5391a6
Child:
7:4136b94b5cb9
try modif to standby

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 6:37d4c4e18227 52 enum TypeMagScale
raminou 6:37d4c4e18227 53 {
raminou 6:37d4c4e18227 54 MagScale_2G = 0, // +/- 2 Ga
raminou 6:37d4c4e18227 55 MagScale_8G = 1, // +/- 8 Ga
raminou 6:37d4c4e18227 56 };
raminou 6:37d4c4e18227 57
raminou 6:37d4c4e18227 58 enum TypeState
BaserK 0:e5f8da308b60 59 {
raminou 6:37d4c4e18227 60 Standby_MODE = 0,
raminou 6:37d4c4e18227 61 Continuous_MODE = 1
raminou 6:37d4c4e18227 62 };
raminou 6:37d4c4e18227 63
raminou 6:37d4c4e18227 64 enum TypeODR
raminou 6:37d4c4e18227 65 {
raminou 6:37d4c4e18227 66 F10 = 0,
raminou 6:37d4c4e18227 67 F50 = 1,
raminou 6:37d4c4e18227 68 F100 = 2,
raminou 6:37d4c4e18227 69 F200 = 3
BaserK 0:e5f8da308b60 70 };
BaserK 0:e5f8da308b60 71
sarahbest 3:6aac221b613d 72 class QMC5883L
BaserK 0:e5f8da308b60 73 {
BaserK 0:e5f8da308b60 74 public:
raminou 4:2620ae5391a6 75 QMC5883L(PinName sda, PinName scl);
sarahbest 3:6aac221b613d 76 void init();
sarahbest 3:6aac221b613d 77 double getHeading();
sarahbest 3:6aac221b613d 78 void readMagData(float* dest);
sarahbest 3:6aac221b613d 79 int16_t getMagXvalue();
sarahbest 3:6aac221b613d 80 int16_t getMagYvalue();
sarahbest 3:6aac221b613d 81 int16_t getMagZvalue();
sarahbest 3:6aac221b613d 82 int16_t getMagTemp();
sarahbest 3:6aac221b613d 83 void ChipID();
raminou 6:37d4c4e18227 84 void standby();
raminou 6:37d4c4e18227 85 void soft_reset();
BaserK 0:e5f8da308b60 86 private:
raminou 6:37d4c4e18227 87 TypeState state;
raminou 6:37d4c4e18227 88 TypeODR odr;
raminou 6:37d4c4e18227 89 TypeMagScale mag_scale;
raminou 6:37d4c4e18227 90 float setMagRange(TypeMagScale Mscale);
raminou 6:37d4c4e18227 91 void change_odr_state(TypeMagScale mmag_scale, TypeODR modr, TypeState mstate);
raminou 4:2620ae5391a6 92 uint8_t QMC5883L_ReadByte(uint8_t QMC5883L_reg);
raminou 4:2620ae5391a6 93 void QMC5883L_WriteByte(uint8_t QMC5883L_reg, uint8_t QMC5883L_data);
raminou 4:2620ae5391a6 94 I2C QMC5883L_i2c;
BaserK 0:e5f8da308b60 95 };
BaserK 0:e5f8da308b60 96
raminou 4:2620ae5391a6 97 #endif