Atsumi Toda / QMC5883L

Dependents:   M-G354PDH0_serial_echo_2 M-G354PDH0_serial_echo_1 M-G354PDH0_serial_Lib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers QMC5883L.h Source File

QMC5883L.h

00001 /*     
00002 *   Copyright (c) 2015, Baser Kandehir, baser.kandehir@ieee.metu.edu.tr
00003 *
00004 *   Permission is hereby granted, free of charge, to any person obtaining a copy
00005 *   of this software and associated documentation files (the "Software"), to deal
00006 *   in the Software without restriction, including without limitation the rights
00007 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 *   copies of the Software, and to permit persons to whom the Software is
00009 *   furnished to do so, subject to the following conditions:
00010 *
00011 *   The above copyright notice and this permission notice shall be included in
00012 *   all copies or substantial portions of the Software.
00013 *
00014 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 *   THE SOFTWARE.
00021 *
00022 */
00023 
00024 // Some part of the code is adapted from Adafruit HMC5883 library
00025 
00026 #ifndef QMC5883L_H
00027 #define QMC5883L_H
00028 
00029 #include "mbed.h"
00030 #include "math.h"
00031 
00032 #define PI 3.14159265359 
00033 #define GAUSS_TO_MICROTESLA 100
00034 #define QMC5883L_ADDRESS 0x1A//0x18//
00035 
00036 /* Register Definitions */
00037 #define OUT_X_LSB    0x00
00038 #define OUT_X_MSB    0x01
00039 #define OUT_Y_LSB    0x02
00040 #define OUT_Y_MSB    0x03
00041 #define OUT_Z_LSB    0x04
00042 #define OUT_Z_MSB    0x05
00043 #define STATUS       0x06
00044 #define TEMP_LSB     0x07
00045 #define TEMP_MSB     0x08
00046 #define CONTROL_A    0x09
00047 #define CONTROL_B    0x0A
00048 #define SET_RESET    0x0B
00049 #define CHIP_ID      0x0D
00050 
00051 /* Magnetometer Gain Settings */
00052 enum MagScale
00053 {
00054     MagScale_2G =  0x00,      // +/- 2 Ga
00055     MagScale_8G  = 0x10,     // +/- 8 Ga
00056 };
00057 
00058 class QMC5883L
00059 {
00060     public:
00061         QMC5883L(PinName sda, PinName scl);
00062         void    init();
00063         double  getHeading();
00064         void    readMagData(float* dest);  
00065         int16_t getMagXvalue();
00066         int16_t getMagYvalue();
00067         int16_t getMagZvalue();
00068         int16_t getMagTemp();
00069         void    ChipID();
00070     private:
00071         float setMagRange(MagScale Mscale);
00072         uint8_t QMC5883L_ReadByte(uint8_t QMC5883L_reg);
00073         void QMC5883L_WriteByte(uint8_t QMC5883L_reg, uint8_t QMC5883L_data);
00074         I2C QMC5883L_i2c;
00075 };
00076 
00077 #endif