1 | /*
|
---|
2 | mag3110.cpp
|
---|
3 | libary for using the I2C mag3110 magnetometer
|
---|
4 |
|
---|
5 | (c) Written by Jeroen Cappaert for NanoSatisfi, August 2012
|
---|
6 |
|
---|
7 | - 21 Mar. 2016
|
---|
8 | Library modified to work with Arduino Lucky Shield
|
---|
9 | by andrea[at]arduino[dot]org
|
---|
10 |
|
---|
11 | */
|
---|
12 |
|
---|
13 | #include <Arduino.h>
|
---|
14 | #include "MAG3110.h"
|
---|
15 | #include <Wire.h>
|
---|
16 |
|
---|
17 | //Constructor
|
---|
18 | // MAG3110::MAG3110()
|
---|
19 | // {
|
---|
20 | // }
|
---|
21 |
|
---|
22 | // Configure magnetometer
|
---|
23 | void MAG3110::begin(void) {
|
---|
24 |
|
---|
25 | Wire.beginTransmission(MAG_ADDR);// transmit to device 0x0E
|
---|
26 | Wire.write(0x11); // cntrl register2
|
---|
27 | Wire.write(0x80); // send 0x80, enable auto resets
|
---|
28 | Wire.endTransmission(); // stop transmitting
|
---|
29 |
|
---|
30 | delay(15);
|
---|
31 |
|
---|
32 | Wire.beginTransmission(MAG_ADDR);// transmit to device 0x0E
|
---|
33 | Wire.write(0x10); // cntrl register1
|
---|
34 | Wire.write(1); // send 0x01, active mode
|
---|
35 | Wire.endTransmission(); // stop transmitting
|
---|
36 | }
|
---|
37 |
|
---|
38 | void MAG3110::read()
|
---|
39 | {
|
---|
40 | mag_x =read16Data(0x01,0x02);
|
---|
41 | mag_y =read16Data(0x03,0x04);
|
---|
42 | mag_z =read16Data(0x05,0x06);
|
---|
43 | }
|
---|
44 | // read X value
|
---|
45 |
|
---|
46 | int MAG3110::read16Data(byte MSB, byte LSB)
|
---|
47 | {
|
---|
48 | int xl, xh; //define the MSB and LSB
|
---|
49 |
|
---|
50 | Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
|
---|
51 | Wire.write(MSB); // x MSB reg
|
---|
52 | Wire.endTransmission(); // stop transmitting
|
---|
53 |
|
---|
54 | delayMicroseconds(2); //needs at least 1.3us free time between start and stop
|
---|
55 |
|
---|
56 | Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
|
---|
57 | while(Wire.available()) // slave may send less than requested
|
---|
58 | {
|
---|
59 | xh = Wire.read(); // receive the byte
|
---|
60 | }
|
---|
61 |
|
---|
62 | delayMicroseconds(2); //needs at least 1.3us free time between start and stop
|
---|
63 |
|
---|
64 | Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
|
---|
65 | Wire.write(LSB); // x LSB reg
|
---|
66 | Wire.endTransmission(); // stop transmitting
|
---|
67 |
|
---|
68 | delayMicroseconds(2); //needs at least 1.3us free time between start and stop
|
---|
69 |
|
---|
70 | Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
|
---|
71 | while(Wire.available()) // slave may send less than requested
|
---|
72 | {
|
---|
73 | xl = Wire.read(); // receive the byte
|
---|
74 | }
|
---|
75 |
|
---|
76 | int out = (xl|(xh << 8)); //concatenate the MSB and LSB
|
---|
77 | if (out & 0b1000000000000000){
|
---|
78 | //float yout1 = ((~yout & 0b0111111111111111)+ 1)*(-1) ;
|
---|
79 | return float ((~out & 0b0111111111111111)+ 1)*(-1) ;
|
---|
80 | }
|
---|
81 | return float (out);
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | MAG3110 mag3110;
|
---|
86 |
|
---|