2023-01-19 23:45:31 -05:00
|
|
|
#include "driver/adc.h"
|
|
|
|
#include "esp_adc_cal.h"
|
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// static const char *TAG = "adc.c";
|
2023-01-19 23:45:31 -05:00
|
|
|
static esp_adc_cal_characteristics_t adc1_chars;
|
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// Sets up the ADC to read Vcore. Run this before ADC_get_vcore()
|
|
|
|
void ADC_init(void)
|
|
|
|
{
|
2023-01-19 23:45:31 -05:00
|
|
|
adc1_config_channel_atten(ADC1_CHANNEL_1, ADC_ATTEN_DB_11);
|
|
|
|
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_DEFAULT, 0, &adc1_chars);
|
|
|
|
}
|
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// returns the ADC voltage in mV
|
|
|
|
uint16_t ADC_get_vcore(void)
|
|
|
|
{
|
2023-01-19 23:45:31 -05:00
|
|
|
adc1_config_width(ADC_WIDTH_BIT_DEFAULT);
|
|
|
|
return esp_adc_cal_raw_to_voltage(adc1_get_raw(ADC1_CHANNEL_1), &adc1_chars);
|
|
|
|
}
|