当前位置:   article > 正文

STM32 HAL 驱动PM2.5传感器(GP2Y10AU气体检测模块)_stm32f103c8t6引脚相连的gp2y10

stm32f103c8t6引脚相连的gp2y10

目录

1、简介

2、CubeMX初始化配置

2.1 基础配置

2.1.1 SYS配置

 2.1.2 RCC配置

2.2 ADC外设配置

2.3 串口外设配置

 2.4 项目生成

 3、KEIL端程序整合

3.1 串口重映射

3.2 ADC数据采集

3.3 主函数代

3.4 效果展示


1、简介

本文通过STM32F103C8T6单片机通过HAL库方式对GP2Y10AU气体检测模块进行数据的读取,并通过串口来进行显示。

2、CubeMX初始化配置

2.1 基础配置

2.1.1 SYS配置

 2.1.2 RCC配置

2.2 ADC外设配置

2.3 串口外设配置

 2.4 项目生成

 3、KEIL端程序整合

3.1 串口重映射

具体步骤:stm32(HAL库)使用printf函数打印到串口

#include "stdio.h"
  1. #ifdef __GNUC__
  2. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  3. #else
  4. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  5. #endif
  6. PUTCHAR_PROTOTYPE
  7. {
  8. HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xFFFF);//阻塞方式打印
  9. return ch;
  10. }

3.2 ADC数据采集

首先在adc.c最下方添加ADC采集程序,如下所示:

  1. uint16_t ADC_IN_1(void) //ADC采集程序
  2. {
  3. HAL_ADC_Start(&hadc1);//开始ADC采集
  4. HAL_ADC_PollForConversion(&hadc1,500);//等待采集结束
  5. if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))//读取ADC完成标志位
  6. {
  7. return HAL_ADC_GetValue(&hadc1);//读出ADC数值
  8. }
  9. return 0;
  10. }

接着在adc.h中进行函数声明,如下图所示:

uint16_t ADC_IN_1(void);//ADC采集程序

3.3 主函数代码整合

首先定义变量接受ADC_IN_1()采集数据,如下图所示:

uint8_t adc_PM2.5;

接着进行ADC采样校准,如下图所示:

HAL_ADCEx_Calibration_Start(&hadc1);//ADC采样校准

 最后进行数据打印,如下图所示:

  1. adc_mq2 = ADC_IN_1();
  2. printf("%d",adc_mq2);
  3. HAL_Delay(500);

 所用代码(便于复制)如下:

  1. uint8_t adc_mq2;
  2. HAL_ADCEx_Calibration_Start(&hadc1);//ADC采样校准
  3. adc_mq2 = ADC_IN_1();
  4. printf("%d",adc_mq2);
  5. HAL_Delay(200);

 main.c如下所示:

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "usart.h"
  23. #include "gpio.h"
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. uint8_t adc_PM2_5;
  27. /* USER CODE END Includes */
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */
  30. /* USER CODE END PTD */
  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN PD */
  33. /* USER CODE END PD */
  34. /* Private macro -------------------------------------------------------------*/
  35. /* USER CODE BEGIN PM */
  36. /* USER CODE END PM */
  37. /* Private variables ---------------------------------------------------------*/
  38. /* USER CODE BEGIN PV */
  39. /* USER CODE END PV */
  40. /* Private function prototypes -----------------------------------------------*/
  41. void SystemClock_Config(void);
  42. /* USER CODE BEGIN PFP */
  43. /* USER CODE END PFP */
  44. /* Private user code ---------------------------------------------------------*/
  45. /* USER CODE BEGIN 0 */
  46. /* USER CODE END 0 */
  47. /**
  48. * @brief The application entry point.
  49. * @retval int
  50. */
  51. int main(void)
  52. {
  53. /* USER CODE BEGIN 1 */
  54. /* USER CODE END 1 */
  55. /* MCU Configuration--------------------------------------------------------*/
  56. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  57. HAL_Init();
  58. /* USER CODE BEGIN Init */
  59. /* USER CODE END Init */
  60. /* Configure the system clock */
  61. SystemClock_Config();
  62. /* USER CODE BEGIN SysInit */
  63. /* USER CODE END SysInit */
  64. /* Initialize all configured peripherals */
  65. MX_GPIO_Init();
  66. MX_ADC1_Init();
  67. MX_USART1_UART_Init();
  68. /* USER CODE BEGIN 2 */
  69. HAL_ADCEx_Calibration_Start(&hadc1);//ADC采样校准
  70. /* USER CODE END 2 */
  71. /* Infinite loop */
  72. /* USER CODE BEGIN WHILE */
  73. while (1)
  74. {
  75. adc_PM2_5 = ADC_IN_1();
  76. printf("%d",adc_PM2_5);
  77. HAL_Delay(500);
  78. /* USER CODE END WHILE */
  79. /* USER CODE BEGIN 3 */
  80. }
  81. /* USER CODE END 3 */
  82. }
  83. /**
  84. * @brief System Clock Configuration
  85. * @retval None
  86. */
  87. void SystemClock_Config(void)
  88. {
  89. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  90. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  91. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  92. /** Initializes the RCC Oscillators according to the specified parameters
  93. * in the RCC_OscInitTypeDef structure.
  94. */
  95. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  96. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  97. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  98. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  99. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  100. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  101. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  102. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  103. {
  104. Error_Handler();
  105. }
  106. /** Initializes the CPU, AHB and APB buses clocks
  107. */
  108. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  109. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  110. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  111. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  112. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  113. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  114. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  115. {
  116. Error_Handler();
  117. }
  118. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  119. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  120. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  121. {
  122. Error_Handler();
  123. }
  124. }
  125. /* USER CODE BEGIN 4 */
  126. /* USER CODE END 4 */
  127. /**
  128. * @brief This function is executed in case of error occurrence.
  129. * @retval None
  130. */
  131. void Error_Handler(void)
  132. {
  133. /* USER CODE BEGIN Error_Handler_Debug */
  134. /* User can add his own implementation to report the HAL error return state */
  135. __disable_irq();
  136. while (1)
  137. {
  138. }
  139. /* USER CODE END Error_Handler_Debug */
  140. }
  141. #ifdef USE_FULL_ASSERT
  142. /**
  143. * @brief Reports the name of the source file and the source line number
  144. * where the assert_param error has occurred.
  145. * @param file: pointer to the source file name
  146. * @param line: assert_param error line source number
  147. * @retval None
  148. */
  149. void assert_failed(uint8_t *file, uint32_t line)
  150. {
  151. /* USER CODE BEGIN 6 */
  152. /* User can add his own implementation to report the file name and line number,
  153. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  154. /* USER CODE END 6 */
  155. }
  156. #endif /* USE_FULL_ASSERT */

4 硬件连接 

 从左至右依次编号为1-6(白色为1,红色为6)

1---- 5V

2---- GND

3----D0(不接)

4----GND

5----AO

6----5V

 

5 效果展示

上文如有错误,恳请各位大佬指正。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/554129
推荐阅读
相关标签
  

闽ICP备14008679号