赞
踩
目录
3.设置硬件IIC
欧拉角配置
STM32F1源码 提取码:impu
STM32F4源码 提取码:impu
- #include "main.h"
- #include "i2c.h"
- #include "usart.h"
- #include "gpio.h"
- /*说明
- 1.将这个MPU6050的文件夹复制到刚才创建的工程里
- 2.添加路径和文件夹,选择下载器,将.C文件添加到工程里
- 3.添加头文件,就是刚在添加的那几个文件
- 4.一直复制即可,后续将main函数放入csnd中
- 5.串口重定向,添加stdio.h再将下面代码复制到usart.c的最下面即可
- int fputc(int ch, FILE *f)
- {
- HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);
- return ch;
- }
-
- int fgetc(FILE *f)
- {
- uint8_t ch = 0;
- HAL_UART_Receive(&huart1, &ch, 1, 0xffff);
- return ch;
- }
- 6.这里就ok了
- */
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #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 */
- float pitch, roll, yaw;
- /* 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
- */
- 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_USART1_UART_Init();
- /* USER CODE BEGIN 2 */
- /*测试一下串口,这里我们用的是硬件IIC软甲IIC没有用到,就不需要添加软件IIC的代码了,还得设置这里*/
- /*语句放在串口初始化下面*/
- /*这里需要将串口重定向一下*/
- printf("-- Mpu6050 Project Start -- \r\n");
- HAL_Delay(1000);
- /*
- *DMP初始化,
- */
- while (mpu_dmp_init())
- {
- HAL_Delay(200);
- }
- printf("Successfully initialized!\r\n");
- /*
- *iic读取器件ID ,测试IIC能否使用,测试一下
- */
- printf ("Ready to read the ID\r\n");
- uint8_t recv = 0x00;
- HAL_I2C_Mem_Read(&hi2c1, (0x68 << 1), 0x75, I2C_MEMADD_SIZE_8BIT, &recv, 1, 0xfff);
- if (recv == 0x68)
- {
- printf("mpu6050 ID Read: OK at 0x68\r\n"); //成功
- }
- else
- {
- printf("Err mpu id:0x%x\r\n", recv); //失败
- }
- /* USER CODE END 2 */
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- while(mpu_dmp_get_data(&pitch, &roll, &yaw) != 0); //获取欧拉角的参数
- printf("%f,%f,%f\r\n", pitch, roll, yaw); //打印到串口上
-
- HAL_Delay(500);
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。