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
227 lines
5.8 KiB
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file adc.c
|
|
* @brief This file provides code for the configuration
|
|
* of the ADC instances.
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2025 STMicroelectronics.
|
|
* All rights reserved.
|
|
*
|
|
* This software is licensed under terms that can be found in the LICENSE file
|
|
* in the root directory of this software component.
|
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "bsp_adc.h"
|
|
|
|
/* USER CODE BEGIN 0 */
|
|
|
|
/* USER CODE END 0 */
|
|
|
|
ADC_HandleTypeDef hadc1;
|
|
ADC_HandleTypeDef hadc2;
|
|
|
|
/* ADC1 init function */
|
|
void MX_ADC1_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN ADC1_Init 0 */
|
|
|
|
/* USER CODE END ADC1_Init 0 */
|
|
|
|
ADC_ChannelConfTypeDef sConfig = {0};
|
|
|
|
/* USER CODE BEGIN ADC1_Init 1 */
|
|
|
|
/* USER CODE END ADC1_Init 1 */
|
|
|
|
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
|
|
*/
|
|
hadc1.Instance = ADC1;
|
|
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
|
|
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
|
|
hadc1.Init.ScanConvMode = DISABLE;
|
|
hadc1.Init.ContinuousConvMode = DISABLE;
|
|
hadc1.Init.DiscontinuousConvMode = DISABLE;
|
|
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
|
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
|
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
|
hadc1.Init.NbrOfConversion = 1;
|
|
hadc1.Init.DMAContinuousRequests = DISABLE;
|
|
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
|
|
if (HAL_ADC_Init(&hadc1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
|
|
*/
|
|
sConfig.Channel = ADC_CHANNEL_8;
|
|
sConfig.Rank = 1;
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
|
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN ADC1_Init 2 */
|
|
|
|
/* USER CODE END ADC1_Init 2 */
|
|
|
|
}
|
|
/* ADC2 init function */
|
|
void MX_ADC2_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN ADC2_Init 0 */
|
|
|
|
/* USER CODE END ADC2_Init 0 */
|
|
|
|
ADC_ChannelConfTypeDef sConfig = {0};
|
|
|
|
/* USER CODE BEGIN ADC2_Init 1 */
|
|
|
|
/* USER CODE END ADC2_Init 1 */
|
|
|
|
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
|
|
*/
|
|
hadc2.Instance = ADC2;
|
|
hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
|
|
hadc2.Init.Resolution = ADC_RESOLUTION_12B;
|
|
hadc2.Init.ScanConvMode = DISABLE;
|
|
hadc2.Init.ContinuousConvMode = DISABLE;
|
|
hadc2.Init.DiscontinuousConvMode = DISABLE;
|
|
hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
|
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
|
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
|
hadc2.Init.NbrOfConversion = 1;
|
|
hadc2.Init.DMAContinuousRequests = DISABLE;
|
|
hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
|
|
if (HAL_ADC_Init(&hadc2) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
|
|
*/
|
|
sConfig.Channel = ADC_CHANNEL_9;
|
|
sConfig.Rank = 1;
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
|
|
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN ADC2_Init 2 */
|
|
|
|
/* USER CODE END ADC2_Init 2 */
|
|
|
|
}
|
|
|
|
void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
|
|
{
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
if(adcHandle->Instance==ADC1)
|
|
{
|
|
/* USER CODE BEGIN ADC1_MspInit 0 */
|
|
|
|
/* USER CODE END ADC1_MspInit 0 */
|
|
/* ADC1 clock enable */
|
|
__HAL_RCC_ADC1_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
/**ADC1 GPIO Configuration
|
|
PB0 ------> ADC1_IN8
|
|
*/
|
|
GPIO_InitStruct.Pin = GPIO_PIN_0;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
/* USER CODE BEGIN ADC1_MspInit 1 */
|
|
|
|
/* USER CODE END ADC1_MspInit 1 */
|
|
}
|
|
else if(adcHandle->Instance==ADC2)
|
|
{
|
|
/* USER CODE BEGIN ADC2_MspInit 0 */
|
|
|
|
/* USER CODE END ADC2_MspInit 0 */
|
|
/* ADC2 clock enable */
|
|
__HAL_RCC_ADC2_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
/**ADC2 GPIO Configuration
|
|
PB1 ------> ADC2_IN9
|
|
*/
|
|
GPIO_InitStruct.Pin = GPIO_PIN_1;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
/* USER CODE BEGIN ADC2_MspInit 1 */
|
|
|
|
/* USER CODE END ADC2_MspInit 1 */
|
|
}
|
|
}
|
|
|
|
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
|
|
{
|
|
|
|
if(adcHandle->Instance==ADC1)
|
|
{
|
|
/* USER CODE BEGIN ADC1_MspDeInit 0 */
|
|
|
|
/* USER CODE END ADC1_MspDeInit 0 */
|
|
/* Peripheral clock disable */
|
|
__HAL_RCC_ADC1_CLK_DISABLE();
|
|
|
|
/**ADC1 GPIO Configuration
|
|
PB0 ------> ADC1_IN8
|
|
*/
|
|
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0);
|
|
|
|
/* USER CODE BEGIN ADC1_MspDeInit 1 */
|
|
|
|
/* USER CODE END ADC1_MspDeInit 1 */
|
|
}
|
|
else if(adcHandle->Instance==ADC2)
|
|
{
|
|
/* USER CODE BEGIN ADC2_MspDeInit 0 */
|
|
|
|
/* USER CODE END ADC2_MspDeInit 0 */
|
|
/* Peripheral clock disable */
|
|
__HAL_RCC_ADC2_CLK_DISABLE();
|
|
|
|
/**ADC2 GPIO Configuration
|
|
PB1 ------> ADC2_IN9
|
|
*/
|
|
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_1);
|
|
|
|
/* USER CODE BEGIN ADC2_MspDeInit 1 */
|
|
|
|
/* USER CODE END ADC2_MspDeInit 1 */
|
|
}
|
|
}
|
|
|
|
/* USER CODE BEGIN 1 */
|
|
uint32_t readFlowMeterADC() {
|
|
// 启动 ADC1 转换
|
|
if (HAL_ADC_Start(&hadc1) != HAL_OK) {
|
|
// 处理启动错误
|
|
}
|
|
|
|
// 等待转换完成
|
|
if (HAL_ADC_PollForConversion(&hadc1, 100) != HAL_OK) {
|
|
// 处理等待错误
|
|
}
|
|
|
|
// 获取转换结果
|
|
return HAL_ADC_GetValue(&hadc1);
|
|
}
|
|
/* USER CODE END 1 */
|