Increase ADC precision

This commit is contained in:
Riku Isokoski 2021-07-27 22:41:48 +03:00
parent a0a05553cf
commit 79ee65bfd5

View File

@ -39,7 +39,7 @@ void Battery::SaadcInit() {
nrf_saadc_channel_config_t adcChannelConfig = {.resistor_p = NRF_SAADC_RESISTOR_DISABLED, nrf_saadc_channel_config_t adcChannelConfig = {.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
.resistor_n = NRF_SAADC_RESISTOR_DISABLED, .resistor_n = NRF_SAADC_RESISTOR_DISABLED,
.gain = NRF_SAADC_GAIN1_5, .gain = NRF_SAADC_GAIN1_4,
.reference = NRF_SAADC_REFERENCE_INTERNAL, .reference = NRF_SAADC_REFERENCE_INTERNAL,
.acq_time = NRF_SAADC_ACQTIME_40US, .acq_time = NRF_SAADC_ACQTIME_40US,
.mode = NRF_SAADC_MODE_SINGLE_ENDED, .mode = NRF_SAADC_MODE_SINGLE_ENDED,
@ -59,11 +59,11 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
APP_ERROR_CHECK(nrfx_saadc_buffer_convert(&saadc_value, 1)); APP_ERROR_CHECK(nrfx_saadc_buffer_convert(&saadc_value, 1));
// A hardware voltage divider divides the battery voltage by 2 // A hardware voltage divider divides the battery voltage by 2
// ADC gain is 1/5 // ADC gain is 1/4
// thus adc_voltage = battery_voltage / 2 * gain = battery_voltage / 10 // thus adc_voltage = battery_voltage / 2 * gain = battery_voltage / 8
// reference_voltage is 0.6V // reference_voltage is 0.6V
// p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024 // p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024
voltage = p_event->data.done.p_buffer[0] * 6000 / 1024; voltage = p_event->data.done.p_buffer[0] * (0.6 * 8 * 1000) / 1024;
if (voltage > battery_max) { if (voltage > battery_max) {
percentRemaining = 100; percentRemaining = 100;