This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

ADC battery level correct only when debugging

I implemented measuring of Lithium battery level with the help of thread How to measure Lithium battery voltage. I am using nRF51422 with SoftDevice 110 and SDK 9.0.0 and Keil uVision IDE. The problem is that I get right result only when debugging my nRF51 with DevKit. To work correctly, it is enough to just start the debugging and after it waits to be started on main(), continuing execution. Also, I would prefer to read battery level only on startup.

What could be the reason?

My code:

void ADC_init(void)
{
    const nrf_adc_config_t nrf_adc_config = NRF_ADC_CONFIG_DEFAULT;
	
    nrf_adc_configure( (nrf_adc_config_t *)&nrf_adc_config);
    nrf_adc_input_select(NRF_ADC_CONFIG_INPUT_3);
    nrf_adc_int_enable(ADC_INTENSET_END_Enabled << ADC_INTENSET_END_Pos);
    NVIC_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_HIGH);
    NVIC_EnableIRQ(ADC_IRQn);
}

void ADC_IRQHandler(void)
{
	nrf_adc_conversion_event_clean();
	float adc_sample = nrf_adc_result_get(); // wrong when not debugging
    // getting final value here...
}

int main(void)
{
    timers_init();	
    buttons_leds_init(&erase_bonds);
    ble_stack_init();
    ADC_init();
    nrf_adc_start(); // trigger battery level measurement

    // other init, advertising, power_manage()..
}
Related