-
Notifications
You must be signed in to change notification settings - Fork 1
/
microbit and mag3110.txt
130 lines (111 loc) · 3.98 KB
/
microbit and mag3110.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <Wire.h>
#define MAG_ADDR 0x0E //7-bit address for the MAG3110, doesn't change
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
config(); // turn the MAG3110 on
}
void loop()
{
print_values();
delay(500);
}
void config(void)
{
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x11); // cntrl register2
Wire.write(0x80); // send 0x80, enable auto resets
Wire.endTransmission(); // stop transmitting
delay(15);
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x10); // cntrl register1
Wire.write(1); // send 0x01, active mode
Wire.endTransmission(); // stop transmitting
}
void print_values(void)
{
Serial.print("x=");
Serial.print(readx());
Serial.print(",");
Serial.print("y=");
Serial.print(ready());
Serial.print(",");
Serial.print("z=");
Serial.println(readz());
}
int readx(void)
{
int xl, xh; //define the MSB and LSB
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x01); // x MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
xh = Wire.read(); // receive the byte
}
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x02); // x LSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
xl = Wire.read(); // receive the byte
}
int xout = (xl|(xh << 8)); //concatenate the MSB and LSB
return xout;
}
int ready(void)
{
int yl, yh; //define the MSB and LSB
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x03); // y MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
yh = Wire.read(); // receive the byte
}
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x04); // y LSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
yl = Wire.read(); // receive the byte
}
int yout = (yl|(yh << 8)); //concatenate the MSB and LSB
return yout;
}
int readz(void)
{
int zl, zh; //define the MSB and LSB
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x05); // z MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
zh = Wire.read(); // receive the byte
}
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x06); // z LSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
zl = Wire.read(); // receive the byte
}
int zout = (zl|(zh << 8)); //concatenate the MSB and LSB
return zout;
}