STM32CubeIDE(ADC)

Learning link:[HAL library detailed explanation] STM32 ADC HAL library usage_bilibili_bilibili

Table of contents

1. Concept

1. Introduction to ADC

2. Main features of ADC

2. ADC mode

1. Query mode

1.1 Single-channel acquisition software configuration

1.2 Query mode steps

1.3 Single channel acquisition main code

Three, Tatsudo Kashu

2.1 Software configuration

2.2 Main code

1. Concept

1. Introduction to ADC

12-bit ADC is a successive approximation analog-to-digital converter. It has up to 18 channels and can measure 16 external and 2 internal signal sources. A/D conversion of each channel can be performed in single, continuous, scan or discontinuous modes. The results of the ADC can be stored in a 16-bit data register either left-aligned or right-aligned.

2. Main features of ADC

12-bit resolution
● Interrupts at end of conversion, end of injected conversion and analog watchdog events
● Single and continuous conversions Mode
● Auto scan mode from channel 0 to channel n
● Self-calibration
● With embedded data consistency Data alignment
● Sampling intervals can be programmed individually per channel
● External triggering options for both rule conversion and injection conversion
● Discontinuity Mode
● Dual mode (devices with 2 or more ADCs)
● ADC conversion time:
─ STM32F103xx enhanced Product: 1μs when the clock is 56MHz (1.17μs when the clock is 72MHz)
● ADC power supply requirement: 2.4V to 3.6V
● ADC input range: VREF - ≤ VIN ≤ VREF+
● A DMA request is generated during regular channel conversion.

2. ADC mode

1. Query (blocking) mode

1.1 Single-channel acquisition software configuration

1.1.1 Just select the interface

1.1.2 Open the serial port for printing data

1.2 Query mode steps

1.3 Main functions of query mode

①HAL_ADC_Start(&hadc1);

Start ADC and ADC conversion

②HAL_ADC_Stop(&hadc1);

Turn off ADC

③HAL_ADC_PollForConversion(&hadc1, 100);//Blocking to determine whether the conversion is completed

Polling to check whether the ADC conversion is completed, a status will be returned when the conversion is completed

④HAL_ADC_PollForEvent();

Wait for the specified ADC event to occur and return a status, which can be used to wait for a specific event to occur and then execute related code.

//ADC_PollForEvent`函数用于轮询检查ADC事件是否发生。
// 假设使用了ADC1进行操作
ADC_HandleTypeDef hadc1; // 声明一个 ADC_HandleTypeDef 结构体变量
// 在适当的地方对ADC1进行初始化配置
// 等待ADC转换完成的函数
void wait_for_adc_event() {
    // 在这里进行ADC转换启动等操作(例如设置通道、采样时间等)
    // 轮询检查特定的ADC事件,例如转换完成事件
    while (HAL_ADC_PollForEvent(&hadc1, ADC_EOC_SINGLE_CONV) != HAL_OK) {
        // 可以添加一些等待的代码,或者执行其他操作
    }
    // 如果到达这里,表示已经检测到了特定的ADC事件(例如转换完成事件)
    // 可以继续执行其他代码,例如获取ADC转换值等
}
//使用ADC_PollForEvent函数等待特定的 ADC 事件(单次转换完成事件 ADC_EOC_SINGLE_CONV)
//然后在事件发生后执行相应的操作。在实际应用中,根据需求可能需要进行相应的配置和处理。

1.4 Single channel acquisition main code

#include <stdio.h>
#include <string.h>

//ADC采集值获取
int16_t get_adc_value(void)
{
	HAL_ADC_Start(&hadc1);
	HAL_ADC_PollForConversion(&hadc1, 100);//阻塞判断是否转换完成
	if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1),HAL_ADC_STATE_REG_EOC))
	{
		return HAL_ADC_GetValue(&hadc1);
	}
	return -1;
}


//main函数主循环代码如下
/* USER CODE BEGIN WHILE */
 int16_t value=0;
 char buff[30];
 while (1)
 {
  value=get_adc_value();
  if(value==-1)
  {
	  HAL_UART_Transmit(&huart2, (uint8_t*)"GetADCValue Faild !! \r\n", 25, 50);
  }
  else
  {
	  sprintf(buff, "value:%d \r\n",value);
	  HAL_UART_Transmit(&huart2, (uint8_t*)buff, strlen(buff), 50);
  }
  HAL_Delay(1000);
   /* USER CODE END WHILE */

   /* USER CODE BEGIN 3 */
 }

1.5 Multi-channel acquisition

2. Interrupt mode

3. DMA mode

3. Multi-channel acquisition

2.1 Software configuration

2.2 Main code

  while (1)
  {
	  get_adc_value(DMARes);

	  sprintf(buff, "value:%d,%d,%d \r\n",DMARes[0],DMARes[1],DMARes[2]);
	  HAL_UART_Transmit(&huart2, (uint8_t*)buff, strlen(buff), 50);
	  HAL_Delay(500);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }



void get_adc_value(int* DMARes)
{
	HAL_ADC_Start(&hadc1);

	HAL_ADC_PollForConversion(&hadc1, 100);//阻塞判断是否转换完成
	DMARes[0]= HAL_ADC_GetValue(&hadc1);

	HAL_ADC_PollForConversion(&hadc1, 100);//阻塞判断是否转换完成
	DMARes[1]= HAL_ADC_GetValue(&hadc1);

	HAL_ADC_PollForConversion(&hadc1, 100);//阻塞判断是否转换完成
	DMARes[2]= HAL_ADC_GetValue(&hadc1);

	HAL_ADC_Stop(&hadc1);
}

Guess you like

Origin blog.csdn.net/qq_57594025/article/details/134166384
Recommended