[Cubemx][HAL]使用STM32自带的IIC驱动mpu6050---2


Cubemx配置部分

 

 

 

 

 

 

 

 

代码部分

主要用到这几个

                                  下载:sysbrk/STM32-HAL-MPU6050-DMP解算获取角度和温度 - 码云 - 开源中国 (gitee.com)

 

 

在移植的时候记得改mpu6050.c里的这10个地方:

如果用的是I2C1那就不用改,是其它的话要改

 

 

  

 

 

 

主函数:

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "mpu6050.h"
#include "inv_mpu.h"
#include "inv_mpu_dmp_motion_driver.h"
/* 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 */
int __io_putchar(int ch) {
    uint8_t temp[1] = {ch};
    HAL_UART_Transmit(&huart1, temp, 1, 0xff);
    return (ch);
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
    float pitch, roll, yaw;            //欧拉角
    short aacx, aacy, aacz;            //加速度传感器原始数据
    short gyrox, gyroy, gyroz;         //陀螺仪原始数据
    float temp;                        //温度
  /* 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_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
    MPU_Init();
    mpu_dmp_init();
    printf("初始化成功!\n");
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
      HAL_Delay(500);
      while (mpu_dmp_get_data(&pitch, &roll, &yaw));    //必须要用while等待,才能读取成功
      MPU_Get_Accelerometer(&aacx, &aacy, &aacz);          //得到加速度传感器数据
      MPU_Get_Gyroscope(&gyrox, &gyroy, &gyroz);           //得到陀螺仪数据
      temp = MPU_Get_Temperature();                                  //得到温度信息
      printf("X:%.1f°  Y:%.1f°  Z:%.1f°  %.2f°C\r\n", roll, pitch, yaw, temp / 100);//串口1输出采集信息
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

 

 

输出:

 

 

posted @ 2022-08-10 17:13  grey_electron  阅读(397)  评论(0编辑  收藏  举报