一、CubeMX配置:
仅需打开Single-ended,取默认配置。
二、代码部分:
创建MyACD.h和MyADC.c文件。
MyADC.h代码:
#ifndef __MYADC_H
#define __MYADC_H
#include "main.h"
#include "adc.h"
float MyADC_GetV();
#endif
MyADC.c代码:
#include "MyADC.h"
float MyADC_GetV(){
float adc = 0;
HAL_ADC_Start(&hadc2);
adc = HAL_ADC_GetValue(&hadc2);
return adc * 3.3 / 4095;
}
在main.c初始化的位置写ADC校准函数:
HAL_ADCEx_Calibration_Start(&hadc2,ADC_SINGLE_ENDED);