找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3133|回复: 4
打印 上一主题 下一主题
收起左侧

stm32f411通过硬件i2c读取mup9250数据的程序

[复制链接]
跳转到指定楼层
楼主
ID:336587 发表于 2018-5-25 11:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
stm32f411通过硬件i2c读取数据的程序

单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "stm32f4xx_hal.h"
  4. #include "i2c.h"
  5. #include "usart.h"
  6. #include "gpio.h"
  7. #include "mpu9250.h"


  8. /* Private function prototypes -----------------------------------------------*/
  9. void SystemClock_Config(void);

  10. int main(void)
  11. {
  12.          short Accel[3];
  13.        
  14.   HAL_Init();
  15.   SystemClock_Config();
  16.   MX_GPIO_Init();
  17.   MX_USART2_UART_Init();
  18.   MX_I2C1_Init();
  19.         MPU9250_Init();
  20.         MPU9250_ID();

  21.   while (1)
  22.   {
  23.                 MPU9250ReadAcc(Accel);
  24.                 printf("%8d ,%8d ,%8d\r\n",Accel[0],Accel[1],Accel[2]);
  25.                 HAL_Delay(1);


  26.   }

  27. }

  28. /**
  29.   * @brief System Clock Configuration
  30.   * @retval None
  31.   */
  32. void SystemClock_Config(void)
  33. {

  34.   RCC_OscInitTypeDef RCC_OscInitStruct;
  35.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  36.     /**Configure the main internal regulator output voltage
  37.     */
  38.   __HAL_RCC_PWR_CLK_ENABLE();

  39.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  40.     /**Initializes the CPU, AHB and APB busses clocks
  41.     */
  42.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  43.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  44.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  45.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  46.   RCC_OscInitStruct.PLL.PLLM = 8;
  47.   RCC_OscInitStruct.PLL.PLLN = 144;
  48.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  49.   RCC_OscInitStruct.PLL.PLLQ = 4;
  50.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  51.   {
  52.     _Error_Handler(__FILE__, __LINE__);
  53.   }

  54.     /**Initializes the CPU, AHB and APB busses clocks
  55.     */
  56.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  57.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  58.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  59.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  60.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  61.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  62.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  63.   {
  64.     _Error_Handler(__FILE__, __LINE__);
  65.   }

  66.     /**Configure the Systick interrupt time
  67.     */
  68.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  69.     /**Configure the Systick
  70.     */
  71.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  72.   /* SysTick_IRQn interrupt configuration */
  73.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  74. }


  75. void _Error_Handler(char *file, int line)
  76. {
  77.   /* USER CODE BEGIN Error_Handler_Debug */
  78.   /* User can add his own implementation to report the HAL error return state */
  79.   while(1)
  80.   {
  81.   }
  82.   /* USER CODE END Error_Handler_Debug */
  83. }

  84. #ifdef  USE_FULL_ASSERT
  85. /**
  86.   * @brief  Reports the name of the source file and the source line number
  87.   *         where the assert_param error has occurred.
  88.   * @param  file: pointer to the source file name
  89.   * @param  line: assert_param error line source number
  90.   * @retval None
  91.   */
  92. void assert_failed(uint8_t* file, uint32_t line)
  93. {
  94.   /* USER CODE BEGIN 6 */
  95.   /* User can add his own implementation to report the file name and line number,
  96.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  97.   /* USER CODE END 6 */
  98. }
  99. #endif /* USE_FULL_ASSERT */

  100. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
stm32f411_mup9250.rar (504.16 KB, 下载次数: 51)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:170522 发表于 2018-7-26 11:29 | 只看该作者
谢谢分享
回复

使用道具 举报

板凳
ID:96552 发表于 2018-7-26 19:04 | 只看该作者

谢谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表