32HAL——ADC模数转换多通道功能
注:ADC通道频率不易太快

一、ADC数模转换
1. ADC常用函数
HAL_ADCEx_Calibration_Start(&hadc1); //电压校准函数
2. cubeMx配置
Continuous Conversion Mode(持续转换模式):开启后只需启动一次ADC转换即可

3. 示例程序
该代码实现了测量某引脚外部输入电压
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @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 "main.h"
#include "adc.h"
#include "i2c.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "oled.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 *//* USER CODE END 0 *//*** @brief The application entry point.* @retval int*/
int main(void)
{/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_ADC1_Init();MX_I2C2_Init();/* USER CODE BEGIN 2 */HAL_Delay(20);OLED_Init();uint32_t value = 0;float voltage = 0.00f;char message[50];HAL_ADCEx_Calibration_Start(&hadc1); //电压校准函数// 若开启Continuous Conversion Mode(连续转换模式),则只需在初始化之后开启并等待完成一次即可HAL_ADC_Start(&hadc1);HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){OLED_NewFrame();// 若未开启Continuous Conversion Mode(连续转换模式),则需要不断开启并等待转换完成// HAL_ADC_Start(&hadc1);// HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY); //等待转换完成value = HAL_ADC_GetValue(&hadc1);voltage = value / 4095.0f * 3.3f;sprintf(message, "Voltage: %.2fv", voltage);OLED_PrintASCIIString(0, 0, message, &afont8x6, OLED_COLOR_NORMAL);OLED_ShowFrame();HAL_Delay(500);/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}
二、多通道功能
1. cubeMx配置
这里开启了3个通道,分别为:IN5(PA5引脚外部输入电压);Temperature Sensor Channel(片内温度通道);Vrefint Channel(基准电压通道)
Scan Conversion Mode(扫描模式):循环扫描各个通道
SampLing Time(测量周期):由图4图5可知,内部参考电压测量时间大概为61.2个周期,避免出错,这里设置为71.5个周期。





2. 示例程序
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @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 "main.h"
#include "adc.h"
#include "dma.h"
#include "i2c.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>#include "oled.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint16_t values[3];
char message[50];
float voltage = 0.0f;
float vref = 0.0f; //参考电压// void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
// if (hadc == &hadc1) {
// sprintf(message, "%d %d %d", values[0], values[1], values[2]);
// OLED_PrintASCIIString(0, 0, message, &afont8x6, OLED_COLOR_NORMAL);
// }
// }/* USER CODE END 0 *//*** @brief The application entry point.* @retval int*/
int main(void)
{/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_DMA_Init();MX_ADC1_Init();MX_I2C2_Init();/* USER CODE BEGIN 2 */HAL_Delay(20);OLED_Init();HAL_ADCEx_Calibration_Start(&hadc1);HAL_ADC_Start_DMA(&hadc1, (uint32_t *)values, 3);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){OLED_NewFrame();vref = 1.2 * (4095.0 / values[2]);voltage = (values[0] / 4095.0f) * vref;sprintf(message, "ADC:%d voltage:%.2f", values[0], voltage);// sprintf(message, "%d %d %d", values[0], values[1], values[2]);OLED_PrintASCIIString(0, 0, message, &afont8x6, OLED_COLOR_NORMAL);OLED_ShowFrame();HAL_Delay(500);/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}
