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

Battery percentage level

Hello,

I am using a Nrf52382 custom board and want to know the Vbattery so I used below code

#define MIN_BATTERY_ADC_VALUE (int16_t)11295    //(min_voltage_on_analog_pin *4551) 2.48*4551 = 11295
#define MAX_BATTERY_ADC_VALUE (int16_t)15391    //(max_voltage_on_analog_pin *4551) 3.38*4551 = 15391
float Battery_status(void){

int16_t result = 0;
float precise_result = 0;


// Configure SAADC singled-ended channel, Internal reference (0.6V) and 1/6 gain.
NRF_SAADC->CH[0].CONFIG = (SAADC_CH_CONFIG_GAIN_Gain1_6    << SAADC_CH_CONFIG_GAIN_Pos) |
		(SAADC_CH_CONFIG_MODE_SE         << SAADC_CH_CONFIG_MODE_Pos) |
		(SAADC_CH_CONFIG_REFSEL_Internal << SAADC_CH_CONFIG_REFSEL_Pos) |
		(SAADC_CH_CONFIG_RESN_Bypass     << SAADC_CH_CONFIG_RESN_Pos) |
		(SAADC_CH_CONFIG_RESP_Bypass     << SAADC_CH_CONFIG_RESP_Pos) |
		(SAADC_CH_CONFIG_TACQ_3us        << SAADC_CH_CONFIG_TACQ_Pos);


// Configure the SAADC channel with VDD as a positive input, no negative input(single ended).
NRF_SAADC->CH[0].PSELP = SAADC_CH_PSELN_PSELN_AnalogInput7 << SAADC_CH_PSELN_PSELN_Pos ;//changed pin number to AIN7
NRF_SAADC->CH[0].PSELN = SAADC_CH_PSELN_PSELN_NC << SAADC_CH_PSELN_PSELN_Pos;

//	  NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);

// Configure the SAADC resolution.
NRF_SAADC->RESOLUTION = SAADC_RESOLUTION_VAL_14bit << SAADC_RESOLUTION_VAL_Pos;

// Configure result to be put in RAM at the location of "result" variable.
NRF_SAADC->RESULT.MAXCNT = 1;
NRF_SAADC->RESULT.PTR = (uint32_t)&result;

// No automatic sampling, will trigger with TASKS_SAMPLE.
NRF_SAADC->SAMPLERATE = SAADC_SAMPLERATE_MODE_Task << SAADC_SAMPLERATE_MODE_Pos;

// Enable SAADC (would capture analog pins if they were used in CH[0].PSELP)
NRF_SAADC->ENABLE = SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos;

// Calibrate the SAADC (only needs to be done once in a while)
NRF_SAADC->TASKS_CALIBRATEOFFSET = 1;
while (NRF_SAADC->EVENTS_CALIBRATEDONE == 0);
NRF_SAADC->EVENTS_CALIBRATEDONE = 0;
while (NRF_SAADC->STATUS == (SAADC_STATUS_STATUS_Busy <<SAADC_STATUS_STATUS_Pos));

// Start the SAADC and wait for the started event.
NRF_SAADC->TASKS_START = 1;
while (NRF_SAADC->EVENTS_STARTED == 0);
NRF_SAADC->EVENTS_STARTED = 0;

// Do a SAADC sample, will put the result in the configured RAM buffer.
NRF_SAADC->TASKS_SAMPLE = 1;
while (NRF_SAADC->EVENTS_END == 0);
NRF_SAADC->EVENTS_END = 0;

precise_result = (float)(result/4551);

// Stop the SAADC, since it's not used anymore.
NRF_SAADC->TASKS_STOP = 1;
while (NRF_SAADC->EVENTS_STOPPED == 0);
NRF_SAADC->EVENTS_STOPPED = 0;
NVIC_ClearPendingIRQ(SAADC_IRQn);
return precise_result;

When I use analog VDD pin I am receiving proper result to be 2.99v but when other analog pins are used result is coming to be different to what it should be. I am linking battery connection schematics with this question. I am using 3.7v 27mah battery it's working range is 4.2 - 3.0

image description

according to my reading of voltage division 4.2*(2220/(2220+330)) = 3.38 max on analog pin 3.0*(2220/(2220+330)) = 2.60min on analog pin

Ques=>are this calculation correct??

if correct then why the analog result is less than 2.60 on other analog pins. These calculation are seen when the battery is connected and not in debug mode. I am asking for Battery status using

Related