34
34
static uint8_t bus_addr = 0x68 ;
35
35
// Register addresses in the MPU6050 to read and write data to
36
36
const uint8_t REG_PWR_MGMT_1 = 0x6B , REG_ACCEL_OUT = 0x3B , REG_GYRO_OUT = 0x43 , REG_TEMP_OUT = 0x41 ,
37
- REG_SIGNAL_PATH_RESET = 0x68 , REG_GYRO_CONFIG = 0x1B , REG_ACCEL_CONFIG = 0x1C ;
37
+ REG_SIGNAL_PATH_RESET = 0x68 , REG_GYRO_CONFIG = 0x1B , REG_ACCEL_CONFIG = 0x1C , REG_WHO_AM_I = 0x75 ;
38
38
39
39
const float gyro_scale_deg [] = {250. / 0x7fff , 500. / 0x7fff , 1000. / 0x7fff , 2000. / 0x7fff };
40
40
const float gyro_scale_rad [] = {M_PI / 180. * 250. / 0x7fff , M_PI / 180. * 500. / 0x7fff ,
@@ -131,6 +131,7 @@ void mpu6050_scale_gyro_rad(float gyro[3], int16_t gyro_raw[3], MPU6050_Scale sc
131
131
float mpu6050_scale_temp (float temp_raw ) {
132
132
return (temp_raw / 340.0 ) + 36.53 ;
133
133
}
134
+
134
135
void mpu6050_gyro_selftest_on (void ) {
135
136
uint8_t regdata = 0b11100000 & ((uint8_t )MPU_FS_0 << 3 );
136
137
mpu6050_writereg (REG_GYRO_CONFIG , regdata );
@@ -140,9 +141,12 @@ void mpu6050_accel_selftest_on(void) {
140
141
mpu6050_writereg (REG_ACCEL_CONFIG , regdata );
141
142
}
142
143
143
- //TODO: set timing
144
- //TODO: set and read filter cutoff
145
- //TODO: interrupts
144
+ bool mpu6050_is_connected (void ) {
145
+ uint8_t who_are_you ;
146
+ mpu6050_readreg (REG_WHO_AM_I , & who_are_you , 1 );
147
+ return who_are_you == 0x68 ;
148
+ }
149
+
146
150
147
151
bool setup_MPU6050_i2c () {
148
152
#if !defined(i2c_default ) || !defined(PICO_DEFAULT_I2C_SDA_PIN ) || !defined(PICO_DEFAULT_I2C_SCL_PIN )
@@ -175,6 +179,10 @@ int run_MPU6050_demo() {
175
179
mpu6050_power (1 , false, false, false);
176
180
177
181
while (1 ) {
182
+ while (!mpu6050_is_connected ()) {
183
+ printf ("MPU6050 is not connected...\n" );
184
+ sleep_ms (1000 );
185
+ }
178
186
uint64_t time_us = to_us_since_boot (get_absolute_time ());
179
187
mpu6050_read_raw (accel_raw , gyro_raw , & temp_raw );
180
188
time_us = to_us_since_boot (get_absolute_time ()) - time_us ;
0 commit comments