-
Notifications
You must be signed in to change notification settings - Fork 1
/
Adafruit_HMC5883_U.h
120 lines (100 loc) · 4.43 KB
/
Adafruit_HMC5883_U.h
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
/***************************************************************************
This is a library for the HMC5883 magnentometer/compass
Designed specifically to work with the Adafruit HMC5883 Breakout
http://www.adafruit.com/products/1746
These displays use I2C to communicate, 2 pins are required to interface.
Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
from Adafruit!
Written by Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
***************************************************************************/
#ifndef __HMC5883_H__
#define __HMC5883_H__
#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Adafruit_Sensor.h"
#ifdef __AVR_ATtiny85__
#include "TinyWireM.h"
#define Wire TinyWireM
#else
#include <Wire.h>
#endif
/*=========================================================================
I2C ADDRESS/BITS
-----------------------------------------------------------------------*/
#define HMC5883_ADDRESS_MAG (0x3C >> 1) // 0011110x
/*=========================================================================*/
/*=========================================================================
REGISTERS
-----------------------------------------------------------------------*/
typedef enum
{
HMC5883_REGISTER_MAG_CRA_REG_M = 0x00,
HMC5883_REGISTER_MAG_CRB_REG_M = 0x01,
HMC5883_REGISTER_MAG_MR_REG_M = 0x02,
HMC5883_REGISTER_MAG_OUT_X_H_M = 0x03,
HMC5883_REGISTER_MAG_OUT_X_L_M = 0x04,
HMC5883_REGISTER_MAG_OUT_Z_H_M = 0x05,
HMC5883_REGISTER_MAG_OUT_Z_L_M = 0x06,
HMC5883_REGISTER_MAG_OUT_Y_H_M = 0x07,
HMC5883_REGISTER_MAG_OUT_Y_L_M = 0x08,
HMC5883_REGISTER_MAG_SR_REG_Mg = 0x09,
HMC5883_REGISTER_MAG_IRA_REG_M = 0x0A,
HMC5883_REGISTER_MAG_IRB_REG_M = 0x0B,
HMC5883_REGISTER_MAG_IRC_REG_M = 0x0C,
HMC5883_REGISTER_MAG_TEMP_OUT_H_M = 0x31,
HMC5883_REGISTER_MAG_TEMP_OUT_L_M = 0x32
} hmc5883MagRegisters_t;
/*=========================================================================*/
/*=========================================================================
MAGNETOMETER GAIN SETTINGS
-----------------------------------------------------------------------*/
typedef enum
{
HMC5883_MAGGAIN_1_3 = 0x20, // +/- 1.3
HMC5883_MAGGAIN_1_9 = 0x40, // +/- 1.9
HMC5883_MAGGAIN_2_5 = 0x60, // +/- 2.5
HMC5883_MAGGAIN_4_0 = 0x80, // +/- 4.0
HMC5883_MAGGAIN_4_7 = 0xA0, // +/- 4.7
HMC5883_MAGGAIN_5_6 = 0xC0, // +/- 5.6
HMC5883_MAGGAIN_8_1 = 0xE0 // +/- 8.1
} hmc5883MagGain;
/*=========================================================================*/
/*=========================================================================
INTERNAL MAGNETOMETER DATA TYPE
-----------------------------------------------------------------------*/
typedef struct hmc5883MagData_s
{
float x;
float y;
float z;
float orientation;
} hmc5883MagData;
/*=========================================================================*/
/*=========================================================================
CHIP ID
-----------------------------------------------------------------------*/
#define HMC5883_ID (0b11010100)
/*=========================================================================*/
/* Unified sensor driver for the magnetometer */
class Adafruit_HMC5883_Unified : public Adafruit_Sensor
{
public:
Adafruit_HMC5883_Unified(int32_t sensorID = -1);
bool begin(void);
void setMagGain(hmc5883MagGain gain);
bool getEvent(sensors_event_t*);
void getSensor(sensor_t*);
private:
hmc5883MagGain _magGain;
hmc5883MagData _magData; // Last read magnetometer data will be available here
int32_t _sensorID;
void write8(byte address, byte reg, byte value);
byte read8(byte address, byte reg);
void read(void);
};
#endif