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

adc_event_handler doesn't work well

NRF51822 most time is in sleep mode. Each 5 seconds it wakes up to make ADC measurements. But adc_event_handler force only once, but I want to see it every 5 seconds.

	err_code = app_timer_create(&PERIODIC_WAKEUP_TIMER_ID, APP_TIMER_MODE_REPEATED, PeriodicWakeUp);
	APP_ERROR_CHECK(err_code);

    void PeriodicWakeUp(void * p_context)
{
    // Trigger conversion on both enabled channels.
    nrf_drv_adc_sample();
	//RedLedBlink(NULL, NULL);
}

PeriodicWakeUp function called each 5 seconds as expected. But adc_event_handler occurs only one time after power on. I see very strange thing for me. If I put breakpoint inside PeriodicWakeUp function adc event always happens. Maybe need some delay after wakeup to start ADC??? I alredy try to insert delay inside PeriodicWakeUp, it has not affect.

void adc_config(void)
{
	ret_code_t ret = nrf_drv_adc_init(NULL, adc_event_handler);
    static nrf_drv_adc_channel_t channel1 = NRF_DRV_ADC_DEFAULT_CHANNEL(NRF_ADC_CONFIG_INPUT_2); //note keyword 'static'
	static nrf_drv_adc_channel_t channel2 =  {{{                                                       \
												.resolution = NRF_ADC_CONFIG_RES_10BIT,                \
												.input      = NRF_ADC_CONFIG_SCALING_INPUT_TWO_THIRDS, \
												.reference  = NRF_ADC_CONFIG_REF_VBG,                  \
												.ain        = (NRF_ADC_CONFIG_INPUT_3)                 \
											}}, NULL};
    static nrf_drv_adc_channel_t channel3 = NRF_DRV_ADC_DEFAULT_CHANNEL(NRF_ADC_CONFIG_INPUT_7); //note keyword 'static'
    nrf_drv_adc_channel_enable(&channel1);
    nrf_drv_adc_channel_enable(&channel2);
	nrf_drv_adc_channel_enable(&channel3);
	
    // Start sampling to buffer.
    ret = nrf_drv_adc_buffer_convert(buffer,3);	
	
	// Trigger conversion on both enabled channels.
    //nrf_drv_adc_sample();
}

void adc_event_handler(nrf_drv_adc_evt_t const * p_event)
{
    if (p_event->type == NRF_DRV_ADC_EVT_DONE)
    {

		
        adc_battery_voltage = buffer[1];
		adc_load_current 	= buffer[2];

		battery_voltage = (adc_battery_voltage * 3600)/1024;
		if(battery_voltage < LOW_BATTERY_THRESHOLD)
			RedLedBlink();
			//RedLedOn();
			//app_sched_event_put(NULL, NULL, RedLedBlink);	// put led blink event to schedule queue
		
		if(adc_load_current > LOAD_CURRENT_SWITCH_ON_THR)
			DCDC_On();
		if(adc_load_current < LOAD_CURRENT_SWITCH_OFF_THR)
			DCDC_Off();
    }    
}
Parents Reply Children
No Data
Related