I have a development problem, please help me. Due to the power management requirements of the product, I need to enable and disable the ADC function in the program to save power. But the problem I'm having now is that once the ADC goes through enable->disable->enable, it can no longer trigger an interrupt to read the MCU input voltage. In my program, it only fires once to read the ADC value. What did I do wrong?
static nrf_saadc_value_t s_bufferPool[SAMPLES_IN_BUFFER];
uint16_t batteryVoltage;
static void adcCallbackFunc(nrf_drv_saadc_evt_t const *pEvent)
{
if(pEvent->type == NRF_DRV_SAADC_EVT_DONE)
{
nrf_saadc_value_t adcResult = 0;
ret_code_t errCode;
errCode = nrf_drv_saadc_buffer_convert(pEvent->data.done.p_buffer, SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(errCode);
for (uint8_t i = 0; i < SAMPLES_IN_BUFFER; i++)
{
adcResult += pEvent->data.done.p_buffer[i];
}
batteryVoltage = ADC_RESULT_IN_MILLI_VOLTS(adcResult/SAMPLES_IN_BUFFER);
}
}
void ADC_Read(void)
{
ret_code_t errCode;
errCode = nrf_drv_saadc_sample();
APP_ERROR_CHECK(errCode);
}
void ADC_Init(void)
{
ret_code_t errCode;
errCode = nrf_drv_saadc_init(NULL, adcCallbackFunc);
APP_ERROR_CHECK(errCode);
nrf_saadc_channel_config_t channelConfig = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD);
errCode = nrf_drv_saadc_channel_init(0, &channelConfig);
APP_ERROR_CHECK(errCode);
errCode = nrf_drv_saadc_buffer_convert(s_bufferPool, SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(errCode);
}
void EnableADC()
{
ADC_Init();
}
void DisableADC()
{
nrfx_saadc_uninit();
}
int main(void)
{
// Initialize.
ADC_Init();
log_init();
timers_init();
power_management_init();
ble_stack_init();
gap_params_init();
gatt_init();
services_init();
ConfirmAllData();
advertising_init();
conn_params_init();
DisableADC();
advertising_start();
// Enter main loop.
for (;;)
{
EnableADC();
ADC_Read();
DisableADC();
idle_state_handle();
}
}