From 5af7a0b4a29857f29ce623ab7d3789a7b60b989e Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Wed, 1 Jul 2026 13:44:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85ADC=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stm32f103c8t6/Core/Inc/adc_utils.h | 19 +++++++++++++++++++ stm32f103c8t6/Core/Src/adc_utils.c | 26 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 stm32f103c8t6/Core/Inc/adc_utils.h create mode 100644 stm32f103c8t6/Core/Src/adc_utils.c diff --git a/stm32f103c8t6/Core/Inc/adc_utils.h b/stm32f103c8t6/Core/Inc/adc_utils.h new file mode 100644 index 0000000..c46ca03 --- /dev/null +++ b/stm32f103c8t6/Core/Inc/adc_utils.h @@ -0,0 +1,19 @@ +/* + * adc_utils.h + * + * Created on: 2026年7月1日 + * Author: xiaozhou233 + */ + +#ifndef ADC_UTILS_H +#define ADC_UTILS_H + +#include "main.h" + +// Read ADC value +uint16_t ADC_Read(ADC_HandleTypeDef *hadc); + +// Convert ADC value to millivolts +uint32_t ADC_ToMilliVolt(uint16_t adc); + +#endif diff --git a/stm32f103c8t6/Core/Src/adc_utils.c b/stm32f103c8t6/Core/Src/adc_utils.c new file mode 100644 index 0000000..fbad489 --- /dev/null +++ b/stm32f103c8t6/Core/Src/adc_utils.c @@ -0,0 +1,26 @@ +/* + * adc_utils.c + * + * Created on: 2026年7月1日 + * Author: xiaozhou233 + */ + +#include "adc_utils.h" + +uint16_t ADC_Read(ADC_HandleTypeDef *hadc) +{ + HAL_ADC_Start(hadc); + + HAL_ADC_PollForConversion(hadc, HAL_MAX_DELAY); + + uint16_t value = HAL_ADC_GetValue(hadc); + + HAL_ADC_Stop(hadc); + + return value; +} + +uint32_t ADC_ToMilliVolt(uint16_t adc) +{ + return ((uint32_t)adc * 3300 + 2047) / 4095; +}