|
本帖最后由 john800422 于 2015-7-18 14:17 编辑
自己調通的代碼,透過 SPI 讀取 MPU9255 的加速度計、陀螺儀、磁力計資料,
使用 MPU6555 內建的自動讀取功能,設定好後就會連續自動讀取 AK8963 的資料,
只需要從 EXT_SENS_DATA_00 - EXT_SENS_DATA_07 讀取出來就可以了,
這個功能蠻方便的,下面放出幾個關鍵的部分,詳細的工程最下面可以下載,或是在 github 下載
https://github.com/KitSprout/Red ... _Module_MPU9255_SPI
使用 STM32F103C 實現,目前 400 Hz 還可以正常運作。
接收部分使用 MATLAB,詳細參考下面帖子
http://www.amobbs.com/forum.php? ... =5627278&extra=
- #define MPU6555_InitRegNum 11
- #define AK8963_InitRegNum 2
- uint8_t MPU9255_Init( MPU_InitTypeDef *MPUx )
- {
- uint8_t status = ERROR;
- uint8_t MPU6555_InitData[MPU6555_InitRegNum][2] = {
- {0x80, MPU6555_PWR_MGMT_1}, // Reset Device
- {0x01, MPU6555_PWR_MGMT_1}, // Clock Source
- {0x30, MPU6555_USER_CTRL}, // Set I2C_MST_EN, I2C_IF_DIS
- {0x00, MPU6555_PWR_MGMT_2}, // Enable Acc & Gyro
- {0x07, MPU6555_CONFIG}, // DLPF_CFG[2:0] = 111;
- {0x18, MPU6555_GYRO_CONFIG}, // +-2000dps
- {0x08, MPU6555_ACCEL_CONFIG}, // +-4G
- {0x48, MPU6555_ACCEL_CONFIG_2}, // Set Acc Data Rates
- {0x10, MPU6555_INT_PIN_CFG}, // Set INT_ANYRD_2CLEAR
- {0x10, MPU6555_INT_ENABLE}, //
- {0x4D, MPU6555_I2C_MST_CTRL}, // I2C Speed 400 kHz
- };
- uint8_t AK8963_InitData[AK8963_InitRegNum][2] = {
- {0x01, AK8963_CNTL2}, // Reset Device
- {0x06, AK8963_CNTL1}, // Continuous measurement mode 2
- };
- MPU6555_InitData[5][0] = MPUx->MPU_Gyr_FullScale; // MPU6555_GYRO_CONFIG
- MPU6555_InitData[4][0] = MPUx->MPU_Gyr_LowPassFilter; // MPU6555_CONFIG
- MPU6555_InitData[6][0] = MPUx->MPU_Acc_FullScale; // MPU6555_ACCEL_CONFIG
- MPU6555_InitData[7][0] = MPUx->MPU_Acc_LowPassFilter; // MPU6555_ACCEL_CONFIG_2
- AK8963_InitData[1][0] |= MPUx->MPU_Mag_FullScale;
- for(uint8_t i = 0; i < MPU6555_InitRegNum; i++) {
- MPU9255_WriteReg(MPU6555_InitData[i][1], MPU6555_InitData[i][0]);
- Delay_1ms(1);
- }
- status = MPU9255_Check();
- if(status != SUCCESS)
- return ERROR;
- Delay_10ms(1);
- #ifdef _USE_MAG_AK8963
- for(uint8_t i = 0; i < AK8963_InitRegNum; i++) {
- MPU9255_Mag_WriteReg(AK8963_InitData[i][1], AK8963_InitData[i][0]);
- Delay_10ms(1);
- }
- MPU9255_WriteReg(MPU6555_I2C_SLV0_REG, AK8963_ST1);
- MPU9255_WriteReg(MPU6555_I2C_SLV0_ADDR, AK8963_I2C_ADDR | 0x80);
- MPU9255_WriteReg(MPU6555_I2C_SLV0_CTRL, 0x80 | 8);
- #endif
- MPU9255_SetSpeed(SPIx_SPEED_HIGH);
- Delay_10ms(1);
- return SUCCESS;
- }
复制代码
- void MPU9255_getData( int16_t *dataIMU )
- {
- uint8_t tmpRead[22] = {0};
- #ifdef _USE_MAG_AK8963
- MPU9255_ReadRegs(MPU6555_ACCEL_XOUT_H, tmpRead, 22);
- #else
- MPU9255_ReadRegs(MPU6555_ACCEL_XOUT_H, tmpRead, 14);
- #endif
- dataIMU[0] = (Byte16(int16_t, tmpRead[6], tmpRead[7])); // Temp
- dataIMU[1] = (Byte16(int16_t, tmpRead[0], tmpRead[1])); // Acc.X
- dataIMU[2] = (Byte16(int16_t, tmpRead[2], tmpRead[3])); // Acc.Y
- dataIMU[3] = (Byte16(int16_t, tmpRead[4], tmpRead[5])); // Acc.Z
- dataIMU[4] = (Byte16(int16_t, tmpRead[8], tmpRead[9])); // Gyr.X
- dataIMU[5] = (Byte16(int16_t, tmpRead[10], tmpRead[11])); // Gyr.Y
- dataIMU[6] = (Byte16(int16_t, tmpRead[12], tmpRead[13])); // Gyr.Z
- #ifdef _USE_MAG_AK8963
- dataIMU[7] = (Byte16(int16_t, tmpRead[16], tmpRead[15])); // Mag.X
- dataIMU[8] = (Byte16(int16_t, tmpRead[18], tmpRead[17])); // Mag.Y
- dataIMU[9] = (Byte16(int16_t, tmpRead[20], tmpRead[19])); // Mag.Z
- #endif
- }
复制代码
- void SysTick_Handler( void )
- {
- int16_t IMU_Buf[12] = {0};
- int16_t readData[12] = {0};
- LED_R_Toggle;
- MPU9255_getData(readData);
- readData[1] = readData[1] - Bx; // Acc.X
- readData[2] = readData[2] - By; // Acc.Y
- readData[3] = readData[3] - Bz; // Acc.Z
- IMU_Buf[0] = (int16_t)(readData[1] * Sxx + readData[2] * Sxy + readData[3] * Sxz); // Acc.X
- IMU_Buf[1] = (int16_t)(readData[1] * Syx + readData[2] * Syy + readData[3] * Syz); // Acc.Y
- IMU_Buf[2] = (int16_t)(readData[1] * Szx + readData[2] * Szy + readData[3] * Szz); // Acc.Z
- IMU_Buf[3] = readData[4] * MPU9255G_2000dps; // Gyr.X
- IMU_Buf[4] = readData[5] * MPU9255G_2000dps; // Gyr.Y
- IMU_Buf[5] = readData[6] * MPU9255G_2000dps; // Gyr.Z
- IMU_Buf[6] = readData[7] * MPU9255M_4800uT; // Mag.X
- IMU_Buf[7] = readData[8] * MPU9255M_4800uT; // Mag.Y
- IMU_Buf[8] = readData[9] * MPU9255M_4800uT; // Mag.Z
- IMU_Buf[9] = readData[0] * MPU9255T_85degC; // temp
- Serial_sendDataMATLAB(IMU_Buf, 10);
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|