添加ADC采集电压和RGB灯
This commit is contained in:
43
APP/vcc_adc.c
Normal file
43
APP/vcc_adc.c
Normal file
@ -0,0 +1,43 @@
|
||||
//
|
||||
// Created by hu123456 on 2024/3/8.
|
||||
//
|
||||
|
||||
#include "vcc_adc.h"
|
||||
|
||||
void adc_init(void)
|
||||
{
|
||||
HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
|
||||
}
|
||||
|
||||
uint32_t adc_get_result(void)
|
||||
{
|
||||
ADC_ChannelConfTypeDef adc_ch_conf = {0};
|
||||
|
||||
adc_ch_conf.Channel = ADC_CHANNEL_8;
|
||||
adc_ch_conf.Rank = ADC_REGULAR_RANK_1;
|
||||
adc_ch_conf.SamplingTime = ADC_SAMPLETIME_810CYCLES_5;
|
||||
adc_ch_conf.SingleDiff = ADC_SINGLE_ENDED;
|
||||
adc_ch_conf.OffsetNumber = ADC_OFFSET_NONE;
|
||||
adc_ch_conf.Offset = 0;
|
||||
HAL_ADC_ConfigChannel(&hadc1, &adc_ch_conf);
|
||||
|
||||
HAL_ADC_Start(&hadc1);
|
||||
HAL_ADC_PollForConversion(&hadc1, 10);
|
||||
return HAL_ADC_GetValue(&hadc1);
|
||||
}
|
||||
|
||||
float get_VCC(void)
|
||||
{
|
||||
uint32_t temp_val = 0;
|
||||
float vcc;
|
||||
for (uint8_t t = 0; t < 10; t++)
|
||||
{
|
||||
temp_val += adc_get_result();
|
||||
HAL_Delay(5);
|
||||
}
|
||||
vcc = temp_val/10.f/65535.f * 3.3 * 11.f;
|
||||
return vcc;
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user