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

Battery measurement with ADC on nrf51422, S210

I'm trying to use the ADC and internal reference to measure battery voltage on the nrf51422, while S210 is enabled.

From the example nrf51-ble-app-temp-master, I found

#define VBAT_MAX_IN_MV 3300 //3.3v - set this to battery max voltage in mV

uint8_t battery_level_get(void) //Returns battery level as a percentage
{
// Configure ADC
NRF_ADC->CONFIG     = (ADC_CONFIG_RES_8bit                        << ADC_CONFIG_RES_Pos)     |
                      (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos)  |
                      (ADC_CONFIG_REFSEL_VBG                      << ADC_CONFIG_REFSEL_Pos)  |
                      (ADC_CONFIG_PSEL_Disabled                   << ADC_CONFIG_PSEL_Pos)    |
                      (ADC_CONFIG_EXTREFSEL_None                  << ADC_CONFIG_EXTREFSEL_Pos);
NRF_ADC->EVENTS_END = 0;
NRF_ADC->ENABLE     = ADC_ENABLE_ENABLE_Enabled;

NRF_ADC->EVENTS_END  = 0;    // Stop any running conversions.
NRF_ADC->TASKS_START = 1;

while (!NRF_ADC->EVENTS_END)
{
}

uint16_t vbg_in_mv = 1200;
uint8_t adc_max = 255;
uint16_t vbat_current_in_mv = (NRF_ADC->RESULT * 3 * vbg_in_mv) / adc_max;

NRF_ADC->EVENTS_END     = 0;
NRF_ADC->TASKS_STOP     = 1;

return (uint8_t) ((vbat_current_in_mv * 100) / VBAT_MAX_IN_MV);
}

However, when I run this in my code, the debugger shows it stalls in the while (!NRF_ADC->EVENTS_END) loop.

What is the chip doing while this loop runs? Any ideas why its stalling, what I need to fix, or a better way of measuring battery level?

Thanks

Related