MPU9250的DMP移植
前言
不废话了,直接讲怎么移植MPU9250的DMP库,基于CubeMx,硬件STM32F103C8TC,正点原子9250实验历程,软件IAR和Keil。
一、配置CubeMx
1.串口
其实要改的很简单,直接配置IIC,关于时钟,调试的配置大家应该都会了。
先打开一个串口发送数据,用于测试。很简单,默认选项就可以了。
1.I2C
I2C的配置如图
二、正点原子DMP库修改
1.DMP文件添加
复制正点原子的DMP文件,9250文件,移到自己的工程目录。
为了简单我直接把所以内部文件搞到一个文件夹,这样添加起来方便。这里要注意替换一下里面的lib文件改成M3的文件,具体文件目录在官方的库文件里面。根据自己的编译器选择,我这里IAR的和KEIL的都移植了。
然后就是工程里面添加文件了,具体目录如下。
2.程序修改
1.第一个修改MPU9250文件
//.h文件
#ifndef _MPU9250_H
#define _MPU9250_H
#include "main.h"
#include "inv_mpu.h"
#include "inv_mpu_dmp_motion_driver.h"
//u16替换为uint16_t
//u8替换为uint8_t
---------------------------------------------------
//.c文件
#include "mpu9250.h"
#include "i2c.h"
//u16替换为uint16_t
//u8替换为uint8_t
//delay_ms替换为HAL_Delay
uint8_t MPU9250_Init(void)//注释掉IIC初始化函数
{
//IIC_Init(); //初始化IIC总线
..........................
}
2.替换原来的IIC读写函数
//IIC连续写
//addr:器件地址
//reg:寄存器地址
//len:写入长度
//buf:数据区
//返回值:0,正常
// 其他,错误代码
uint8_t MPU_Write_Len(uint8_t addr,uint8_t reg,uint8_t len,uint8_t *buf)
{
HAL_I2C_Mem_Write( &hi2c1, addr<<1,reg,1,buf,len,20);
}
//IIC连续读
//addr:器件地址
//reg:要读取的寄存器地址
//len:要读取的长度
//buf:读取到的数据存储区
//返回值:0,正常
// 其他,错误代码
uint8_t MPU_Read_Len(uint8_t addr,uint8_t reg,uint8_t len,uint8_t *buf)
{
HAL_I2C_Mem_Read( &hi2c1,addr<<1,reg,1,buf,len,20);
}
//IIC写一个字节
//reg:寄存器地址
//data:数据
//返回值:0,正常
// 其他,错误代码
uint8_t MPU_Write_Byte(uint8_t devaddr,uint8_t reg,uint8_t data)
{
HAL_I2C_Mem_Write( &hi2c1,devaddr<<1,reg,1,&data,1,20);
return 0;
}
//IIC读一个字节
//reg:寄存器地址
//返回值:读到的数据
uint8_t MPU_Read_Byte(uint8_t devaddr,uint8_t reg)
{
uint8_t data;
HAL_I2C_Mem_Read( &hi2c1,devaddr<<1,reg,1,&data,1,20);
return data;
}
3.修改inv_mpu.c文件
//#include "myiic.h"
//#include "delay.h"
//#include "usart.h"
//#include "mpu9250.h"
//改成
#include "mpu9250.h"
//u16替换为uint16_t
//u8替换为uint8_t
#define i2c_write MPU_Write_Len
#define i2c_read MPU_Read_Len
#define delay_ms HAL_Delay
#define get_ms mget_ms
-------------------------------------------------------
//inv_mpu.h文件
#ifndef _INV_MPU_H_
#define _INV_MPU_H_
#include "main.h"
4.修改log_stm32.c
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "packet.h"
#include "log.h"
#include "stm32f1xx.h" //原来是F4的
#include "usart.h"
5.添加宏定义
USE_HAL_DRIVER
STM32F103xB
MPL_LOG_NDEBUG=1
EMPL
MPU9250
EMPL_TARGET_STM32F4
到这里编译可以通过了。
三.数据验证
代码如下(示例):
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
#include "mpu9250.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
float yaw,pitch,roll;
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART3_UART_Init();
MPU9250_Init();
while(mpu_dmp_init());
mpu_mpl_get_data(&pitch,&roll,&yaw);
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
HAL_Delay(20);
mpu_mpl_get_data(&pitch,&roll,&yaw);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
可以看到到这一步基本上通过了
也可以使用串口发送一下数据进行测试
四、一些问题
俯仰角的测量虽然是准,但是变化速度有点慢,感觉有好几秒的延迟,不知道谁知道怎么回事,还是说正常情况。欢迎大家给我解答一下。下载连接如下。
https://download.csdn.net/download/qq_15504787/15142191