【HAL库+cubeMX基础】利用ADC规则通道和注入通道读取传感器数据
·
ADC规则通道
规则通道用于常规的模数转换任务,通常按预定义的顺序扫描多个输入通道。规则通道组中的通道转换顺序可编程,转换结果存储在固定的数据寄存器中。规则通道适用于需要连续或周期性采样多个模拟信号的场景,例如多路传感器数据采集。
ADC注入通道
注入通道用于高优先级的中断式模数转换,可打断规则通道的转换过程。注入通道组支持最多4个通道,转换顺序和触发条件独立于规则通道。转换结果存储在专用的注入数据寄存器中。注入通道适用于需要快速响应紧急事件的场景,例如过压保护或故障检测。
主要区别
- 优先级:注入通道可抢占规则通道的转换,响应延迟更低。
- 寄存器:规则通道使用DR寄存器,注入通道使用JDRx寄存器。
- 触发方式:规则通道通常由定时器触发,注入通道支持外部触发或软件触发。
- 队列长度:规则通道组支持更多通道(如16个),注入通道通常限制为4个。
注:具体特性可能因芯片型号(如STM32)而异,需参考对应参考手册确认细节。
本实验是基于STM32f1HAL库利用ADC规则通道和注入通道读取传感器数据
CubeMX配置
在CubeMX选配好单片机后点击【System Core】——【SYS】——【Debug】选择Serial Wire

然后配置时钟,点击【RCC】在高速时钟和低速时钟选择晶振

然后将时钟配置为72MHz

打开USART1方便单片机与电脑通过CH340连接

点击【Analog】——【ADC1】选择通道4和通道5

打开ADC中断后对其进行配置(注意通道4为规则通道,通道5为注入通道)

代码部分
将CubeMX配置好后,新建文件名,选择【MDK-ARM】后就可以编写代码了
/* 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 "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include<stdio.h>
#include<string.h>
#include<math.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 */
uint32_t adc_val[2];//存储传感器ADC的值
float voltage [2];//存储传感器ADC电压
/* 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_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_ADCEx_Calibration_Start(&hadc1);//校准ADC
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_ADC_Start_IT (&hadc1);//启用一次中断读取ADC
HAL_ADCEx_InjectedStart_IT (&hadc1);//启用一次中断读取注入通到ADC
HAL_Delay(500);//每500ms读取一次
voltage[0] = (float)adc_val [0] / 4095 *3.3f;
voltage[1] = (float)adc_val [1] / 4095 *3.3f;
printf("\r\n距离传感器(规则通道IN4):%f,温度传感器(注入通道IN5):%f\r\n",
voltage[0],voltage[1]);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
//
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef*hadc)
{
if(hadc -> Instance == ADC1 )
{
//读取ADC1的转换结果,并保存
adc_val [0] = HAL_ADC_GetValue(hadc);
}
}
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef*hadc)
{
if(hadc -> Instance == ADC1)
{
//读取ADC1的注入通道转换结果,并保存
adc_val [1] = HAL_ADCEx_InjectedGetValue (hadc,ADC_INJECTED_RANK_1);
}
}
int fputc(int ch , FILE *f)
{
HAL_UART_Transmit(&huart1 , (uint8_t*)&ch,1,HAL_MAX_DELAY);
return ch;
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
实验现象如下所示:

更多推荐



所有评论(0)