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
sarahbest 3:6aac221b613d 1 /* QMC5883L Digital Compass Library
BaserK 0:e5f8da308b60 2 *
BaserK 0:e5f8da308b60 3 * @author: Baser Kandehir
BaserK 0:e5f8da308b60 4 * @date: August 5, 2015
BaserK 0:e5f8da308b60 5 * @license: MIT license
BaserK 0:e5f8da308b60 6 *
BaserK 0:e5f8da308b60 7 * Copyright (c) 2015, Baser Kandehir, baser.kandehir@ieee.metu.edu.tr
BaserK 0:e5f8da308b60 8 *
BaserK 0:e5f8da308b60 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
BaserK 0:e5f8da308b60 10 * of this software and associated documentation files (the "Software"), to deal
BaserK 0:e5f8da308b60 11 * in the Software without restriction, including without limitation the rights
BaserK 0:e5f8da308b60 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
BaserK 0:e5f8da308b60 13 * copies of the Software, and to permit persons to whom the Software is
BaserK 0:e5f8da308b60 14 * furnished to do so, subject to the following conditions:
BaserK 0:e5f8da308b60 15 *
BaserK 0:e5f8da308b60 16 * The above copyright notice and this permission notice shall be included in
BaserK 0:e5f8da308b60 17 * all copies or substantial portions of the Software.
BaserK 0:e5f8da308b60 18 *
BaserK 0:e5f8da308b60 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
BaserK 0:e5f8da308b60 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
BaserK 0:e5f8da308b60 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
BaserK 0:e5f8da308b60 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
BaserK 0:e5f8da308b60 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
BaserK 0:e5f8da308b60 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
BaserK 0:e5f8da308b60 25 * THE SOFTWARE.
BaserK 0:e5f8da308b60 26 *
BaserK 0:e5f8da308b60 27 */
BaserK 0:e5f8da308b60 28
BaserK 0:e5f8da308b60 29 // Some part of the code is adapted from Adafruit HMC5883 library
BaserK 0:e5f8da308b60 30
sarahbest 3:6aac221b613d 31 #include "QMC5883L.h"
BaserK 0:e5f8da308b60 32
raminou 4:2620ae5391a6 33 QMC5883L::QMC5883L(PinName sda, PinName scl): QMC5883L_i2c(sda, scl)
raminou 4:2620ae5391a6 34 {
raminou 4:2620ae5391a6 35 }
raminou 4:2620ae5391a6 36
raminou 7:4136b94b5cb9 37 float QMC5883L::setMagRange(MagScale Mscale)
raminou 4:2620ae5391a6 38 {
raminou 4:2620ae5391a6 39 float mRes; // Varies with gain
raminou 4:2620ae5391a6 40
sarahbest 3:6aac221b613d 41 switch(Mscale)
BaserK 0:e5f8da308b60 42 {
sarahbest 3:6aac221b613d 43 case MagScale_2G:
sarahbest 3:6aac221b613d 44 mRes = 1.0/12000; //LSB/G
BaserK 0:e5f8da308b60 45 break;
sarahbest 3:6aac221b613d 46 case MagScale_8G:
sarahbest 3:6aac221b613d 47 mRes = 1.0/3000;
BaserK 0:e5f8da308b60 48 break;
sarahbest 3:6aac221b613d 49 }
sarahbest 3:6aac221b613d 50 return mRes;
BaserK 0:e5f8da308b60 51 }
BaserK 0:e5f8da308b60 52
raminou 4:2620ae5391a6 53 void QMC5883L::QMC5883L_WriteByte(uint8_t QMC5883L_reg, uint8_t QMC5883L_data)
BaserK 0:e5f8da308b60 54 {
sarahbest 3:6aac221b613d 55 char data_out[2];
sarahbest 3:6aac221b613d 56 data_out[0]=QMC5883L_reg;
sarahbest 3:6aac221b613d 57 data_out[1]=QMC5883L_data;
raminou 4:2620ae5391a6 58 this->QMC5883L_i2c.write(QMC5883L_ADDRESS, data_out, 2, 0);
BaserK 0:e5f8da308b60 59 }
BaserK 0:e5f8da308b60 60
raminou 4:2620ae5391a6 61 uint8_t QMC5883L::QMC5883L_ReadByte(uint8_t QMC5883L_reg)
BaserK 0:e5f8da308b60 62 {
sarahbest 3:6aac221b613d 63 char data_out[1], data_in[1];
sarahbest 3:6aac221b613d 64 data_out[0] = QMC5883L_reg;
raminou 4:2620ae5391a6 65 this->QMC5883L_i2c.write(QMC5883L_ADDRESS, data_out, 1, 1);
raminou 4:2620ae5391a6 66 this->QMC5883L_i2c.read(QMC5883L_ADDRESS, data_in, 1, 0);
sarahbest 3:6aac221b613d 67 return (data_in[0]);
BaserK 0:e5f8da308b60 68 }
BaserK 0:e5f8da308b60 69
sarahbest 3:6aac221b613d 70 void QMC5883L::ChipID()
BaserK 0:e5f8da308b60 71 {
raminou 4:2620ae5391a6 72 uint8_t ChipID = QMC5883L_ReadByte(CHIP_ID); // Should return 0x68
sarahbest 3:6aac221b613d 73 }
sarahbest 3:6aac221b613d 74
sarahbest 3:6aac221b613d 75 void QMC5883L::init()
raminou 7:4136b94b5cb9 76 {
sarahbest 3:6aac221b613d 77 setMagRange(MagScale_8G);
raminou 7:4136b94b5cb9 78 QMC5883L_WriteByte(CONTROL_A, 0x0D | MagScale_8G); // Range: 8G, ODR: 200 Hz, mode:Continuous-Measurement
sarahbest 3:6aac221b613d 79 QMC5883L_WriteByte(SET_RESET, 0x01);
BaserK 0:e5f8da308b60 80 wait_ms(10);
BaserK 0:e5f8da308b60 81 }
BaserK 0:e5f8da308b60 82
sarahbest 3:6aac221b613d 83 int16_t QMC5883L::getMagXvalue()
sarahbest 3:6aac221b613d 84 {
sarahbest 3:6aac221b613d 85 uint8_t LoByte, HiByte;
sarahbest 3:6aac221b613d 86 LoByte = QMC5883L_ReadByte(OUT_X_LSB); // read Accelerometer X_Low value
sarahbest 3:6aac221b613d 87 HiByte = QMC5883L_ReadByte(OUT_X_MSB); // read Accelerometer X_High value
sarahbest 3:6aac221b613d 88 return((HiByte<<8) | LoByte);
sarahbest 3:6aac221b613d 89 }
sarahbest 3:6aac221b613d 90
sarahbest 3:6aac221b613d 91 int16_t QMC5883L::getMagYvalue()
BaserK 0:e5f8da308b60 92 {
sarahbest 3:6aac221b613d 93 uint8_t LoByte, HiByte;
sarahbest 3:6aac221b613d 94 LoByte = QMC5883L_ReadByte(OUT_Y_LSB); // read Accelerometer X_Low value
sarahbest 3:6aac221b613d 95 HiByte = QMC5883L_ReadByte(OUT_Y_MSB); // read Accelerometer X_High value
sarahbest 3:6aac221b613d 96 return ((HiByte<<8) | LoByte);
sarahbest 3:6aac221b613d 97 }
sarahbest 3:6aac221b613d 98
sarahbest 3:6aac221b613d 99 int16_t QMC5883L::getMagZvalue()
sarahbest 3:6aac221b613d 100 {
sarahbest 3:6aac221b613d 101 uint8_t LoByte, HiByte;
sarahbest 3:6aac221b613d 102 LoByte = QMC5883L_ReadByte(OUT_Z_LSB); // read Accelerometer X_Low value
sarahbest 3:6aac221b613d 103 HiByte = QMC5883L_ReadByte(OUT_Z_MSB); // read Accelerometer X_High value
sarahbest 3:6aac221b613d 104 return ((HiByte<<8) | LoByte);
sarahbest 3:6aac221b613d 105 }
sarahbest 3:6aac221b613d 106
sarahbest 3:6aac221b613d 107 int16_t QMC5883L::getMagTemp()
sarahbest 3:6aac221b613d 108 {
sarahbest 3:6aac221b613d 109 uint8_t LoByte, HiByte;
sarahbest 3:6aac221b613d 110 LoByte = QMC5883L_ReadByte(TEMP_LSB); // read Accelerometer X_Low value
sarahbest 3:6aac221b613d 111 HiByte = QMC5883L_ReadByte(TEMP_MSB); // read Accelerometer X_High value
sarahbest 3:6aac221b613d 112 return ((HiByte<<8) | LoByte);
raminou 4:2620ae5391a6 113 }