当前位置:   article > 正文

STM32之烟雾传感器_stm32烟雾

stm32烟雾

目录

原理:

1、寄存器配置

(1) 由原理图可知,寄存器配置PA4(传感器的采集值)

(2)配置DMA(优化选项,可以不用)​编辑(3)生成代码

​编辑​编辑2、代码

(1)修改代码

(2)编译+下载

3、显示结果

(1)配置串口调试助手

​编辑(2)按下复位键

(3)按下五向按键,显示结果


原理:

实现五向按键的中断、ADC转化、DMA

CSDN在此基础上添加功能DMA即可

1、寄存器配置

(1) 由原理图可知,寄存器配置PA4(传感器的采集值)

(2)配置DMA(优化选项,可以不用)(3)生成代码

2、代码

(1)修改代码

重要代码:

  1. uint32_t adcData[2] = {0};
  2. HAL_ADC_Start_DMA(&hadc, adcData, 2); //这里的第三个参数2 -- 指的是DMA的转换通道;
  3. printf("keyvalue:%d\n",adcData[0]); //显示按键按下之后的值
  4. printf("gasvalue:%d\n",adcData[1]); //显示传感器的值

全部代码

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2024 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 "dma.h"
  23. #include "usart.h"
  24. #include "gpio.h"
  25. /* Private includes ----------------------------------------------------------*/
  26. /* USER CODE BEGIN Includes */
  27. #include "stdio.h"
  28. /* USER CODE END Includes */
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31. /* USER CODE END PTD */
  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34. /* USER CODE END PD */
  35. /* Private macro -------------------------------------------------------------*/
  36. /* USER CODE BEGIN PM */
  37. /* USER CODE END PM */
  38. /* Private variables ---------------------------------------------------------*/
  39. /* USER CODE BEGIN PV */
  40. /* USER CODE END PV */
  41. /* Private function prototypes -----------------------------------------------*/
  42. void SystemClock_Config(void);
  43. /* USER CODE BEGIN PFP */
  44. /* USER CODE END PFP */
  45. /* Private user code ---------------------------------------------------------*/
  46. /* USER CODE BEGIN 0 */
  47. //定义一个代表按键按下的标志位
  48. int KEY_FLAG = 0;
  49. //按键中断的回调函数
  50. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  51. {
  52. printf("key pressed\n");
  53. KEY_FLAG = 1;
  54. }
  55. //printf()功能实现
  56. int fputc(int c, FILE* S)
  57. {
  58. uint8_t tem = c;
  59. HAL_UART_Transmit(&huart1, &tem,sizeof(tem),HAL_MAX_DELAY);
  60. return c;
  61. }
  62. /* USER CODE END 0 */
  63. /**
  64. * @brief The application entry point.
  65. * @retval int
  66. */
  67. int main(void)
  68. {
  69. /* USER CODE BEGIN 1 */
  70. /* USER CODE END 1 */
  71. /* MCU Configuration--------------------------------------------------------*/
  72. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  73. HAL_Init();
  74. /* USER CODE BEGIN Init */
  75. /* USER CODE END Init */
  76. /* Configure the system clock */
  77. SystemClock_Config();
  78. /* USER CODE BEGIN SysInit */
  79. /* USER CODE END SysInit */
  80. /* Initialize all configured peripherals */
  81. MX_GPIO_Init();
  82. MX_DMA_Init();
  83. MX_USART1_UART_Init();
  84. MX_ADC_Init();
  85. /* USER CODE BEGIN 2 */
  86. uint32_t adcData[2] = {0};
  87. /* USER CODE END 2 */
  88. /* Infinite loop */
  89. /* USER CODE BEGIN WHILE */
  90. while (1)
  91. {
  92. if(1 == KEY_FLAG)
  93. {
  94. //开始ADC的转换---电压值&可燃气体
  95. HAL_ADC_Start_DMA(&hadc, adcData, 2); //这里的第三个参数2 -- 指的是DMA的转换通道;
  96. printf("keyvalue:%d\n",adcData[0]);
  97. printf("gasvalue:%d\n",adcData[1]);
  98. //将标志位重新归零
  99. KEY_FLAG = 0;
  100. }
  101. /* USER CODE END WHILE */
  102. /* USER CODE BEGIN 3 */
  103. }
  104. /* USER CODE END 3 */
  105. }
  106. /**
  107. * @brief System Clock Configuration
  108. * @retval None
  109. */
  110. void SystemClock_Config(void)
  111. {
  112. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  113. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  114. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  115. /** Initializes the RCC Oscillators according to the specified parameters
  116. * in the RCC_OscInitTypeDef structure.
  117. */
  118. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI14;
  119. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  120. RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
  121. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  122. RCC_OscInitStruct.HSI14CalibrationValue = 16;
  123. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  124. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  125. {
  126. Error_Handler();
  127. }
  128. /** Initializes the CPU, AHB and APB buses clocks
  129. */
  130. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  131. |RCC_CLOCKTYPE_PCLK1;
  132. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  133. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  134. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  135. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  136. {
  137. Error_Handler();
  138. }
  139. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  140. PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
  141. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  142. {
  143. Error_Handler();
  144. }
  145. }
  146. /* USER CODE BEGIN 4 */
  147. /* USER CODE END 4 */
  148. /**
  149. * @brief This function is executed in case of error occurrence.
  150. * @retval None
  151. */
  152. void Error_Handler(void)
  153. {
  154. /* USER CODE BEGIN Error_Handler_Debug */
  155. /* User can add his own implementation to report the HAL error return state */
  156. __disable_irq();
  157. while (1)
  158. {
  159. }
  160. /* USER CODE END Error_Handler_Debug */
  161. }
  162. #ifdef USE_FULL_ASSERT
  163. /**
  164. * @brief Reports the name of the source file and the source line number
  165. * where the assert_param error has occurred.
  166. * @param file: pointer to the source file name
  167. * @param line: assert_param error line source number
  168. * @retval None
  169. */
  170. void assert_failed(uint8_t *file, uint32_t line)
  171. {
  172. /* USER CODE BEGIN 6 */
  173. /* User can add his own implementation to report the file name and line number,
  174. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  175. /* USER CODE END 6 */
  176. }
  177. #endif /* USE_FULL_ASSERT */

(2)编译+下载

3、显示结果

(1)配置串口调试助手

(2)按下复位键

(3)按下五向按键,显示结果

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

闽ICP备14008679号