NuMaker-PFM-NUC472: I2C0 read on-board MPU6500 and calculate tilt angle

Files at this revision

API Documentation at this revision

Comitter:
cyliang
Date:
Thu Feb 02 10:33:11 2023 +0000
Parent:
3:a5799a7d3bd1
Child:
5:ea1ed4e4c3ee
Commit message:
Fixed un-sync i2c object

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mpu6500.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Feb 02 09:59:52 2023 +0000
+++ b/main.cpp	Thu Feb 02 10:33:11 2023 +0000
@@ -6,10 +6,10 @@
 #define PI 3.14159265359
 #if TARGET_NUMAKER_PFM_NUC472
 I2C i2c0(PC_9, PA_15); // I2C0_SDA, I2C0_SCL
-#endif
-
-#if TARGET_NUMAKER_IOT_M467
+#elif TARGET_NUMAKER_IOT_M467
 I2C i2c0(PD_0, PD_1);
+#else
+I2C i2c0(I2C_SDA, I2C_SCL);
 #endif
 
 MPU6500 IMU; // IMU use on-board MPU6500
--- a/mpu6500.cpp	Thu Feb 02 09:59:52 2023 +0000
+++ b/mpu6500.cpp	Thu Feb 02 10:33:11 2023 +0000
@@ -1,27 +1,30 @@
 #include "mbed.h"
 #include "mpu6500.h"
 
-I2C  mpu6500_i2c(PC_9, PA_15); // I2C0_SDA, I2C0_SCL
+//I2C  mpu6500_i2c(PC_9, PA_15); // I2C0_SDA, I2C0_SCL
+extern I2C i2c0;
+I2C  *mpu6500_i2c;
 
 void MPU6500_WriteByte(uint8_t MPU6500_reg, uint8_t MPU6500_data)
 {
     char data_out[2];
     data_out[0]=MPU6500_reg;
     data_out[1]=MPU6500_data;
-    mpu6500_i2c.write(MPU6500_slave_addr, data_out, 2, 0);
+    mpu6500_i2c->write(MPU6500_slave_addr, data_out, 2, 0);
 }
 
 uint8_t MPU6500_ReadByte(uint8_t MPU6500_reg)
 {
     char data_out[1], data_in[1];
     data_out[0] = MPU6500_reg;
-    mpu6500_i2c.write(MPU6500_slave_addr, data_out, 1, 1);
-    mpu6500_i2c.read(MPU6500_slave_addr, data_in, 1, 0);
+    mpu6500_i2c->write(MPU6500_slave_addr, data_out, 1, 1);
+    mpu6500_i2c->read(MPU6500_slave_addr, data_in, 1, 0);
     return (data_in[0]);
 }
 
 void MPU6500::initialize()
 {
+    mpu6500_i2c = &i2c0;
     MPU6500_WriteByte(MPU6500_PWR_MGMT_1, 0x00);    // CLK_SEL=0: internal 8MHz, TEMP_DIS=0, SLEEP=0 
     MPU6500_WriteByte(MPU6500_SMPLRT_DIV, 0x07);  // Gyro output sample rate = Gyro Output Rate/(1+SMPLRT_DIV)
     MPU6500_WriteByte(MPU6500_CONFIG, 0x06);      // set TEMP_OUT_L, DLPF=2 (Fs=1KHz)