This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

LOCOMP function failed when reference level being changed periodically

Dear all,

I ran LPCOMP example from SDK16 on nRF52 DK and it works just fine.

But I've occurred some problem modifying reference voltage. I init the lpcomp the same way as SDK shows, then I manually triggerred a SAMPLE TASK to get the compare RESULT. I need to distinguish the input voltage into three levels so I need to compare the analog input with two different reference. So I do nrf_drv_lpcomp_uninit() and run nrf_drv_lpcomp_init() with another refence voltage. But the comparator result will remain 0 no matter what input is appllied on input pin.

If I initialize the lpcomp with fixed refence voltage and not doing uninit and init during system running, the comparator works well. So I assume there must be something wrong with the uninit part?

Code is shown as follows and your help is appreciated!

static void batt_level_deinit()
{
	nrf_drv_lpcomp_uninit();
}


static void lpcomp_event_handler(nrf_lpcomp_event_t event)
{
	
}


static void level_set(nrf_lpcomp_ref_t ref, nrf_lpcomp_detect_t det)	//NRF_LPCOMP_REF_SUPPLY_4_8  NRF_LPCOMP_DETECT_UP
{
    uint32_t	err_code;

    nrf_drv_lpcomp_config_t config = NRF_DRV_LPCOMP_DEFAULT_CONFIG;
    config.input = NRF_LPCOMP_INPUT_0;
		config.hal.hyst = NRF_LPCOMP_HYST_50mV;
		config.hal.reference = ref;
		config.hal.detection = det;

    // initialize LPCOMP driver, from this point LPCOMP will be active and provided
    // event handler will be executed when defined action is detected
    err_code = nrf_drv_lpcomp_init(&config, lpcomp_event_handler);
    APP_ERROR_CHECK(err_code);
		nrf_drv_lpcomp_enable();
}


static uint8_t get_comp_result()
{
	NRF_LPCOMP->TASKS_SAMPLE=1;
	while(!nrf_lpcomp_event_check(NRF_LPCOMP_EVENT_READY));
	
	return NRF_LPCOMP->RESULT;
}


static uint8_t batt_level_ergodic()
{
	level_set(NRF_LPCOMP_REF_SUPPLY_13_16, NRF_LPCOMP_DETECT_DOWN);
	

	if(get_comp_result())
	{
		batt_level_deinit();
		return 3;
	}
	
//	batt_level_deinit();
//	level_set(NRF_LPCOMP_REF_SUPPLY_9_16, NRF_LPCOMP_DETECT_DOWN);
//	if(get_comp_result())
//	{
//		batt_level_deinit();
//		return 2;
//	}

//	batt_level_deinit();
//	level_set(NRF_LPCOMP_REF_SUPPLY_3_16, NRF_LPCOMP_DETECT_DOWN);
//	if(get_comp_result())
//	{
//		batt_level_deinit();
//		return 1;
//	}
	batt_level_deinit();
	return 0;
}


/****************** MAIN *********************/
int main()
{
	uint32_t result;
	
	NRF_LOG_INIT(NULL);
	NRF_LOG_DEFAULT_BACKENDS_INIT();
	NRF_LOG_INFO("start!\r\n");

	while(1)
	{
		result = batt_level_ergodic();
		NRF_LOG_INFO("%d\r\n", result);

		nrf_delay_ms(1000);
		NRF_LOG_FLUSH();

	}
}

Parents
  • Thanks Jared,

    Your code worked.

    Meanwhile I figured out the problem of my code: "nrf_lpcomp_event_check(NRF_LPCOMP_EVENT_READY)" is not actually working, I still need to insert delay manually everytime I re-init the LPCOMP before sampling.

    Thanks!

Reply
  • Thanks Jared,

    Your code worked.

    Meanwhile I figured out the problem of my code: "nrf_lpcomp_event_check(NRF_LPCOMP_EVENT_READY)" is not actually working, I still need to insert delay manually everytime I re-init the LPCOMP before sampling.

    Thanks!

Children
No Data
Related