MAG3110 magnetometer library

Dependents:   FRDM-FXS-MULTI2-B_SensorShield_HelloWorld xtrinsic_sensors

Committer:
AswinSivakumar
Date:
Mon Apr 24 19:09:26 2017 +0000
Revision:
1:4194a41cfd16
Parent:
0:f430a728163a
updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AswinSivakumar 1:4194a41cfd16 1 /*
AswinSivakumar 1:4194a41cfd16 2 * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
AswinSivakumar 1:4194a41cfd16 3 * Copyright 2016-2017 NXP
AswinSivakumar 1:4194a41cfd16 4 *
AswinSivakumar 1:4194a41cfd16 5 * Redistribution and use in source and binary forms, with or without modification,
AswinSivakumar 1:4194a41cfd16 6 * are permitted provided that the following conditions are met:
AswinSivakumar 1:4194a41cfd16 7 *
AswinSivakumar 1:4194a41cfd16 8 * o Redistributions of source code must retain the above copyright notice, this list
AswinSivakumar 1:4194a41cfd16 9 * of conditions and the following disclaimer.
AswinSivakumar 1:4194a41cfd16 10 *
AswinSivakumar 1:4194a41cfd16 11 * o Redistributions in binary form must reproduce the above copyright notice, this
AswinSivakumar 1:4194a41cfd16 12 * list of conditions and the following disclaimer in the documentation and/or
AswinSivakumar 1:4194a41cfd16 13 * other materials provided with the distribution.
AswinSivakumar 1:4194a41cfd16 14 *
AswinSivakumar 1:4194a41cfd16 15 * o Neither the name of the copyright holder nor the names of its
AswinSivakumar 1:4194a41cfd16 16 * contributors may be used to endorse or promote products derived from this
AswinSivakumar 1:4194a41cfd16 17 * software without specific prior written permission.
AswinSivakumar 1:4194a41cfd16 18 *
AswinSivakumar 1:4194a41cfd16 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
AswinSivakumar 1:4194a41cfd16 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
AswinSivakumar 1:4194a41cfd16 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
AswinSivakumar 1:4194a41cfd16 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
AswinSivakumar 1:4194a41cfd16 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
AswinSivakumar 1:4194a41cfd16 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
AswinSivakumar 1:4194a41cfd16 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
AswinSivakumar 1:4194a41cfd16 26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
AswinSivakumar 1:4194a41cfd16 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
AswinSivakumar 1:4194a41cfd16 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
AswinSivakumar 1:4194a41cfd16 29 */
AswinSivakumar 0:f430a728163a 30
AswinSivakumar 0:f430a728163a 31 #include "MAG3110.h"
AswinSivakumar 0:f430a728163a 32 #include "mbed.h"
AswinSivakumar 0:f430a728163a 33
AswinSivakumar 0:f430a728163a 34 MAG3110::MAG3110(PinName sda, PinName scl) : MAG3110_i2c(sda,scl)
AswinSivakumar 0:f430a728163a 35 {
AswinSivakumar 0:f430a728163a 36
AswinSivakumar 0:f430a728163a 37 }
AswinSivakumar 0:f430a728163a 38
AswinSivakumar 0:f430a728163a 39
AswinSivakumar 0:f430a728163a 40 void MAG3110::MAG3110_config(void)
AswinSivakumar 0:f430a728163a 41 {
AswinSivakumar 0:f430a728163a 42 char d[2];
AswinSivakumar 0:f430a728163a 43 d[0] = MAG3110_CTRL_REG1; //Puts device in Standby mode
AswinSivakumar 0:f430a728163a 44 d[1] = 0x00;
AswinSivakumar 0:f430a728163a 45 MAG3110_i2c.write(MAG3110_I2C_ADDRESS, d,2);
AswinSivakumar 0:f430a728163a 46
AswinSivakumar 0:f430a728163a 47
AswinSivakumar 0:f430a728163a 48 d[0] = MAG3110_CTRL_REG2; //Set Automatic Magnetic Sensor Reset bit
AswinSivakumar 0:f430a728163a 49 d[1] = 0x80;
AswinSivakumar 0:f430a728163a 50 MAG3110_i2c.write(MAG3110_I2C_ADDRESS, d, 2);
AswinSivakumar 0:f430a728163a 51
AswinSivakumar 0:f430a728163a 52 d[0] = MAG3110_CTRL_REG1; //Set ODR=40Hz and OSR=32 and set device to active mode.
AswinSivakumar 0:f430a728163a 53 d[1] = 0x09;
AswinSivakumar 0:f430a728163a 54 MAG3110_i2c.write(MAG3110_I2C_ADDRESS, d, 2);
AswinSivakumar 0:f430a728163a 55
AswinSivakumar 0:f430a728163a 56
AswinSivakumar 0:f430a728163a 57 }
AswinSivakumar 0:f430a728163a 58
AswinSivakumar 0:f430a728163a 59 void MAG3110::acquire_MAG3110_data_uT(float * m_data)
AswinSivakumar 0:f430a728163a 60 {
AswinSivakumar 0:f430a728163a 61
AswinSivakumar 0:f430a728163a 62 char data_bytes[7];
AswinSivakumar 0:f430a728163a 63 char d[1];
AswinSivakumar 0:f430a728163a 64 d[0]=MAG3110_DR_STATUS;
AswinSivakumar 0:f430a728163a 65 MAG3110_i2c.write(MAG3110_I2C_ADDRESS,d,1,true);
AswinSivakumar 0:f430a728163a 66 MAG3110_i2c.read(MAG3110_I2C_ADDRESS,data_bytes,7);// Read the 6 data bytes - LSB and MSB for X, Y and Z Axes.
AswinSivakumar 0:f430a728163a 67
AswinSivakumar 0:f430a728163a 68 m_data[0] = (float)((int16_t)((data_bytes[1]*256) + (data_bytes[2]))) * 0.1;
AswinSivakumar 0:f430a728163a 69 m_data[1] = (float)((int16_t)((data_bytes[3]*256) + (data_bytes[4]))) * 0.1;
AswinSivakumar 0:f430a728163a 70 m_data[2] = (float)((int16_t)((data_bytes[5]*256) + (data_bytes[6]))) * 0.1;
AswinSivakumar 0:f430a728163a 71
AswinSivakumar 0:f430a728163a 72 }
AswinSivakumar 0:f430a728163a 73
AswinSivakumar 0:f430a728163a 74