Engduino  3.1.0
A fun device for learning coding
EngduinoMagnetometer.h
Go to the documentation of this file.
1 
14 #ifndef __ENGDUINOMAGNETOMETER_H__
15 #define __ENGDUINOMAGNETOMETER_H__
16 
17 #include <stdio.h>
18 #include <Arduino.h>
19 #include <Engduino.h>
20 
21 #if defined (__BOARD_ENGDUINOV2) || defined(__BOARD_ENGDUINOV3)
22 
23 /***********************************************************************************************\
24 * Public macros
25 \***********************************************************************************************/
26 
27 // 7 Bit I2C Device Address
28 #define FXMS3110_IIC_ADDRESS (0x0E)
29 
30 /***********************************************************************************************
31 **
32 ** FXMS3110Q Sensor Internal Registers
33 */
34 enum
35 {
36  FXMS3110_DR_STATUS = 0, // 0x00
37  FXMS3110_OUT_X_MSB, // 0x01
38  FXMS3110_OUT_X_LSB, // 0x02
39  FXMS3110_OUT_Y_MSB, // 0x03
40  FXMS3110_OUT_Y_lSB, // 0x04
41  FXMS3110_OUT_Z_MSB, // 0x05
42  FXMS3110_OUT_Z_LSB, // 0x06
43  FXMS3110_WHO_AM_I, // 0x07
44  FXMS3110_SYSMOD, // 0x08
45  FXMS3110_OFF_X_MSB, // 0x09
46  FXMS3110_OFF_X_LSB, // 0x0A
47  FXMS3110_OFF_Y_MSB, // 0x0B
48  FXMS3110_OFF_Y_LSB, // 0x0C
49  FXMS3110_OFF_Z_MSB, // 0x0D
50  FXMS3110_OFF_Z_LSB, // 0x0E
51  FXMS3110_DIE_TEMP, // 0x0F
52  FXMS3110_CTRL_REG1, // 0x10
53  FXMS3110_CTRL_REG2 // 0x11
54 };
55 
56 /*
57 ** DR_STATUS Register
58 */
59 #define DR_STATUS_REG 0x00
60 //
61 #define ZYXOW_BIT Bit._7
62 #define ZOW_BIT Bit._6
63 #define YOW_BIT Bit._5
64 #define XOW_BIT Bit._4
65 #define ZYXDR_BIT Bit._3
66 #define ZDR_BIT Bit._2
67 #define YDR_BIT Bit._1
68 #define XDR_BIT Bit._0
69 //
70 #define ZYXOW_MASK 0x80
71 #define ZOW_MASK 0x40
72 #define YOW_MASK 0x20
73 #define XOW_MASK 0x10
74 #define ZYXDR_MASK 0x08
75 #define ZDR_MASK 0x04
76 #define YDR_MASK 0x02
77 #define XDR_MASK 0x01
78 
79 
80 /*
81 ** XYZ Data Registers
82 */
83 #define OUT_X_MSB_REG 0x01
84 #define OUT_X_LSB_REG 0x02
85 #define OUT_Y_MSB_REG 0x03
86 #define OUT_Y_LSB_REG 0x04
87 #define OUT_Z_MSB_REG 0x05
88 #define OUT_Z_LSB_REG 0x06
89 
90 /*
91 ** WHO_AM_I Device ID Register
92 */
93 #define WHO_AM_I_REG 0x07
94 //
95 #define FXMS3110 0xC4
96 
97 
98 /*
99 ** SYSMOD System Mode Register
100 ** MMA8652 only
101 */
102 #define SYSMOD_REG 0x08
103 //
104 #define SYSMOD1_BIT Bit._1
105 #define SYSMOD0_BIT Bit._0
106 //
107 #define SYSMOD1_MASK 0x02
108 #define SYSMOD0_MASK 0x01
109 #define SYSMOD_MASK 0x03
110 //
111 #define SYSMOD_STANDBY 0x00
112 #define SYSMOD_ACTIVE_RAW (SYSMOD0_MASK)
113 #define SYSMOD_ACTIVE_USER (SYSMOD1_MASK)
114 
115 /*
116 ** XYZ Offset Correction Registers
117 */
118 #define OFF_X_MSB_REG 0x09
119 #define OFF_X_LSB_REG 0x0A
120 #define OFF_Y_MSB_REG 0x0B
121 #define OFF_Y_LSB_REG 0x0C
122 #define OFF_Z_MSB_REG 0x0D
123 #define OFF_Z_LSB_REG 0x0E
124 
125 /*
126 ** Die Temperature Register
127 */
128 #define OFF_X_MSB_REG 0x0F
129 
130 /*
131 ** CTRL_REG1 System Control 1 Register
132 */
133 #define CTRL_REG1 0x10
134 //
135 #define DR2_BIT Bit._7
136 #define DR1_BIT Bit._6
137 #define DR0_BIT Bit._5
138 #define OS1_BIT Bit._4
139 #define OS0_BIT Bit._3
140 #define FREAD_BIT Bit._2
141 #define TM_BIT Bit._1
142 #define ACTIVE_BIT Bit._0
143 //
144 #define DR2_MASK 0x80
145 #define DR1_MASK 0x40
146 #define DR0_MASK 0x20
147 #define OS1_MASK 0x10
148 #define OS0_MASK 0x08
149 #define FREAD_MASK 0x04
150 #define TM_MASK 0x02
151 #define ACTIVE_MASK 0x01
152 #define DR_MASK (DR2_MASK+DR1_MASK+DR0_MASK)
153 #define OS_MASK (OS1_MASK+OS0_MASK)
154 // The values are formed as ADCrate_Oversampling
155 #define DATA_RATE_1280_16 0x00
156 #define DATA_RATE_1280_32 (OS0_MASK)
157 #define DATA_RATE_1280_64 (OS1_MASK)
158 #define DATA_RATE_1280_128 (OS0_MASK+OS1_MASK)
159 #define DATA_RATE_640_16 (DR0)
160 #define DATA_RATE_640_32 (DR0+OS0_MASK)
161 #define DATA_RATE_640_64 (DR0+OS1_MASK)
162 #define DATA_RATE_640_128 (DR0+OS0_MASK+OS1_MASK)
163 #define DATA_RATE_320_16 (DR1)
164 #define DATA_RATE_320_32 (DR1+OS0_MASK)
165 #define DATA_RATE_320_64 (DR1+OS1_MASK)
166 #define DATA_RATE_320_128 (DR1+OS0_MASK+OS1_MASK)
167 #define DATA_RATE_160_16 (DR0+DR1)
168 #define DATA_RATE_160_32 (DR0+DR1+OS0_MASK)
169 #define DATA_RATE_160_64 (DR0+DR1+OS1_MASK)
170 #define DATA_RATE_160_128 (DR0+DR1+OS0_MASK+OS1_MASK)
171 #define DATA_RATE_80_16 (DR2)
172 #define DATA_RATE_80_32 (DR2+OS0_MASK)
173 #define DATA_RATE_80_64 (DR2+OS1_MASK)
174 #define DATA_RATE_80_128 (DR2+OS0_MASK+OS1_MASK)
175 // There are others, but no easy way of naming them directly because of overloading.
176 // See table 32 in the data sheet
177 //
178 #define TRIGGER_OFF 0x00
179 #define TRIGGER_ON (ACTIVE_MASK)
180 #define ACTIVE (ACTIVE_MASK)
181 #define STANDBY 0x00
182 
183 /*
184 ** CTRL_REG2 System Control 2 Register
185 */
186 #define CTRL_REG2 0x11
187 //
188 #define AUTO_MRST_EN_BIT Bit._7
189 #define RAW_BIT Bit._5
190 #define MAG_RST_BIT Bit._4
191 //
192 #define AUTO_MRST_EN_MASK 0x80
193 #define RAW_MASK 0x20
194 #define MAG_RST_MASK 0x10
195 //
196 
197 /***********************************************************************************************\
198 * Public type definitions
199 \***********************************************************************************************/
200 
201 /***********************************************************************************************\
202 * Public memory declarations
203 \***********************************************************************************************/
204 
205 /***********************************************************************************************\
206 * Public prototypes
207 \***********************************************************************************************/
208 
210 {
211  private:
212  void activate();
213  void standby();
214  void writeReg(int firstReg, const uint8_t *buf, uint8_t nRegs=1);
215  void readReg(int firstReg, uint8_t *buf, uint8_t nRegs=1);
216 
217  public:
219  void begin();
220  void end();
221  void xyz(float buf[3]);
222  int8_t temperature();
223 
224 };
225 
226 extern EngduinoMagnetometerClass EngduinoMagnetometer;
227 
228 #endif
229 #endif
230 
Definition: EngduinoMagnetometer.h:209
EngduinoMagnetometerClass()
Constructor.
Definition: EngduinoMagnetometer.cpp:28
void begin()
begin function - must be called before using other functions
Definition: EngduinoMagnetometer.cpp:43
void end()
end function - switch off the magnetometer
Definition: EngduinoMagnetometer.cpp:93
int8_t temperature()
Read the temperature value from the magnetometer.
Definition: EngduinoMagnetometer.cpp:151
void xyz(float buf[3])
Read the xyz values from the magnetometer.
Definition: EngduinoMagnetometer.cpp:108