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

LPCOMP sampling rate

Hi,

I want to sample lpcomp result at 50us, I'm doing so by using the following statements and set/clear a gpio based on the lpcomp result:

nrf_lpcomp_task_trigger(NRF_LPCOMP_TASK_SAMPLE); lpcomp_result = nrf_lpcomp_result_get();

This is done in 50us TIMER interrupt.

LPCOMP configuration : My LPCOMP configuration

We observe some differences in the gpio waveform when an external comparator chip (LM2901N) is used instead of the internal LPCOMP. The observed waveforms are attached below.(some samples seem to be skipped while using the LPCOMP)

waveform using LPCOMP: image description waveform using external comparator chip: image description

Can we sample the LPCOMP at 50us rate? What are the limitations of LPCOMP? Is there a specific way to achieve this?

  • Hi,

    • How are you initializing the lpcomp?
    • What IRQ priority are you using on the Timer ?
    • What kind of signal are you sampling? How often does it change ?
    • Could you post your complete code on how you are doing this?

    From the spec you have that:

    The immediate value of the LPCOMP can be sampled to RESULT by triggering the SAMPLE task.

    So 50us rate should be fine.

    1. Lpcomp initialisation:

      void md_lpcomp_init(void) { uint32_t err_code;

      nrf_drv_lpcomp_config_t lpcomp_config = NRF_DRV_LPCOMP_DEFAULT_CONFIG;
      lpcomp_config.input = NRF_LPCOMP_INPUT_4;
      
      // 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(&lpcomp_config, lpcomp_event_handler);
      APP_ERROR_CHECK(err_code);
      nrf_drv_lpcomp_enable();
      

      }

    I have commented the nrf_drv_common_irq_enable(LPCOMP_IRQn, p_config->interrupt_priority) in nrf_lpcomp_init function

    1. Timer instance 0 is used with IRQ priority 7.

    2. We are sampling a PIR sensor signal via software implemented sigma delta ADC.Based on this Application Note

      static __inline void md_sample_measurement(void)
     { 
         /*Get the last compare result. If 0 then VIN+ < VIN-, if 1 then the opposite. */
         nrf_lpcomp_task_trigger(NRF_LPCOMP_TASK_SAMPLE);
         lpcomp_result = nrf_lpcomp_result_get();
    
         if (0 == lpcomp_result)
         {
           /* lpcomp result is zero i.e. reference higher than input so need more vtg. in*/	 
           NRF_GPIO->OUTSET = (1 << PIR_CHARGE);
           lpcomp_result_count++;
         }
         else
         {
           /* lpcomp result is non zero i.e. input higher than reference so need less vtg.*/
          NRF_GPIO->OUTCLR = (1 << PIR_CHARGE);
          lpcomp_result_count--;		 			 
         }
    
       }
       
     } 
    

    This is the function called in the timer interrupt.

  • Are you using the SoftDevice/BLE at the same time? Does it make any difference if you increase the IRQ priority ?

    Here some code I used to test this, and it worked fine. I'm here sampling at 20 us. Signal input at pin 4, and lpcomp result output at pin 28. I used reference voltage Supply 4/8 when I tested this.


    Is there a reason why you need to sample every 50us? Note that you can get an interrupt every time the signal crosses your voltage reference. That way you don’t need to pool/sample the result every 50 us... Also if the signal changes faster than you are pooling, you will lose data. So using the LPCOMP interrupt is generally preferred over sampling in a timer interrupt.

    1. Currently we are not using a softdevice, but the final solution will have ble. 2.How was it verified that the LPCOMP worked fine?
    2. What was the signal input to the LPCOMP.
    3. We are taking over the code from an already working product where all these time parameters are tuned to detect motions accurately from the pir. The only thing that has changed from the previous circuitry is the comparator. Earlier we used comparator from the stm family MCU, which is now replaced with the nrf52 LPCOMP.
  • I tested this with a PWM input signal. Note that according to basic sampling theorems, you need a minimum sampling frequency that is 2 times higher than the maximum frequency of the input signal.

    Have you tried increasing the sampling rate?

    Again, you don't need to use a TIMER for this. The LPCOMP does automatically detection when the signal goes below/over your reference ( LPCOMP can generate separate events on rising and falling edges of a signal), so you don't need to periodically sample the state from a timer interrupt, the LPCOMP can generate the events itself when the input signal changes.

Related