Hi DevZone
I am trying to measure the battery voltage using the bas service. I got some hints from the ble_app_proximity example.
My problem is that no matter what the output is 100% (And yes it is tested on a CR2032 with 2.8V, so it should be more like 30%ish).
My saadc event hander is:
void saadc_event_handler(nrf_drv_saadc_evt_t const * p_event)
{
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
nrf_saadc_value_t adc_result;
uint16_t batt_lvl_in_milli_volts;
uint8_t percentage_batt_lvl;
uint32_t err_code;
adc_result = p_event->data.done.p_buffer[0];
SEGGER_RTT_printf(0, "adc_result: %d\n", adc_result);
err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, 1);
APP_ERROR_CHECK(err_code);
batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(adc_result) +
DIODE_FWD_VOLT_DROP_MILLIVOLTS;
percentage_batt_lvl = battery_level_in_percent(batt_lvl_in_milli_volts);
err_code = ble_bas_battery_level_update(&m_bas, percentage_batt_lvl);
if (
(err_code != NRF_SUCCESS)
&&
(err_code != NRF_ERROR_INVALID_STATE)
&&
(err_code != BLE_ERROR_NO_TX_PACKETS)
&&
(err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
)
{
APP_ERROR_HANDLER(err_code);
}
}
}
and the adc initialization:
static void adc_configure(void)
{
ret_code_t err_code = nrf_drv_saadc_init(NULL, saadc_event_handler);
APP_ERROR_CHECK(err_code);
nrf_saadc_channel_config_t config =
NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD);
err_code = nrf_drv_saadc_channel_init(0, &config);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_buffer_convert(&adc_buf[0], 1);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_buffer_convert(&adc_buf[1], 1);
APP_ERROR_CHECK(err_code);
}
The result in nRF connect:
Can anybody point me in the right direction? I am a bit lost where my problem is.
Btw. I am using the PCA10056 (NRF52840 PDK) and SDK12.20 :-)
Best Regards Mathias