-
Notifications
You must be signed in to change notification settings - Fork 6
/
mpu6050.c
145 lines (100 loc) · 3.8 KB
/
mpu6050.c
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
#include <inttypes.h>
#include <stdint.h>
#include "i2c.h"
#include "mpu6050_reg.h"
#include "mpu6050.h"
//start mpu6050 over I2C
//return 0x68(device address with AD0 low),
//return 0 if error
uint8_t mpu6050_start(void){
uint8_t res;
res = i2c_start(MPU6050_ADDRESS+I2C_WRITE);
if(~res){
i2c_read_byte(MPU6050_ADDRESS, MPU6050_RA_WHO_AM_I, &res);
if(res==0x68){
return res;
}else{
return 0;
}
}else{
return 0;
}
}
//configure important settings in mpu6050
//subject to change app(ilcation) by app
void mpu6050_init(void){
i2c_write_byte(MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_1, 0x00); //exit sleep mode
i2c_write_byte(MPU6050_ADDRESS, MPU6050_RA_CONFIG, 0x01); // LPF, bandwidth = 184(accel) and 188(gyro)
i2c_write_byte(MPU6050_ADDRESS, MPU6050_RA_GYRO_CONFIG, 1<<4); // gyro ADC scale: 1000 deg/s
i2c_write_byte(MPU6050_ADDRESS, MPU6050_RA_ACCEL_CONFIG, 0x00); //accel ADC scale: 2 g
i2c_write_byte(MPU6050_ADDRESS, MPU6050_RA_INT_ENABLE, 0x00); //enable data ready interrupt
i2c_write_byte(MPU6050_ADDRESS, MPU6050_RA_SIGNAL_PATH_RESET, 0x00); //don't reset signal path
}
// read gyro X, Y, Z all at once, high- & low-8-bits combined
// return int16_t (signed) in buff
//buff must have at least 3 available places
//no error handling for too small buff
void mpu6050_read_gyro_ALL(int16_t * buff){
uint8_t tmp[2];
mpu6050_read_gyro_X(tmp);
buff[0] = (tmp[0]<<8)|(tmp[1]);
mpu6050_read_gyro_Y(tmp);
buff[1] = (tmp[0]<<8)|(tmp[1]);
mpu6050_read_gyro_Z(tmp);
buff[2] = (tmp[0]<<8)|(tmp[1]);
}
// read accel X, Y, Z all at once, high- & low-8-bits combined
// return int16_t (signed) in buff
//buff must have at least 3 available places
//no error handling for too small buff
void mpu6050_read_accel_ALL(int16_t * buff){
uint8_t tmp[2];
mpu6050_read_accel_X(tmp);
buff[0] = (tmp[0]<<8)|(tmp[1]);
mpu6050_read_accel_Y(tmp);
buff[1] = (tmp[0]<<8)|(tmp[1]);
mpu6050_read_accel_Z(tmp);
buff[2] = (tmp[0]<<8)|(tmp[1]);
}
//read gyro X, high- & low-8-bits separated, high first
//buff must have at least 2 available places
//no error handling for too small buff
void mpu6050_read_gyro_X(uint8_t * buff){
i2c_read_byte(MPU6050_ADDRESS, MPU6050_RA_GYRO_XOUT_H, buff);
i2c_read_byte(MPU6050_ADDRESS, MPU6050_RA_GYRO_XOUT_L, buff+1);
}
//read gyro Y, high- & low-8-bits separated, high first
//buff must have at least 2 available places
//no error handling for too small buff
void mpu6050_read_gyro_Y(uint8_t * buff){
i2c_read_byte(MPU6050_ADDRESS, MPU6050_RA_GYRO_YOUT_H, buff);
i2c_read_byte(MPU6050_ADDRESS, MPU6050_RA_GYRO_YOUT_L, buff+1);
}
//read gyro Z, high- & low-8-bits separated, high first
//buff must have at least 2 available places
//no error handling for too small buff
void mpu6050_read_gyro_Z(uint8_t * buff){
i2c_read_byte(MPU6050_ADDRESS, MPU6050_RA_GYRO_ZOUT_H, buff);
i2c_read_byte(MPU6050_ADDRESS, MPU6050_RA_GYRO_ZOUT_L, buff+1);
}
//read accel X, high- & low-8-bits separated, high first
//buff must have at least 2 available places
//no error handling for too small buff
void mpu6050_read_accel_X(uint8_t * buff){
i2c_read_byte(MPU6050_ADDRESS, MPU6050_RA_ACCEL_XOUT_H, buff);
i2c_read_byte(MPU6050_ADDRESS, MPU6050_RA_ACCEL_XOUT_L, buff+1);
}
//read accel Y, high- & low-8-bits separated, high first
//buff must have at least 2 available places
//no error handling for too small buff
void mpu6050_read_accel_Y(uint8_t * buff){
i2c_read_byte(MPU6050_ADDRESS, MPU6050_RA_ACCEL_YOUT_H, buff);
i2c_read_byte(MPU6050_ADDRESS, MPU6050_RA_ACCEL_YOUT_L, buff+1);
}
//read accel Z, high- & low-8-bits separated, high first
//buff must have at least 2 available places
//no error handling for too small buff
void mpu6050_read_accel_Z(uint8_t * buff){
i2c_read_byte(MPU6050_ADDRESS, MPU6050_RA_ACCEL_ZOUT_H, buff);
i2c_read_byte(MPU6050_ADDRESS, MPU6050_RA_ACCEL_ZOUT_L, buff+1);
}