封装ADC工具

This commit is contained in:
xiaozhou233 committed 2026-07-01 13:44:10 +08:00
1 parent 6082aa1ec7
commit 5af7a0b4a2
2 files changed
+45

No files matched your search

+19
View File
@@ -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
+26
View File
@@ -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;
}