This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Get battery level when BLE is connected

Hi,

I have to check every 10 minutes the battery level when BLE is connected.

I have found in this forum this source : github.com/.../main.c

As the source date is 2014-07-25, can you confirm that it is still the right way to check battery level with nRF51822 softdevice 8 and SDK10 when the chip has a active (potential data transfert) connection with a central?

//ADC initialization
static void adc_init(void)
{	
	/* Enable interrupt on ADC sample ready event*/		
	NRF_ADC->INTENSET = ADC_INTENSET_END_Msk;   
	sd_nvic_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_LOW);  
	sd_nvic_EnableIRQ(ADC_IRQn);
	
	NRF_ADC->CONFIG	= (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos) /* Bits 17..16 : ADC external reference pin selection. */
									| (ADC_CONFIG_PSEL_AnalogInput2 << ADC_CONFIG_PSEL_Pos)					/*!< Use analog input 2 as analog input. */
									| (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos)							/*!< Use internal 1.2V bandgap voltage as reference for conversion. */
									| (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) /*!< Analog input specified by PSEL with no prescaling used as input for the conversion. */
									| (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos);									/*!< 8bit ADC resolution. */ 
	
	/* Enable ADC*/
	NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
}

// ADC timer handler to start ADC sampling
static void adc_sampling_timeout_handler(void * p_context)
{
	uint32_t p_is_running = 0;
		
	sd_clock_hfclk_request();
	while(! p_is_running) {  							//wait for the hfclk to be available
		sd_clock_hfclk_is_running((&p_is_running));
	}               
	nrf_gpio_pin_toggle(LED_2);		//Toggle LED2 to indicate start of sampling
	NRF_ADC->TASKS_START = 1;							//Start ADC sampling
}

/* Interrupt handler for ADC data ready event */
void ADC_IRQHandler(void)
{
	/* Clear dataready event */
  NRF_ADC->EVENTS_END = 0;	

  /* Write ADC result to port 2 */
 	nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT2, NRF_ADC->RESULT); 
	nrf_gpio_pin_toggle(LED_3);
	
	//Use the STOP task to save current. Workaround for PAN_028 rev1.5 anomaly 1.
  NRF_ADC->TASKS_STOP = 1;
	
	//Release the external crystal
	sd_clock_hfclk_release();
}

Thanks

Parents
  • The code should work as it is using the registers directly and not a SDK driver. To measure VDD (which is often connected straight to the battery) you can change the INPSEL to SupplyOneThirdPrescaling (and PSEL can be disgarded). This will make the ADC able to measure up to 3.6V, see here.

    NRF_ADC->CONFIG = (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos)
                    | (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos)
                    | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos)
                    | (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos)
                    | (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos);
    

    Starting the HFCLK is not absolutely necessary. It will add some accuracy, but will also add a delay in your code equal to the startup time of the crystal, so the current consumption will be a little more.

  • Depending on the battery the voltage may drop a lot when the current goes up due to internal resistance in the battery. If the sample happens while the radio is on the measurements may be different than if the sample happens when the radio is off. Here is another post regarding this (voltage drops due to CPU is on, ~5mA). You can log the voltage with an oscilloscope to see if this is the case. To measure while the radio is inactive you can use timeslot, see this tutorial on how to use it.

Reply
  • Depending on the battery the voltage may drop a lot when the current goes up due to internal resistance in the battery. If the sample happens while the radio is on the measurements may be different than if the sample happens when the radio is off. Here is another post regarding this (voltage drops due to CPU is on, ~5mA). You can log the voltage with an oscilloscope to see if this is the case. To measure while the radio is inactive you can use timeslot, see this tutorial on how to use it.

Children
No Data
Related