STM32Cube learning (3) - ADC

0 Preface

MCU: STM32F407ZGT6;
emulator: ST Link V2;
development environment: STM32CubeIDE 1.10.1;
function description: use the ADC inside the MCU to perform the most basic analog-to-digital conversion.

Summary:
STM32Cube Learning (1) - Lighting & Configuring
STM32Cube Learning (2) - Timer Interrupt

Reference materials:
[STM32] HAL library STM32CubeMX tutorial nine - ADC
STM32 ADC detailed article (based on HAL library)
STM32F4 data sheet

0.1, STM32 - Introduction to ADC

insert image description here
insert image description here

1. STM32CubeIDE configuration

Open CubeIDE, create a new project, and select the matching chip

1.1, configure the clock

RCC selects HSE, high-speed clock
insert image description here
configuration clock tree, external crystal oscillator is 8MHz, and PLL, system clock, etc. are set at the same time, and the final frequency is 72MHz
insert image description here

1.2. Configure ADC function

Select ADC2——"IN3
insert image description here
Configure ADC according to the figure below
Independent modeIndependent mode: We only use one ADC, so it is set to independent mode; if multiple ADCs are required to be used synchronously, it will be configured in other modes;
Clock PrescalerPCLK2 divided by2

The clock after ADC frequency division should not be higher than 36MHz, after APB2 frequency division, the APB2 peripheral clock is obtained with
insert image description here
a resolution of 36M samplingResolutionConfigured as the default 12bit;
data alignmentData AlignmentRight justified;
scan conversion modeScan Conversion ModeThis mode is usually enabled when multi-channel ADC sampling;
continuous conversion modeContinuous Conversion ModeThis mode is enabled for continuous conversion, and disabled for single conversion, that is, it needs to trigger conversion again before conversion can be performed;
intermittent modeDiscontinuous Conversion ModeCan not be used when using single-channel sampling.
insert image description here
Rule channel settingsADC_Regular_ConversionMode
Number of conversion channelsNumber Of ConversionAccording to the actual channel configuration, it is currently 1;
external trigger conversion sourceExternal Trigger Conversion Source
regular conversion launched by softwarSoftware rules for conversion.
External trigger transition edgeExternal Trigger Conversion Edgedefault
conversion orderRankDefault: 1 (if there are multiple ADC channels, they can be configured one by one to set the conversion order of each channel)
conversion channelChannelDefault: According to the setting configuration 3
conversion timeSampling TimeDefault: 3 clock cycles
insert image description here

1.2.1. External trigger conversion

In addition to the above trigger methods, there are the following trigger methods, such asTimer 1 capture compare event 1
insert image description here
In addition, after setting other trigger methods, the trigger edge can be set, as shown in the figure below
Trigger detection on the rising edgeTrigger detects rising edge
Trigger detection on the falling edgeTrigger detects falling edge
Trigger detection on the rising edge and falling edgeTrigger detects rising and falling edges
insert image description here

1.2.2, injection mode (injected)

Stay in the pit

1.2.3. Watchdog (WatchDog)

Enables analog watchdog mode. In this mode, an interrupt is entered when it is above the threshold (ADC_HTR) or below the threshold (ADC_LTR).
insert image description here

1.2.4, interrupt after conversion

insert image description here

1.2.5, pin configuration

insert image description here

1.3. Other configurations

insert image description here
Click the =='gear'== icon to generate the code.

2. Code addition

/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* 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 Get_ADC_Value;
/* 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();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    
    
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
      HAL_ADC_Start(&hadc1);     //启动ADC转换
	  HAL_ADC_PollForConversion(&hadc1, 50);   //等待转换完成,50为最大等待时间,单位为ms
	  //判断ADC是否转换成功
	  if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))
	  {
    
    
	   Get_ADC_Value = HAL_ADC_GetValue(&hadc1);   //获取AD值
	  }
	 HAL_Delay(1000);
  }
  /* USER CODE END 3 */
}

Guess you like

Origin blog.csdn.net/u014798590/article/details/126654907