I can measure battery from vbat pin, but when I try to convert it to a percentage, it is not correct.
I use cr2032 do you have an example code about it ?
Please refer to ble_bass.c, which supports functions and message handlers including one pertaining to the battery service server and implements this behavior.
struct bass_db_cfg *db_cfg;
struct gapm_profile_task_add_cmd *req = KE_MSG_ALLOC_DYN(
/* Fill message */
req->operation = GAPM_PROFILE_TASK_ADD;
req->sec_lvl = PERM(SVC_AUTH, DISABLE);
req->prf_task_id = TASK_ID_BASS;
req->app_task = TASK_APP;
req->start_hdl = 0;
/* Set parameters */
db_cfg = (struct bass_db_cfg *)req->param;
db_cfg->bas_nb = 1;
db_cfg->features = BAS_BATT_LVL_NTF_SUP;
db_cfg->batt_level_pres_format.description = 0;
db_cfg->batt_level_pres_format.exponent = 0;
db_cfg->batt_level_pres_format.format = 0x4;
db_cfg->batt_level_pres_format.name_space = 1;
db_cfg->batt_level_pres_format.unit = ATT_UNIT_PERCENTAGE;
/* Send the message */
Actually , I’m trying to ask percentage conversion like at below.
if (adc_in_volts >= 3000)
battLevelPercent = 100;
else if (adc_in_volts > 2900)
battLevelPercent = 100 - ((3000 - adc_in_volts) * 58) / 100;
else if (adc_in_volts > 2740)
battLevelPercent = 42 - ((2900 - adc_in_volts) * 24) / 160;
else if (adc_in_volts > 2440)
battLevelPercent = 18 - ((2740 - adc_in_volts) * 12) / 300;
else if (adc_in_volts > 2100)
battLevelPercent = 6 - ((2440 - adc_in_volts) * 6) / 340;
battLevelPercent = 0;
This is not completely available for CR2032 battery. Do you have example like that ? Because as you know that, CR2032 battery voltage curve is not linear. Therefore values will not be correct directly if there is not any linearization.
We have not done any sort of CR2032 characterization on our end to help customers with this process.
There are many resources online that provide charts from different battery manufacture displaying their curves, so it would be a matter of quantizing this voltage curve with the mAh at various points to capture the % of mAh that have been used up at each voltage level.
Thank you for using our community forum.