找回密码
 立即注册

QQ登录

只需一步,快速开始

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

用stm32cubemx编写的读取MPU9255程序 使用HAL库

[复制链接]
跳转到指定楼层
楼主
ID:386098 发表于 2018-8-14 10:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
此处用I2C来读取。使用HAL库。

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

  8. /* USER CODE BEGIN Includes */
  9.     #ifdef  __GUNC__
  10.                 #define PUTCHAR_PROTOTYPE  int __io_putchr(int ch)
  11.                 #else
  12.                 #define PUTCHAR_PROTOTYPE int fputc(int ch,FILE *f)
  13.                 #endif
  14.                
  15.                
  16.                 PUTCHAR_PROTOTYPE
  17.                 {
  18.                         HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xFFFF);
  19.                         return ch;
  20.                 }
  21. /* USER CODE END Includes */

  22. /* Private variables ---------------------------------------------------------*/

  23. /* USER CODE BEGIN PV */
  24. /* Private variables ---------------------------------------------------------*/
  25.                 extern I2C_HandleTypeDef hi2c2;
  26.     extern int16_t g[3],a[3];
  27. /* USER CODE END PV */

  28. /* Private function prototypes -----------------------------------------------*/
  29. void SystemClock_Config(void);

  30. /* USER CODE BEGIN PFP */
  31. /* Private function prototypes -----------------------------------------------*/

  32. /* USER CODE END PFP */

  33. /* USER CODE BEGIN 0 */

  34. /* USER CODE END 0 */

  35. /**
  36.   * @brief  The application entry point.
  37.   *
  38.   * @retval None
  39.   */
  40. int main(void)
  41. {
  42.   /* USER CODE BEGIN 1 */
  43.     int16_t         Re=3;
  44.                 uint8_t Date = 0;
  45.                 uint8_t date = 0x75;
  46.   /* USER CODE END 1 */

  47.   /* MCU Configuration----------------------------------------------------------*/

  48.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  49.   HAL_Init();

  50.   /* USER CODE BEGIN Init */

  51.   /* USER CODE END Init */

  52.   /* Configure the system clock */
  53.   SystemClock_Config();

  54.   /* USER CODE BEGIN SysInit */

  55.   /* USER CODE END SysInit */

  56.   /* Initialize all configured peripherals */
  57.   MX_GPIO_Init();
  58.   MX_I2C2_Init();
  59.   MX_USART1_UART_Init();
  60.   /* USER CODE BEGIN 2 */
  61.      HAL_Delay(100);
  62.      MPU_Init();
  63. //Re=MPU_Read_Byte(0x75);
  64.   /* USER CODE END 2 */

  65.   /* Infinite loop */
  66.   /* USER CODE BEGIN WHILE */
  67.   while (1)
  68.   {

  69.   /* USER CODE END WHILE */

  70.   /* USER CODE BEGIN 3 */
  71.    //Re=MPU_Get_Temperature();
  72.        
  73.         if(HAL_I2C_Mem_Read(&hi2c2,MPU_9255_AddR,date,1,&Date,1,10) == HAL_OK)
  74.                 {
  75.                         printf("\r\n MPU9255 Test OK \r\n");
  76.                                 if(MPU_Get_Gyroscope()==0)
  77.                                         printf("\r\n 陀螺仪:GX:%8d,GY:%8d,GZ:%8d\r\n",g[0],g[1],g[2]);
  78.                                 else
  79.                                         printf("\r\n Gyro Read False\r\n");
  80.                                 if(MPU_Get_Accelerometer()==0)
  81.                                        printf("\r\n 加速度:AX:%8d,AY:%8d,AZ:%8d\r\n",a[0],a[1],a[2]);
  82.                                         else
  83.                                                 printf("\r\n Acceleration Read False\r\n");
  84.                                 if ((Re=MPU_Get_Temperature())!=0)
  85.                                            printf("\r\n 温度:T:%8d\r\n",Re);
  86.                                 else
  87.                                         printf("\r\n Temperature Read False\r\n");
  88.                 }
  89.         else
  90.         {
  91.                 printf("\r\n MPU9255 Test False \r\n");
  92.         }       
  93.         printf("\r\n \r\n \r\n \r\n");

  94.                 HAL_Delay(500);
  95.   /* USER CODE END 3 */

  96. }
  97. }
  98. /**
  99.   * @brief System Clock Configuration
  100.   * @retval None
  101.   */
  102. void SystemClock_Config(void)
  103. {

  104.   RCC_OscInitTypeDef RCC_OscInitStruct;
  105.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  106.     /**Initializes the CPU, AHB and APB busses clocks
  107.     */
  108.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  109.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  110.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  111.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  112.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  113.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  114.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  115.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  116.   {
  117.     _Error_Handler(__FILE__, __LINE__);
  118.   }

  119.     /**Initializes the CPU, AHB and APB busses clocks
  120.     */
  121.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  122.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  123.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  124.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  125.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  126.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  127.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  128.   {
  129.     _Error_Handler(__FILE__, __LINE__);
  130.   }

  131.     /**Configure the Systick interrupt time
  132.     */
  133.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  134.     /**Configure the Systick
  135.     */
  136.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  137.   /* SysTick_IRQn interrupt configuration */
  138.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  139. }

  140. /* USER CODE BEGIN 4 */

  141. /* USER CODE END 4 */

  142. /**
  143.   * @brief  This function is executed in case of error occurrence.
  144.   * @param  file: The file name as string.
  145.   * @param  line: The line in file as a number.
  146.   * @retval None
  147.   */
  148. void _Error_Handler(char *file, int line)
  149. {
  150.   /* USER CODE BEGIN Error_Handler_Debug */
  151.   /* User can add his own implementation to report the HAL error return state */
  152.   while(1)
  153.   {
  154.   }
  155.   /* USER CODE END Error_Handler_Debug */
  156. }

  157. #ifdef  USE_FULL_ASSERT
  158. /**
  159.   * @brief  Reports the name of the source file and the source line number
  160.   *         where the assert_param error has occurred.
  161.   * @param  file: pointer to the source file name
  162.   * @param  line: assert_param error line source number
  163.   * @retval None
  164.   */
  165. void assert_failed(uint8_t* file, uint32_t line)
  166. {
  167.   /* USER CODE BEGIN 6 */
  168.   /* User can add his own implementation to report the file name and line number,
  169.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  170.   /* USER CODE END 6 */
  171. }
  172. #endif /* USE_FULL_ASSERT */

  173. /**
  174.   * @}
  175.   */

  176. /**
  177.   * @}
  178.   */

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

所有资料51hei提供下载:
I2C.rar (9.14 MB, 下载次数: 55)


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

使用道具 举报

沙发
ID:520235 发表于 2019-12-27 16:41 | 只看该作者
感谢分享,下载来看看学习一下!不知道MPU9250是否也类似。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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