-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpu6050.h
372 lines (306 loc) · 11.9 KB
/
mpu6050.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*!
* \file mpu6050.h
* \author Tycho Jobsis
* \date 04-04-2021
* \version 0.1.0
*
* \brief MPU6050 library for the Xmega with the <a href="https://github.com/TychoJ/Xmega-TWI">Xmega-TWI library</a>
*
* \details This library is used to communicate with an MPU6050 sensor over I2C.
*
* \note For the HvA-Xmegaboard you need to use TWIE!
*
* \note This library <b>does not</b> support slave devices for the mpu6050!!!
*/
/*! \copyright
*
* MIT License
*
* Copyright (c) 2021 TychoJ <br>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy <br>
* of this software and associated documentation files (the "Software"), to deal <br>
* in the Software without restriction, including without limitation the rights <br>
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell <br>
* copies of the Software, and to permit persons to whom the Software is <br>
* furnished to do so, subject to the following conditions: <br>
*
* The above copyright notice and this permission notice shall be included in all <br>
* copies or substantial portions of the Software. <br>
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR <br>
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, <br>
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE <br>
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER <br>
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, <br>
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE <br>
* SOFTWARE.<br>
*/
/*!
* \mainpage MPU library for Xmega devices
*
* \section dependencies
*
* <a href="https://github.com/TychoJ/Xmega-TWI">Xmega-TWI library</a><br>
*
* \section section_2 how to use the MPU6050 library
* place the c and h files of the dependencies in the same folder as the c and h files of this library.<br>
* To start using this library you need to init a TWI module of the AtXmega device, this can be done with the TWI library.<br>
* After initializing the TWI module you need to enable the MPU6050.
* \subsection code_example example
* \code{.c}
enable_mpu6050(&TWIx, addr);
\endcode
* &TWIx = The TWI module address of the TWI module that needs to be used for the communication with the MPU6050.<br>
* addr = The address of the MPU6050. If AD0 is connected to ground the address is: 0x68 (hexadecimal).
* If AD0 is connected to VCC then the address of the MPU6050 will be: 0x69 (hexadecimal).
*
* after enabling the MPU6050 you can use the other functions to get acceleration, rotational velocity and temperature data.<br>
* With the MPU6050_ACCEL_SCL_x and the MPU6050_GYRO_SCL_x to select the precision of the measurements. The lower the precision
* the higher the values you can measure.
*
* This library has TWI_MODULE standard defined as &TWIE. you can change it if needed.
* For the HvA-Xmega boards this should not be changed.
*
*/
#include <avr/io.h>
#include <float.h>
#include "TWI.h"
#ifndef MPU6050_H_
#define MPU6050_H_
/*
* warning: Do not change unless you do not use the HvA-Xmegaboard!!
*/
#define TWI_MODULE &TWIE
//0x68 (GND on AD0)
//0x69 (VCC on AD0)
#define MPU6050_ADDRESS 0x68
/*
* \brief Register definitions
*/
/*
* Self test registers
*/
#define MPU_6050_SELF_TEST_X 0x0D
#define MPU_6050_SELF_TEST_Y 0x0E
#define MPU_6050_SELF_TEST_Z 0x0F
#define MPU_6050_SELF_TEST_A 0x10
#define MPU_6050_SMPLRT_DIV 0x19
/*
* Sensor configuration registers of the MPU6050
*/
#define MPU_6050_CONFIG 0x1A
#define MPU_6050_GYRO_CONFIG 0x1B
#define MPU_6050_ACCEL_CONFIG 0x1C
/*
* Slave settings for slave devices for the MPU6050
*/
#define MPU_6050_I2C_SLV0_ADDR 0x25
#define MPU_6050_I2C_SLV0_REG 0x26
#define MPU_6050_I2C_SLV0_DO 0x63
#define MPU_6050_I2C_SLV0_CTRL 0x27
#define MPU_6050_I2C_SLV1_ADDR 0x28
#define MPU_6050_I2C_SLV1_REG 0x29
#define MPU_6050_I2C_SLV1_DO 0x64
#define MPU_6050_I2C_SLV1_CTRL 0x2A
#define MPU_6050_I2C_SLV2_ADDR 0x2B
#define MPU_6050_I2C_SLV2_REG 0x2C
#define MPU_6050_I2C_SLV2_DO 0x65
#define MPU_6050_I2C_SLV2_CTRL 0x2D
#define MPU_6050_I2C_SLV3_ADDR 0x2E
#define MPU_6050_I2C_SLV3_REG 0x2F
#define MPU_6050_I2C_SLV3_DO 0x66
#define MPU_6050_I2C_SLV3_CTRL 0x302
#define MPU_6050_I2C_SLV4_ADDR 0x31
#define MPU_6050_I2C_SLV4_REG 0x32
#define MPU_6050_I2C_SLV4_DO 0x33
#define MPU_6050_I2C_SLV4_CTRL 0x34
#define MPU_6050_I2C_SLV4_DI 0x53
/*
* Master I2C registers for the MPU6050
*/
#define MPU_6050_I2C_MST_CTRL 0x24
#define MPU_6050_I2C_MST_STATUS 0x36
#define MPU_6050_I2C_MST_DELAY_CTRL 0x67
/*
* Interrupt registers
*/
#define MPU_6050_INT_PIN_CFG 0x37
#define MPU_6050_INT_ENABLE 0x38
#define MPU_6050_INT_STATUS 0x3A
/*
* Accelerometer output registers
*/
#define MPU_6050_ACCEL_XOUT_H 0x3B
#define MPU_6050_ACCEL_XOUT_L 0x3C
#define MPU_6050_ACCEL_YOUT_H 0x3D
#define MPU_6050_ACCEL_YOUT_L 0x3E
#define MPU_6050_ACCEL_ZOUT_H 0x3F
#define MPU_6050_ACCEL_ZOUT_L 0x40
/*
* Temperatures output registers
*/
#define MPU_6050_TEMP_OUT_H 0x41
#define MPU_6050_TEMP_OUT_L 0x42
/*
* Gyroscope output registers
*/
#define MPU_6050_GYRO_XOUT_H 0x43
#define MPU_6050_GYRO_XOUT_L 0x44
#define MPU_6050_GYRO_YOUT_H 0x45
#define MPU_6050_GYRO_YOUT_L 0x46
#define MPU_6050_GYRO_ZOUT_H 0x47
#define MPU_6050_GYRO_ZOUT_L 0x48
/*
* External sensor data registers
*/
#define MPU_6050_EXT_SENS_DATA_00 0x49
#define MPU_6050_EXT_SENS_DATA_01 0x4A
#define MPU_6050_EXT_SENS_DATA_02 0x4B
#define MPU_6050_EXT_SENS_DATA_03 0x4C
#define MPU_6050_EXT_SENS_DATA_04 0x4D
#define MPU_6050_EXT_SENS_DATA_05 0x4E
#define MPU_6050_EXT_SENS_DATA_06 0x4F
#define MPU_6050_EXT_SENS_DATA_07 0x50
#define MPU_6050_EXT_SENS_DATA_08 0x51
#define MPU_6050_EXT_SENS_DATA_09 0x52
#define MPU_6050_EXT_SENS_DATA_10 0x53
#define MPU_6050_EXT_SENS_DATA_11 0x54
#define MPU_6050_EXT_SENS_DATA_12 0x55
#define MPU_6050_EXT_SENS_DATA_13 0x56
#define MPU_6050_EXT_SENS_DATA_14 0x57
#define MPU_6050_EXT_SENS_DATA_15 0x58
#define MPU_6050_EXT_SENS_DATA_16 0x59
#define MPU_6050_EXT_SENS_DATA_17 0x5A
#define MPU_6050_EXT_SENS_DATA_18 0x5B
#define MPU_6050_EXT_SENS_DATA_19 0x5C
#define MPU_6050_EXT_SENS_DATA_20 0x5D
#define MPU_6050_EXT_SENS_DATA_21 0x5E
#define MPU_6050_EXT_SENS_DATA_22 0x5F
#define MPU_6050_EXT_SENS_DATA_23 0x60
#define MPU_6050_SIGNAL_PATH_RESET 0x68
#define MPU_6050_USER_CTRL 0x6A
/*
* Power management registers
*/
#define MPU_6050_PWR_MGMT_1 0x6B
#define MPU_6050_PWR_MGMT_2 0x6C
/*
* FIFO registers of the MPU6050
*/
#define MPU_6050_FIFO_EN 0x23
#define MPU_6050_FIFO_COUNTH 0x72
#define MPU_6050_FIFO_COUNTL 0x73
#define MPU_6050_FIFO_R_W 0x74
#define MPU_6050_WHO_AM_I 0x752
/*
* Bit locations in register
*/
#define DATA_RDY_INT_EN 0
#define MPU_6050_I2C_MST_INT_EN 3
#define MPU_6050_FIFO_INT_EN 4
/*
* return values for what_happened_MPU6050
*/
#define DATA_RDY_INT 3
#define MPU_6050_I2C_MST_INT 4
#define MPU_6050_FIFO_INT 5
/*
* CLK selection value
*/
#define MPU6050_CLK_8MHZ 0
/*
* Status code for TWI
*/
#define MPU6050_TWI_ERROR 1
#define MPU6050_TWI_OK 0
#define MPU6050_ACCEL_SCL_2G 0 //!< +-2G Max measurement
#define MPU6050_ACCEL_SCL_4G 1 //!< +-4G Max measurement
#define MPU6050_ACCEL_SCL_8G 2 //!< +-8G Max measurement
#define MPU6050_ACCEL_SCL_16G 3 //!< +-16G Max measurement
#define MPU6050_GYRO_SCL_250 0 //!< +-250 degrees per second Max measurement
#define MPU6050_GYRO_SCL_500 1 //!< +-500 degrees per second Max measurement
#define MPU6050_GYRO_SCL_1000 2 //!< +-1000 degrees per second Max measurement
#define MPU6050_GYRO_SCL_2000 3 //!< +-2000 degrees per second Max measurement
#define ON 1
#define OFF 0
float ACCELX_OFFSET_MPU6050[4]; //!< The sensor offset value for all sensitivities
float ACCELY_OFFSET_MPU6050[4]; //!< The sensor offset value for all sensitivities
float ACCELZ_OFFSET_MPU6050[4]; //!< The sensor offset value for all sensitivities
float GYROX_OFFSET_MPU6050[4]; //!< The sensor offset value for all sensitivities
float GYROY_OFFSET_MPU6050[4]; //!< The sensor offset value for all sensitivities
float GYROZ_OFFSET_MPU6050[4]; //!< The sensor offset value for all sensitivities
/*! \brief Union to store 16bits sensor values
*
* This union is used to store a 16 bit sensor value in two 8bit unsigned integers.
* You can use DATA to access the whole 16 bit value. You can also use DATAL and DATAH to either store the lower or the higher eight bits of the sixteen bit DATA value.
*
*
*/
typedef union {
struct {
uint8_t DATAL;
uint8_t DATAH;
};
int16_t DATA;
} data16_t;
typedef union {
struct {
uint8_t DATAL;
uint8_t DATAH;
};
int16_t TEMP;
} TEMP16_t;
uint8_t enable_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t disable_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t wake_up_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t sleep_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t get_accel_x_raw_mpu6050(TWI_t *twi, uint8_t addr, int16_t *data);
uint8_t get_accel_y_raw_mpu6050(TWI_t *twi, uint8_t addr, int16_t *data);
uint8_t get_accel_z_raw_mpu6050(TWI_t *twi, uint8_t addr, int16_t *data);
uint8_t get_gyro_x_raw_mpu6050(TWI_t *twi, uint8_t addr, int16_t *data);
uint8_t get_gyro_y_raw_mpu6050(TWI_t *twi, uint8_t addr, int16_t *data);
uint8_t get_gyro_z_raw_mpu6050(TWI_t *twi, uint8_t addr, int16_t *data);
uint8_t get_accel_x_mpu6050(TWI_t *twi, uint8_t addr, float *data);
uint8_t get_accel_y_mpu6050(TWI_t *twi, uint8_t addr, float *data);
uint8_t get_accel_z_mpu6050(TWI_t *twi, uint8_t addr, float *data);
uint8_t get_gyro_x_mpu6050(TWI_t *twi, uint8_t addr, float *data);
uint8_t get_gyro_y_mpu6050(TWI_t *twi, uint8_t addr, float *data);
uint8_t get_gyro_z_mpu6050(TWI_t *twi, uint8_t addr, float *data);
uint8_t get_temp_mpu6050(TWI_t *twi, uint8_t addr, float *data);
uint8_t int_enable_mpu6050(TWI_t *twi, uint8_t addr, uint8_t interupt);
uint8_t int_disable_mpu6050(TWI_t *twi, uint8_t addr, uint8_t interupt);
uint8_t what_happend_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t ext_sens_value_mpu6050(TWI_t *twi, uint8_t addr, uint8_t reg, uint8_t *data);
uint8_t disable_temp_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t enable_temp_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t reset_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t reset_accel_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t reset_gyro_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t reset_temp_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t clk_sel_mpu6050(TWI_t *twi, uint8_t addr, uint8_t clk_sel);
uint8_t self_test_x_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t self_test_y_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t self_test_z_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t self_test_a_mpu6050(TWI_t *twi, uint8_t addr, uint8_t xyz);
uint8_t check_err_mpu6050(uint8_t err);
uint8_t accel_set_scale_mpu6050(TWI_t *twi, uint8_t addr, uint8_t scale);
uint8_t accel_get_scale_mpu6050(TWI_t *twi, uint8_t addr, uint8_t *scale);
uint8_t gyro_set_scale_mpu6050(TWI_t *twi, uint8_t addr, uint8_t scale);
uint8_t gyro_get_scale_mpu6050(TWI_t *twi, uint8_t addr, uint8_t *scale);
uint8_t temp_set_scale_mpu6050(TWI_t *twi, uint8_t addr, uint8_t scale);
uint8_t temp_get_scale_mpu6050(TWI_t *twi, uint8_t addr, uint8_t *scale);
uint8_t calibrate_gyro_x_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t calibrate_gyro_y_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t calibrate_gyro_z_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t calibrate_accel_x_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t calibrate_accel_y_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t calibrate_accel_z_mpu6050(TWI_t *twi, uint8_t addr);
uint8_t stdby_all_mpu6050(TWI_t *twi, uint8_t addr, uint8_t on_off);
uint8_t stdby_accel_x_mpu6050(TWI_t *twi, uint8_t addr, uint8_t on_off);
uint8_t stdby_accel_y_mpu6050(TWI_t *twi, uint8_t addr, uint8_t on_off);
uint8_t stdby_accel_z_mpu6050(TWI_t *twi, uint8_t addr, uint8_t on_off);
uint8_t stdby_gyro_x_mpu6050(TWI_t *twi, uint8_t addr, uint8_t on_off);
uint8_t stdby_gyro_y_mpu6050(TWI_t *twi, uint8_t addr, uint8_t on_off);
uint8_t stdby_gyro_z_mpu6050(TWI_t *twi, uint8_t addr, uint8_t on_off);
#endif /* MPU6050_H_ */