1.原理图
2.CUbemx配置
3.代码
(1)dadc.c
#include "dadc.h"
double getADC(ADC_HandleTypeDef *pin)
{
unsigned int value;
//开启ADC
HAL_ADC_Start(pin);
//获取ADC的值
value=HAL_ADC_GetValue(pin);
//12位精度即4096
//4096就代表3.3V
return value*(3.3/4096);
}
(2)dadc.h
#ifndef __DADC_H__
#define __DADC_H__
#include "main.h"
double getADC(ADC_HandleTypeDef *pin);
#endif
(3)main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 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 "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lcd.h"
#include "led.h"
#include "dadc.h"
#<