-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compass_HMC5883L.cpp
82 lines (71 loc) · 2.11 KB
/
Compass_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
#include "Arduino.h"
#include <Wire.h>
#include "Compass_HMC5883L.h"
/*
By: Riza Çelik
Created on: 12/05/2021
License: OSHW 1.0, http://freedomdefined.org/OSHW
Uno A4 (SDA), A5 (SCL)
Mega2560 20 (SDA), 21 (SCL)
Leonardo 2 (SDA), 3 (SCL)
Due 20 (SDA), 21 (SCL), SDA1, SCL1
STM32 PB7 (SDA), PB6 (SCL)
https://jensd.dk/cansat/gy80gy87gy88gy91/datasheet/HMC5883L.pdf
https://jensd.dk/cansat/gy80gy87gy88gy91/datasheet/MPU6050.pdf
https://jensd.dk/cansat/gy80gy87gy88gy91/datasheet/MPU9250.pdf
https://jensd.dk/cansat/gy80gy87gy88gy91/datasheet/BMP180.pdf
https://jensd.dk/cansat/gy80gy87gy88gy91/datasheet/BMP085.pdf
https://jensd.dk/cansat/gy80gy87gy88gy91/datasheet/ADXL345.pdf
https://jensd.dk/cansat/gy80gy87gy88gy91/datasheet/L3G4200D.pdf
*/
bool Compass_HMC5883L::begin()
{
Wire.begin();
// HMC5883L Setup
Wire.beginTransmission(HMC5883L);
// set scale to +/- 1.3 gauss
// scale = 0.92; // for 1.3 gauss
Wire.write(0x01); // select register address
Wire.write(0x20); // write data
Wire.endTransmission();
Wire.beginTransmission(HMC5883L);
// set continuous measurement mode
Wire.write(0x02); // select register address
Wire.write(0x00); // write data
Wire.endTransmission();
}
void Compass_HMC5883L::setOffset(int xo, int yo)
{
xOffset = xo;
yOffset = yo;
}
float Compass_HMC5883L::getX(void)
{
float XAxis = ((float)readRegister16(0x03) - xOffset) * scale;
return XAxis;
}
float Compass_HMC5883L::getY(void)
{
float YAxis = ((float)readRegister16(0x07) - yOffset) * scale;
return YAxis;
}
float Compass_HMC5883L::getZ(void)
{
float ZAxis = (float)readRegister16(0x05) * scale;
return ZAxis;
}
int16_t Compass_HMC5883L::readRegister16(uint8_t reg)
{
int16_t value;
Wire.beginTransmission(HMC5883L);
Wire.write(reg);
Wire.endTransmission();
Wire.beginTransmission(HMC5883L);
Wire.requestFrom(HMC5883L, 2);
while (!Wire.available()) {};
uint8_t vha = Wire.read();
uint8_t vla = Wire.read();
Wire.endTransmission();
value = vha << 8 | vla;
return value;
}