Rev Author Line No. Line
2381 kaklik 1 /*
2 MAG3110 Breakout Example Code
3  
4 by: Aaron Weiss, aaron at sparkfun dot com
5 SparkFun Electronics 2011
6 date: 9/6/11
7 license: beerware, if you use this code and happen to meet me, you
8 can by me a beer
9  
10 The code reads the raw 16-bit x, y, and z values and prints them
11 out. This sketch does not use the INT1 pin, nor does it poll for
12 new data.
13  
14 */
15  
16 #include <Wire.h>
17  
18 #define MAG_ADDR 0x0E //7-bit address for the MAG3110, doesn't change
19  
20 void setup()
21 {
22 Wire.begin(); // join i2c bus (address optional for master)
23 Serial.begin(9600); // start serial for output
24 config(); // turn the MAG3110 on
25 }
26  
27 void loop()
28 {
29 print_values();
30 delay(5);
31 }
32  
33 void config(void)
34 {
35 Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
36 Wire.send(0x11); // cntrl register2
37 Wire.send(0x80); // send 0x80, enable auto resets
38 Wire.endTransmission(); // stop transmitting
39  
40 delay(15);
41  
42 Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
43 Wire.send(0x10); // cntrl register1
44 Wire.send(1); // send 0x01, active mode
45 Wire.endTransmission(); // stop transmitting
46 }
47  
48 void print_values(void)
49 {
50 Serial.print(readx());
51 Serial.print(" ");
52 Serial.print(ready());
53 Serial.print(" ");
54 Serial.println(readz());
55 }
56  
57 int readx(void)
58 {
59 int xl, xh; //define the MSB and LSB
60  
61 Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
62 Wire.send(0x01); // x MSB reg
63 Wire.endTransmission(); // stop transmitting
64  
65 delayMicroseconds(2); //needs at least 1.3us free time between start and stop
66  
67 Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
68 while(Wire.available()) // slave may send less than requested
69 {
70 xh = Wire.receive(); // receive the byte
71 }
72  
73 delayMicroseconds(2); //needs at least 1.3us free time between start and stop
74  
75 Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
76 Wire.send(0x02); // x LSB reg
77 Wire.endTransmission(); // stop transmitting
78  
79 delayMicroseconds(2); //needs at least 1.3us free time between start and stop
80  
81 Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
82 while(Wire.available()) // slave may send less than requested
83 {
84 xl = Wire.receive(); // receive the byte
85 }
86  
87 int xout = (xl|(xh << 8)); //concatenate the MSB and LSB
88 return xout;
89 }
90  
91 int ready(void)
92 {
93 int yl, yh; //define the MSB and LSB
94  
95 Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
96 Wire.send(0x03); // y MSB reg
97 Wire.endTransmission(); // stop transmitting
98  
99 delayMicroseconds(2); //needs at least 1.3us free time between start and stop
100  
101 Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
102 while(Wire.available()) // slave may send less than requested
103 {
104 yh = Wire.receive(); // receive the byte
105 }
106  
107 delayMicroseconds(2); //needs at least 1.3us free time between start and stop
108  
109 Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
110 Wire.send(0x04); // y LSB reg
111 Wire.endTransmission(); // stop transmitting
112  
113 delayMicroseconds(2); //needs at least 1.3us free time between start and stop
114  
115 Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
116 while(Wire.available()) // slave may send less than requested
117 {
118 yl = Wire.receive(); // receive the byte
119 }
120  
121 int yout = (yl|(yh << 8)); //concatenate the MSB and LSB
122 return yout;
123 }
124  
125 int readz(void)
126 {
127 int zl, zh; //define the MSB and LSB
128  
129 Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
130 Wire.send(0x05); // z MSB reg
131 Wire.endTransmission(); // stop transmitting
132  
133 delayMicroseconds(2); //needs at least 1.3us free time between start and stop
134  
135 Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
136 while(Wire.available()) // slave may send less than requested
137 {
138 zh = Wire.receive(); // receive the byte
139 }
140  
141 delayMicroseconds(2); //needs at least 1.3us free time between start and stop
142  
143 Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
144 Wire.send(0x06); // z LSB reg
145 Wire.endTransmission(); // stop transmitting
146  
147 delayMicroseconds(2); //needs at least 1.3us free time between start and stop
148  
149 Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
150 while(Wire.available()) // slave may send less than requested
151 {
152 zl = Wire.receive(); // receive the byte
153 }
154  
155 int zout = (zl|(zh << 8)); //concatenate the MSB and LSB
156 return zout;
157 }