It's used as aid for inertial measurement system

Dependents:   M-G354PDH0_serial_echo_2 M-G354PDH0_serial_echo_1 M-G354PDH0_serial_Lib

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
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
sarahbest 3:6aac221b613d 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 5:d046009bc7c3 58 #ifdef DEBUG
raminou 5:d046009bc7c3 59 printf("WRITE: %d, %x, %x\r\n", QMC5883L_ADDRESS, data_out[0], data_out[1]);
raminou 5:d046009bc7c3 60 #endif
raminou 4:2620ae5391a6 61 this->QMC5883L_i2c.write(QMC5883L_ADDRESS, data_out, 2, 0);
BaserK 0:e5f8da308b60 62 }
BaserK 0:e5f8da308b60 63
raminou 4:2620ae5391a6 64 uint8_t QMC5883L::QMC5883L_ReadByte(uint8_t QMC5883L_reg)
BaserK 0:e5f8da308b60 65 {
sarahbest 3:6aac221b613d 66 char data_out[1], data_in[1];
sarahbest 3:6aac221b613d 67 data_out[0] = QMC5883L_reg;
raminou 5:d046009bc7c3 68 #ifdef DEBUG
raminou 5:d046009bc7c3 69 printf("RWRITE: %d, %x\r\n", QMC5883L_ADDRESS, data_out[0]);
raminou 5:d046009bc7c3 70 #endif
raminou 4:2620ae5391a6 71 this->QMC5883L_i2c.write(QMC5883L_ADDRESS, data_out, 1, 1);
raminou 4:2620ae5391a6 72 this->QMC5883L_i2c.read(QMC5883L_ADDRESS, data_in, 1, 0);
raminou 5:d046009bc7c3 73 #ifdef DEBUG
raminou 5:d046009bc7c3 74 printf("Read: %d, %x\r\n", QMC5883L_ADDRESS, data_in);
raminou 5:d046009bc7c3 75 #endif
sarahbest 3:6aac221b613d 76 return (data_in[0]);
BaserK 0:e5f8da308b60 77 }
BaserK 0:e5f8da308b60 78
sarahbest 3:6aac221b613d 79 void QMC5883L::ChipID()
BaserK 0:e5f8da308b60 80 {
raminou 4:2620ae5391a6 81 uint8_t ChipID = QMC5883L_ReadByte(CHIP_ID); // Should return 0x68
sarahbest 3:6aac221b613d 82 }
sarahbest 3:6aac221b613d 83
sarahbest 3:6aac221b613d 84 void QMC5883L::init()
sarahbest 3:6aac221b613d 85 {
raminou 5:d046009bc7c3 86 changeState(osr512, MagScale_8G, odr_200hz, mode_continuous);
sarahbest 3:6aac221b613d 87 QMC5883L_WriteByte(SET_RESET, 0x01);
BaserK 0:e5f8da308b60 88 wait_ms(10);
BaserK 0:e5f8da308b60 89 }
BaserK 0:e5f8da308b60 90
raminou 5:d046009bc7c3 91 void QMC5883L::changeState(OSR osr, MagScale rng, ODR odr, Mode mode)
raminou 5:d046009bc7c3 92 {
raminou 5:d046009bc7c3 93 char tmp = ((osr & 0x3) << 6) | ((rng & 0x3) << 4) | ((odr & 0x3) << 2) | (mode & 0x3);
raminou 5:d046009bc7c3 94 QMC5883L_WriteByte(CONTROL_A, tmp);
raminou 5:d046009bc7c3 95 }
raminou 5:d046009bc7c3 96
raminou 5:d046009bc7c3 97 void QMC5883L::standby()
raminou 5:d046009bc7c3 98 {
raminou 5:d046009bc7c3 99 // Standby mode
raminou 5:d046009bc7c3 100 changeState(osr512, MagScale_2G, odr_1Ohz, mode_standby);
raminou 5:d046009bc7c3 101 wait_ms(10);
raminou 5:d046009bc7c3 102 }
raminou 5:d046009bc7c3 103
raminou 5:d046009bc7c3 104 void QMC5883L::reset()
raminou 5:d046009bc7c3 105 {
raminou 5:d046009bc7c3 106 // Reset
raminou 5:d046009bc7c3 107 QMC5883L_WriteByte(CONTROL_B, 0x80);
raminou 5:d046009bc7c3 108 wait_ms(10);
raminou 5:d046009bc7c3 109
raminou 5:d046009bc7c3 110 // Config de base
raminou 5:d046009bc7c3 111 init();
raminou 5:d046009bc7c3 112 }
raminou 5:d046009bc7c3 113
sarahbest 3:6aac221b613d 114 int16_t QMC5883L::getMagXvalue()
sarahbest 3:6aac221b613d 115 {
sarahbest 3:6aac221b613d 116 uint8_t LoByte, HiByte;
sarahbest 3:6aac221b613d 117 LoByte = QMC5883L_ReadByte(OUT_X_LSB); // read Accelerometer X_Low value
sarahbest 3:6aac221b613d 118 HiByte = QMC5883L_ReadByte(OUT_X_MSB); // read Accelerometer X_High value
sarahbest 3:6aac221b613d 119 return((HiByte<<8) | LoByte);
sarahbest 3:6aac221b613d 120 }
sarahbest 3:6aac221b613d 121
sarahbest 3:6aac221b613d 122 int16_t QMC5883L::getMagYvalue()
BaserK 0:e5f8da308b60 123 {
sarahbest 3:6aac221b613d 124 uint8_t LoByte, HiByte;
sarahbest 3:6aac221b613d 125 LoByte = QMC5883L_ReadByte(OUT_Y_LSB); // read Accelerometer X_Low value
sarahbest 3:6aac221b613d 126 HiByte = QMC5883L_ReadByte(OUT_Y_MSB); // read Accelerometer X_High value
sarahbest 3:6aac221b613d 127 return ((HiByte<<8) | LoByte);
sarahbest 3:6aac221b613d 128 }
sarahbest 3:6aac221b613d 129
sarahbest 3:6aac221b613d 130 int16_t QMC5883L::getMagZvalue()
sarahbest 3:6aac221b613d 131 {
sarahbest 3:6aac221b613d 132 uint8_t LoByte, HiByte;
sarahbest 3:6aac221b613d 133 LoByte = QMC5883L_ReadByte(OUT_Z_LSB); // read Accelerometer X_Low value
sarahbest 3:6aac221b613d 134 HiByte = QMC5883L_ReadByte(OUT_Z_MSB); // read Accelerometer X_High value
sarahbest 3:6aac221b613d 135 return ((HiByte<<8) | LoByte);
sarahbest 3:6aac221b613d 136 }
sarahbest 3:6aac221b613d 137
sarahbest 3:6aac221b613d 138 int16_t QMC5883L::getMagTemp()
sarahbest 3:6aac221b613d 139 {
sarahbest 3:6aac221b613d 140 uint8_t LoByte, HiByte;
sarahbest 3:6aac221b613d 141 LoByte = QMC5883L_ReadByte(TEMP_LSB); // read Accelerometer X_Low value
sarahbest 3:6aac221b613d 142 HiByte = QMC5883L_ReadByte(TEMP_MSB); // read Accelerometer X_High value
sarahbest 3:6aac221b613d 143 return ((HiByte<<8) | LoByte);
raminou 4:2620ae5391a6 144 }