Hello, I'm from the United States.
I am currently working on a development project using nrf52840. In this project, I am implementing a battery voltage monitoring function, but the value of the output voltage hardly changes even if I keep using power. Specifically, we are implementing three functions related to saadc as shown in the following program: the first function uploads the voltage value as a percentage when an event occurs, the second function completes the initialization, and the third function is implemented in the main function as The second function completes the initialization, and the third function outputs the voltage value by looping through the while statement in the main function.
Since I am a beginner in developing with the nrf52840, I may not have enough prior knowledge in some areas. Thank you very much for your help. Thank you in advance.
static void saadc_event_handler(nrf_drv_saadc_evt_t const * p_evt)
{
ret_code_t err_code;
if (p_evt->type == NRF_DRV_SAADC_EVT_DONE)
{
nrf_saadc_value_t adc_result;
adc_result = p_evt->data.done.p_buffer[0];
m_batt_lvl_in_milli_volts =
ADC_RESULT_IN_MILLI_VOLTS(adc_result) + DIODE_FWD_VOLT_DROP_MILLIVOLTS;
percentage_batt_lvl=battery_level_in_percent(m_batt_lvl_in_milli_volts);
NRF_LOG_INFO("Battery in %03d%%",percentage_batt_lvl);
err_code=ble_bas_battery_level_update(&m_bas,percentage_batt_lvl,BLE_CONN_HANDLE_ALL);
if((err_code !=NRF_SUCCESS)&&
(err_code !=NRF_ERROR_INVALID_STATE)&&
(err_code !=NRF_ERROR_RESOURCES)&&
(err_code !=NRF_ERROR_BUSY)&&
(err_code !=BLE_ERROR_GATTS_SYS_ATTR_MISSING)
)
{
APP_ERROR_HANDLER(err_code);
}
nrf_drv_saadc_uninit();
}
}
void es_battery_voltage_init(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, 1);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_sample();
APP_ERROR_CHECK(err_code);
}
void es_battery_voltage_get(uint16_t * p_vbatt)
{
VERIFY_PARAM_NOT_NULL_VOID(p_vbatt);
*p_vbatt = m_batt_lvl_in_milli_volts;
if (!nrf_drv_saadc_is_busy())
{
ret_code_t err_code = nrf_drv_saadc_buffer_convert(&adc_buf, 1);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_sample();
APP_ERROR_CHECK(err_code);
}
}int main(void)
{
initialize();
start();
nrf_gpio_pin_clear(POWER);
nrf_gpio_pin_clear(ONOFF_SERVER_R_LED);
nrf_gpio_pin_clear(ONOFF_SERVER_G_LED);
nrf_gpio_pin_clear(ONOFF_SERVER_B_LED);
nrf_gpio_cfg_input(DIPSW_0,NRF_GPIO_PIN_PULLUP);
nrf_gpio_cfg_input(DIPSW_1,NRF_GPIO_PIN_PULLUP);
nrf_gpio_cfg_input(DIPSW_2,NRF_GPIO_PIN_PULLUP);
nrf_gpio_cfg_input(DIPSW_3,NRF_GPIO_PIN_PULLUP);
nrf_gpio_cfg_output(BUZZER);
int sw_1 = (nrf_gpio_pin_read(DIPSW_0) == 0)*1;
int sw_2 = (nrf_gpio_pin_read(DIPSW_1) == 0)*2;
int node_number = 1 + sw_1 + sw_2;
es_battery_voltage_init();
uint16_t vbatt;
uint16_t battVolt;
uint16_t battVolt_1;
int battVolts[5];
int battVolts_new[3];
int i=0;
int tmp;
while (true)
{
//counter=0;
(void)sd_app_evt_wait();
es_battery_voltage_get(&vbatt);
battVolt=(uint16_t)vbatt;
battVolt_1=battVolt;
battVolts[i]=battVolt_1;
//__LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "battery_voltage=%d",battVolt_1);
if(i<3)
{
i++;
nrf_delay_ms(1);
}
else
{
i=0;
for (int k=0; k<5; ++k)
{
for (int j=k+1; j<5; ++j)
{
if (battVolts[k] > battVolts[j])
{
tmp = battVolts[k];
battVolts[k] = battVolts[j];
battVolts[j] = tmp;
}
}
}
__LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "battery_voltage=,%d\n",(battVolts[1]+battVolts[2]+battVolts[3])/3);
if((battVolts[1]+battVolts[2]+battVolts[3])/3<3300)
{
//indicate_battery_voltage();
}
//nrf_delay_ms(5000);
}
}
}