基质喷涂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
5.8 KiB

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file adc.c
  5. * @brief This file provides code for the configuration
  6. * of the ADC instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2025 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "bsp_adc.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. ADC_HandleTypeDef hadc1;
  25. ADC_HandleTypeDef hadc2;
  26. /* ADC1 init function */
  27. void MX_ADC1_Init(void)
  28. {
  29. /* USER CODE BEGIN ADC1_Init 0 */
  30. /* USER CODE END ADC1_Init 0 */
  31. ADC_ChannelConfTypeDef sConfig = {0};
  32. /* USER CODE BEGIN ADC1_Init 1 */
  33. /* USER CODE END ADC1_Init 1 */
  34. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  35. */
  36. hadc1.Instance = ADC1;
  37. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  38. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  39. hadc1.Init.ScanConvMode = DISABLE;
  40. hadc1.Init.ContinuousConvMode = DISABLE;
  41. hadc1.Init.DiscontinuousConvMode = DISABLE;
  42. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  43. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  44. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  45. hadc1.Init.NbrOfConversion = 1;
  46. hadc1.Init.DMAContinuousRequests = DISABLE;
  47. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  48. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  49. {
  50. Error_Handler();
  51. }
  52. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  53. */
  54. sConfig.Channel = ADC_CHANNEL_8;
  55. sConfig.Rank = 1;
  56. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  57. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  58. {
  59. Error_Handler();
  60. }
  61. /* USER CODE BEGIN ADC1_Init 2 */
  62. /* USER CODE END ADC1_Init 2 */
  63. }
  64. /* ADC2 init function */
  65. void MX_ADC2_Init(void)
  66. {
  67. /* USER CODE BEGIN ADC2_Init 0 */
  68. /* USER CODE END ADC2_Init 0 */
  69. ADC_ChannelConfTypeDef sConfig = {0};
  70. /* USER CODE BEGIN ADC2_Init 1 */
  71. /* USER CODE END ADC2_Init 1 */
  72. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  73. */
  74. hadc2.Instance = ADC2;
  75. hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  76. hadc2.Init.Resolution = ADC_RESOLUTION_12B;
  77. hadc2.Init.ScanConvMode = DISABLE;
  78. hadc2.Init.ContinuousConvMode = DISABLE;
  79. hadc2.Init.DiscontinuousConvMode = DISABLE;
  80. hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  81. hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  82. hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  83. hadc2.Init.NbrOfConversion = 1;
  84. hadc2.Init.DMAContinuousRequests = DISABLE;
  85. hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  86. if (HAL_ADC_Init(&hadc2) != HAL_OK)
  87. {
  88. Error_Handler();
  89. }
  90. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  91. */
  92. sConfig.Channel = ADC_CHANNEL_9;
  93. sConfig.Rank = 1;
  94. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  95. if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
  96. {
  97. Error_Handler();
  98. }
  99. /* USER CODE BEGIN ADC2_Init 2 */
  100. /* USER CODE END ADC2_Init 2 */
  101. }
  102. void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
  103. {
  104. GPIO_InitTypeDef GPIO_InitStruct = {0};
  105. if(adcHandle->Instance==ADC1)
  106. {
  107. /* USER CODE BEGIN ADC1_MspInit 0 */
  108. /* USER CODE END ADC1_MspInit 0 */
  109. /* ADC1 clock enable */
  110. __HAL_RCC_ADC1_CLK_ENABLE();
  111. __HAL_RCC_GPIOB_CLK_ENABLE();
  112. /**ADC1 GPIO Configuration
  113. PB0 ------> ADC1_IN8
  114. */
  115. GPIO_InitStruct.Pin = GPIO_PIN_0;
  116. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  117. GPIO_InitStruct.Pull = GPIO_NOPULL;
  118. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  119. /* USER CODE BEGIN ADC1_MspInit 1 */
  120. /* USER CODE END ADC1_MspInit 1 */
  121. }
  122. else if(adcHandle->Instance==ADC2)
  123. {
  124. /* USER CODE BEGIN ADC2_MspInit 0 */
  125. /* USER CODE END ADC2_MspInit 0 */
  126. /* ADC2 clock enable */
  127. __HAL_RCC_ADC2_CLK_ENABLE();
  128. __HAL_RCC_GPIOB_CLK_ENABLE();
  129. /**ADC2 GPIO Configuration
  130. PB1 ------> ADC2_IN9
  131. */
  132. GPIO_InitStruct.Pin = GPIO_PIN_1;
  133. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  134. GPIO_InitStruct.Pull = GPIO_NOPULL;
  135. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  136. /* USER CODE BEGIN ADC2_MspInit 1 */
  137. /* USER CODE END ADC2_MspInit 1 */
  138. }
  139. }
  140. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
  141. {
  142. if(adcHandle->Instance==ADC1)
  143. {
  144. /* USER CODE BEGIN ADC1_MspDeInit 0 */
  145. /* USER CODE END ADC1_MspDeInit 0 */
  146. /* Peripheral clock disable */
  147. __HAL_RCC_ADC1_CLK_DISABLE();
  148. /**ADC1 GPIO Configuration
  149. PB0 ------> ADC1_IN8
  150. */
  151. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0);
  152. /* USER CODE BEGIN ADC1_MspDeInit 1 */
  153. /* USER CODE END ADC1_MspDeInit 1 */
  154. }
  155. else if(adcHandle->Instance==ADC2)
  156. {
  157. /* USER CODE BEGIN ADC2_MspDeInit 0 */
  158. /* USER CODE END ADC2_MspDeInit 0 */
  159. /* Peripheral clock disable */
  160. __HAL_RCC_ADC2_CLK_DISABLE();
  161. /**ADC2 GPIO Configuration
  162. PB1 ------> ADC2_IN9
  163. */
  164. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_1);
  165. /* USER CODE BEGIN ADC2_MspDeInit 1 */
  166. /* USER CODE END ADC2_MspDeInit 1 */
  167. }
  168. }
  169. /* USER CODE BEGIN 1 */
  170. uint32_t readFlowMeterADC() {
  171. // 启动 ADC1 转换
  172. if (HAL_ADC_Start(&hadc1) != HAL_OK) {
  173. // 处理启动错误
  174. }
  175. // 等待转换完成
  176. if (HAL_ADC_PollForConversion(&hadc1, 100) != HAL_OK) {
  177. // 处理等待错误
  178. }
  179. // 获取转换结果
  180. return HAL_ADC_GetValue(&hadc1);
  181. }
  182. /* USER CODE END 1 */