STM32F4DoorControl/adc.c

27 lines
678 B
C
Raw Normal View History

2021-02-13 19:39:00 +00:00
#include "adc.h"
static uint32_t bat_voltage_mv;
void adc_setup(void) {
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_ADC1);
gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO3);
adc_power_off(ADC1);
adc_disable_scan_mode(ADC1);
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_480CYC);
adc_set_continuous_conversion_mode(ADC1);
uint8_t channel = 3;
adc_set_regular_sequence(ADC1, 1, &channel);
adc_power_on(ADC1);
adc_start_conversion_regular(ADC1);
}
uint32_t adc_bat_voltage(void) {
if (adc_get_flag(ADC1, ADC_SR_EOC)) {
2021-07-16 19:21:22 +00:00
bat_voltage_mv = adc_read_regular(ADC1) * 3300 / 2048;
2021-02-13 19:39:00 +00:00
}
return bat_voltage_mv;
}