MAG3110 magnetometer library
Dependents: FRDM-FXS-MULTI2-B_SensorShield_HelloWorld xtrinsic_sensors
MAG3110.cpp@0:f430a728163a, 2016-01-23 (annotated)
- Committer:
- AswinSivakumar
- Date:
- Sat Jan 23 00:04:56 2016 +0000
- Revision:
- 0:f430a728163a
- Child:
- 1:4194a41cfd16
First commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AswinSivakumar | 0:f430a728163a | 1 | /* Copyright (c) 2015 NXP Semiconductors. MIT License |
AswinSivakumar | 0:f430a728163a | 2 | * |
AswinSivakumar | 0:f430a728163a | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
AswinSivakumar | 0:f430a728163a | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
AswinSivakumar | 0:f430a728163a | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
AswinSivakumar | 0:f430a728163a | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
AswinSivakumar | 0:f430a728163a | 7 | * Software is furnished to do so, subject to the following conditions: |
AswinSivakumar | 0:f430a728163a | 8 | * |
AswinSivakumar | 0:f430a728163a | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
AswinSivakumar | 0:f430a728163a | 10 | * substantial portions of the Software. |
AswinSivakumar | 0:f430a728163a | 11 | * |
AswinSivakumar | 0:f430a728163a | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
AswinSivakumar | 0:f430a728163a | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
AswinSivakumar | 0:f430a728163a | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
AswinSivakumar | 0:f430a728163a | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
AswinSivakumar | 0:f430a728163a | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
AswinSivakumar | 0:f430a728163a | 17 | */ |
AswinSivakumar | 0:f430a728163a | 18 | |
AswinSivakumar | 0:f430a728163a | 19 | #include "MAG3110.h" |
AswinSivakumar | 0:f430a728163a | 20 | #include "mbed.h" |
AswinSivakumar | 0:f430a728163a | 21 | |
AswinSivakumar | 0:f430a728163a | 22 | MAG3110::MAG3110(PinName sda, PinName scl) : MAG3110_i2c(sda,scl) |
AswinSivakumar | 0:f430a728163a | 23 | { |
AswinSivakumar | 0:f430a728163a | 24 | |
AswinSivakumar | 0:f430a728163a | 25 | } |
AswinSivakumar | 0:f430a728163a | 26 | |
AswinSivakumar | 0:f430a728163a | 27 | |
AswinSivakumar | 0:f430a728163a | 28 | void MAG3110::MAG3110_config(void) |
AswinSivakumar | 0:f430a728163a | 29 | { |
AswinSivakumar | 0:f430a728163a | 30 | char d[2]; |
AswinSivakumar | 0:f430a728163a | 31 | d[0] = MAG3110_CTRL_REG1; //Puts device in Standby mode |
AswinSivakumar | 0:f430a728163a | 32 | d[1] = 0x00; |
AswinSivakumar | 0:f430a728163a | 33 | MAG3110_i2c.write(MAG3110_I2C_ADDRESS, d,2); |
AswinSivakumar | 0:f430a728163a | 34 | |
AswinSivakumar | 0:f430a728163a | 35 | |
AswinSivakumar | 0:f430a728163a | 36 | d[0] = MAG3110_CTRL_REG2; //Set Automatic Magnetic Sensor Reset bit |
AswinSivakumar | 0:f430a728163a | 37 | d[1] = 0x80; |
AswinSivakumar | 0:f430a728163a | 38 | MAG3110_i2c.write(MAG3110_I2C_ADDRESS, d, 2); |
AswinSivakumar | 0:f430a728163a | 39 | |
AswinSivakumar | 0:f430a728163a | 40 | d[0] = MAG3110_CTRL_REG1; //Set ODR=40Hz and OSR=32 and set device to active mode. |
AswinSivakumar | 0:f430a728163a | 41 | d[1] = 0x09; |
AswinSivakumar | 0:f430a728163a | 42 | MAG3110_i2c.write(MAG3110_I2C_ADDRESS, d, 2); |
AswinSivakumar | 0:f430a728163a | 43 | |
AswinSivakumar | 0:f430a728163a | 44 | |
AswinSivakumar | 0:f430a728163a | 45 | } |
AswinSivakumar | 0:f430a728163a | 46 | |
AswinSivakumar | 0:f430a728163a | 47 | void MAG3110::acquire_MAG3110_data_uT(float * m_data) |
AswinSivakumar | 0:f430a728163a | 48 | { |
AswinSivakumar | 0:f430a728163a | 49 | |
AswinSivakumar | 0:f430a728163a | 50 | char data_bytes[7]; |
AswinSivakumar | 0:f430a728163a | 51 | char d[1]; |
AswinSivakumar | 0:f430a728163a | 52 | d[0]=MAG3110_DR_STATUS; |
AswinSivakumar | 0:f430a728163a | 53 | MAG3110_i2c.write(MAG3110_I2C_ADDRESS,d,1,true); |
AswinSivakumar | 0:f430a728163a | 54 | 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 | 55 | |
AswinSivakumar | 0:f430a728163a | 56 | m_data[0] = (float)((int16_t)((data_bytes[1]*256) + (data_bytes[2]))) * 0.1; |
AswinSivakumar | 0:f430a728163a | 57 | m_data[1] = (float)((int16_t)((data_bytes[3]*256) + (data_bytes[4]))) * 0.1; |
AswinSivakumar | 0:f430a728163a | 58 | m_data[2] = (float)((int16_t)((data_bytes[5]*256) + (data_bytes[6]))) * 0.1; |
AswinSivakumar | 0:f430a728163a | 59 | |
AswinSivakumar | 0:f430a728163a | 60 | } |
AswinSivakumar | 0:f430a728163a | 61 | |
AswinSivakumar | 0:f430a728163a | 62 |