-
Notifications
You must be signed in to change notification settings - Fork 115
/
HMC5883L.cpp
292 lines (232 loc) · 6.08 KB
/
HMC5883L.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
HMC5883L.cpp - Class file for the HMC5883L Triple Axis Digital Compass Arduino Library.
Version: 1.1.0
(c) 2014 Korneliusz Jarzebski
www.jarzebski.pl
This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include "HMC5883L.h"
bool HMC5883L::begin()
{
Wire.begin();
if ((fastRegister8(HMC5883L_REG_IDENT_A) != 0x48)
|| (fastRegister8(HMC5883L_REG_IDENT_B) != 0x34)
|| (fastRegister8(HMC5883L_REG_IDENT_C) != 0x33))
{
return false;
}
setRange(HMC5883L_RANGE_1_3GA);
setMeasurementMode(HMC5883L_CONTINOUS);
setDataRate(HMC5883L_DATARATE_15HZ);
setSamples(HMC5883L_SAMPLES_1);
mgPerDigit = 0.92f;
return true;
}
Vector HMC5883L::readRaw(void)
{
v.XAxis = readRegister16(HMC5883L_REG_OUT_X_M) - xOffset;
v.YAxis = readRegister16(HMC5883L_REG_OUT_Y_M) - yOffset;
v.ZAxis = readRegister16(HMC5883L_REG_OUT_Z_M) - zOffset;
return v;
}
Vector HMC5883L::readNormalize(void)
{
v.XAxis = ((float)readRegister16(HMC5883L_REG_OUT_X_M) - xOffset) * mgPerDigit;
v.YAxis = ((float)readRegister16(HMC5883L_REG_OUT_Y_M) - yOffset) * mgPerDigit;
v.ZAxis = ((float)readRegister16(HMC5883L_REG_OUT_Z_M) - zOffset) * mgPerDigit;
return v;
}
void HMC5883L::setOffset(int xo, int yo, int zo)
{
xOffset = xo;
yOffset = yo;
zOffset = zo;
}
void HMC5883L::setRange(hmc5883l_range_t range)
{
switch(range)
{
case HMC5883L_RANGE_0_88GA:
mgPerDigit = 0.073f;
break;
case HMC5883L_RANGE_1_3GA:
mgPerDigit = 0.92f;
break;
case HMC5883L_RANGE_1_9GA:
mgPerDigit = 1.22f;
break;
case HMC5883L_RANGE_2_5GA:
mgPerDigit = 1.52f;
break;
case HMC5883L_RANGE_4GA:
mgPerDigit = 2.27f;
break;
case HMC5883L_RANGE_4_7GA:
mgPerDigit = 2.56f;
break;
case HMC5883L_RANGE_5_6GA:
mgPerDigit = 3.03f;
break;
case HMC5883L_RANGE_8_1GA:
mgPerDigit = 4.35f;
break;
default:
break;
}
writeRegister8(HMC5883L_REG_CONFIG_B, range << 5);
}
hmc5883l_range_t HMC5883L::getRange(void)
{
return (hmc5883l_range_t)((readRegister8(HMC5883L_REG_CONFIG_B) >> 5));
}
void HMC5883L::setMeasurementMode(hmc5883l_mode_t mode)
{
uint8_t value;
value = readRegister8(HMC5883L_REG_MODE);
value &= 0b11111100;
value |= mode;
writeRegister8(HMC5883L_REG_MODE, value);
}
hmc5883l_mode_t HMC5883L::getMeasurementMode(void)
{
uint8_t value;
value = readRegister8(HMC5883L_REG_MODE);
value &= 0b00000011;
return (hmc5883l_mode_t)value;
}
void HMC5883L::setDataRate(hmc5883l_dataRate_t dataRate)
{
uint8_t value;
value = readRegister8(HMC5883L_REG_CONFIG_A);
value &= 0b11100011;
value |= (dataRate << 2);
writeRegister8(HMC5883L_REG_CONFIG_A, value);
}
hmc5883l_dataRate_t HMC5883L::getDataRate(void)
{
uint8_t value;
value = readRegister8(HMC5883L_REG_CONFIG_A);
value &= 0b00011100;
value >>= 2;
return (hmc5883l_dataRate_t)value;
}
void HMC5883L::setSamples(hmc5883l_samples_t samples)
{
uint8_t value;
value = readRegister8(HMC5883L_REG_CONFIG_A);
value &= 0b10011111;
value |= (samples << 5);
writeRegister8(HMC5883L_REG_CONFIG_A, value);
}
hmc5883l_samples_t HMC5883L::getSamples(void)
{
uint8_t value;
value = readRegister8(HMC5883L_REG_CONFIG_A);
value &= 0b01100000;
value >>= 5;
return (hmc5883l_samples_t)value;
}
Vector HMC5883L::selfTest()
{
Vector value;
setMeasurementMode(HMC5883L_SINGLE);
writeRegister8(HMC5883L_REG_CONFIG_A, 0x00);
writeRegister8(HMC5883L_REG_CONFIG_A, 0x01);
value = readRaw();
writeRegister8(HMC5883L_REG_CONFIG_A, 0x00);
return value;
}
// Write byte to register
void HMC5883L::writeRegister8(uint8_t reg, uint8_t value)
{
Wire.beginTransmission(HMC5883L_ADDRESS);
#if ARDUINO >= 100
Wire.write(reg);
Wire.write(value);
#else
Wire.send(reg);
Wire.send(value);
#endif
Wire.endTransmission();
}
// Read byte to register
uint8_t HMC5883L::fastRegister8(uint8_t reg)
{
uint8_t value;
Wire.beginTransmission(HMC5883L_ADDRESS);
#if ARDUINO >= 100
Wire.write(reg);
#else
Wire.send(reg);
#endif
Wire.endTransmission();
Wire.requestFrom(HMC5883L_ADDRESS, 1);
#if ARDUINO >= 100
value = Wire.read();
#else
value = Wire.receive();
#endif;
Wire.endTransmission();
return value;
}
// Read byte from register
uint8_t HMC5883L::readRegister8(uint8_t reg)
{
uint8_t value;
Wire.beginTransmission(HMC5883L_ADDRESS);
#if ARDUINO >= 100
Wire.write(reg);
#else
Wire.send(reg);
#endif
Wire.endTransmission();
Wire.beginTransmission(HMC5883L_ADDRESS);
Wire.requestFrom(HMC5883L_ADDRESS, 1);
while(!Wire.available()) {};
#if ARDUINO >= 100
value = Wire.read();
#else
value = Wire.receive();
#endif;
Wire.endTransmission();
return value;
}
// Read word from register
int16_t HMC5883L::readRegister16(uint8_t reg)
{
int16_t value;
Wire.beginTransmission(HMC5883L_ADDRESS);
#if ARDUINO >= 100
Wire.write(reg);
#else
Wire.send(reg);
#endif
Wire.endTransmission();
Wire.beginTransmission(HMC5883L_ADDRESS);
Wire.requestFrom(HMC5883L_ADDRESS, 2);
while(!Wire.available()) {};
#if ARDUINO >= 100
uint8_t vha = Wire.read();
uint8_t vla = Wire.read();
#else
uint8_t vha = Wire.receive();
uint8_t vla = Wire.receive();
#endif;
Wire.endTransmission();
value = vha << 8 | vla;
return value;
}