Skip to content

Commit c974dc8

Browse files
add mpu6050_is_connected()
1 parent 8cce5bd commit c974dc8

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

i2c/mpu6050_i2c/mpu6050_i2c.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
static uint8_t bus_addr = 0x68;
3535
// Register addresses in the MPU6050 to read and write data to
3636
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;
3838

3939
const float gyro_scale_deg[] = {250. / 0x7fff, 500. / 0x7fff, 1000. / 0x7fff, 2000. / 0x7fff};
4040
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
131131
float mpu6050_scale_temp(float temp_raw) {
132132
return (temp_raw / 340.0) + 36.53;
133133
}
134+
134135
void mpu6050_gyro_selftest_on(void) {
135136
uint8_t regdata = 0b11100000 & ((uint8_t)MPU_FS_0 << 3);
136137
mpu6050_writereg(REG_GYRO_CONFIG, regdata);
@@ -140,9 +141,12 @@ void mpu6050_accel_selftest_on(void) {
140141
mpu6050_writereg(REG_ACCEL_CONFIG, regdata);
141142
}
142143

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+
146150

147151
bool setup_MPU6050_i2c() {
148152
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
@@ -175,6 +179,10 @@ int run_MPU6050_demo() {
175179
mpu6050_power(1, false, false, false);
176180

177181
while (1) {
182+
while (!mpu6050_is_connected()) {
183+
printf("MPU6050 is not connected...\n");
184+
sleep_ms(1000);
185+
}
178186
uint64_t time_us = to_us_since_boot(get_absolute_time());
179187
mpu6050_read_raw(accel_raw, gyro_raw, &temp_raw);
180188
time_us = to_us_since_boot(get_absolute_time()) - time_us;

i2c/mpu6050_i2c/mpu6050_i2c.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ void mpu6050_setbusaddr(uint8_t addr); //set the i2c bus address for communicati
2525
void mpu6050_power(uint8_t CLKSEL, bool temp_disable, bool sleep, bool cycle);
2626
// Reset power management and signal path registers. MPU6050 returns to default settings. Includes 200ms of wait.
2727
void mpu6050_reset();
28+
// Check whether MPU6050 is connected using the read-only WHO_AM_I register, which is always 0x68
29+
bool mpu6050_is_connected();
2830
// turn on the mpu6050's accelerometer self test. Turn it off after a few 100ms with mpu6050_setscale_accel
2931
void mpu6050_accel_selftest_on(); // TODO: find out what "self test" does
3032
// turn on the mpu6050's gyroscope self test. Turn it off after a few 100ms with mpu6050_setscale_gyro

0 commit comments

Comments
 (0)