STM32F4DoorControl/adc.c

27 lines
664 B
C

#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)) {
bat_voltage_mv = adc_read_regular(ADC1);
}
return bat_voltage_mv;
}