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

LPCOMP issue with sensing multiple inputs

I'm using LPCOMP to sense multiple input.

I call the lpcomp_read() function below for each input pin.

If I only call this function for 1 pin only, it worked fine. 

For example:

read_value  |= lpcomp_read(4) << 1; <== this would return read_value = 2, and that is correct.

However, when I call this function multiple times for multiple pins, it only read the 1st one as if the PSEL stuck on the 1st pin. 

read_value |= lpcomp_read(2) << 0;

read_value |= lpcomp_read(4) << 1; 

I expected read_value to be 3 (as both pins should be above reference voltage). Instead, I always get read_value = 1;

Is there any clearing of the LPCOMP if I want to use it again for another pin?

uint32_t lpcomp_read (uint32_t analog_pin)
{
uint32_t lpcomp_result;

NRF_LPCOMP_Type NRT_LPCOMP;


//Set Reference Voltage to VDD *2/8
NRF_LPCOMP->REFSEL |= (LPCOMP_REFSEL_REFSEL_SupplyTwoEighthsPrescaling << LPCOMP_REFSEL_REFSEL_Pos);

NRF_LPCOMP->PSEL = (analog_pin << LPCOMP_PSEL_PSEL_Pos);

//Enable LPCOMP
NRF_LPCOMP->ENABLE = LPCOMP_ENABLE_ENABLE_Enabled;

//start LPCOMP
NRF_LPCOMP->TASKS_START = 1;

while (!NRF_LPCOMP->EVENTS_READY) {}

//Sample Comparator Value
NRF_LPCOMP->TASKS_SAMPLE = 1;

//reset result
lpcomp_result = NRF_LPCOMP->RESULT;

//stop LPCOMP
NRF_LPCOMP->TASKS_STOP = 1;

//Disable LPCOMP
NRF_LPCOMP->ENABLE = LPCOMP_ENABLE_ENABLE_Disabled;

//return result
return lpcomp_result;
}

Related