<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>LOCOMP function failed when reference level being changed periodically</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/84811/locomp-function-failed-when-reference-level-being-changed-periodically</link><description>Dear all, 
 
 I ran LPCOMP example from SDK16 on nRF52 DK and it works just fine. 
 
 But I&amp;#39;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</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 18 Feb 2022 11:05:00 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/84811/locomp-function-failed-when-reference-level-being-changed-periodically" /><item><title>RE: LOCOMP function failed when reference level being changed periodically</title><link>https://devzone.nordicsemi.com/thread/353812?ContentTypeID=1</link><pubDate>Fri, 18 Feb 2022 11:05:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a7c7d463-a94b-4bc0-a7cf-2bb59eb3afda</guid><dc:creator>Avadacadabara</dc:creator><description>&lt;p&gt;Thanks Jared,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Your code worked.&lt;/p&gt;
&lt;p&gt;Meanwhile I figured out the problem of my code: &amp;quot;nrf_lpcomp_event_check(NRF_LPCOMP_EVENT_READY)&amp;quot; is not actually working, I still need to insert delay manually everytime I re-init the LPCOMP before sampling.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LOCOMP function failed when reference level being changed periodically</title><link>https://devzone.nordicsemi.com/thread/353637?ContentTypeID=1</link><pubDate>Thu, 17 Feb 2022 13:54:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:56f0b0cc-d3ff-45e0-acee-87f99739e4e1</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I wasn&amp;#39;t able to reproduce this on my side, but I tried modifying the lp-comp&amp;nbsp;example by adding your&amp;nbsp;level_set() and see if it was able to change the reference voltage, which it did. Here is the code I used:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#define WAVE_ON_PIN_NUMBER 2

static volatile uint32_t voltage_falls_detected = 0;
static volatile uint32_t voltage_falls_total    = 0;

/**
 * @brief LPCOMP event handler is called when LPCOMP detects voltage drop.
 *
 * This function is called from interrupt context so it is very important
 * to return quickly. Don&amp;#39;t put busy loops or any other CPU intensive actions here.
 * It is also not allowed to call soft device functions from it (if LPCOMP IRQ
 * priority is set to APP_IRQ_PRIORITY_HIGH).
 */
static void lpcomp_event_handler(nrf_lpcomp_event_t event)
{
    if (event == NRF_LPCOMP_EVENT_DOWN)
    {
        bsp_board_led_invert(BSP_BOARD_LED_0); // just change state of first LED
        voltage_falls_detected++;
        voltage_falls_total++;
    }
}


/**
 * @brief Print out detection statistics.
 */
static void print_statistics(void)
{
    while (voltage_falls_detected)
    {
        voltage_falls_detected--;
        NRF_LOG_INFO(&amp;quot;#%d fall detected&amp;quot;, (int)voltage_falls_total);
    }
}


/**
 * @brief Initialize LPCOMP driver.
 */
static void lpcomp_init(void)
{
    uint32_t                err_code;

    nrf_drv_lpcomp_config_t config = NRF_DRV_LPCOMP_DEFAULT_CONFIG;
    config.input = NRF_LPCOMP_INPUT_2;
    // 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(&amp;amp;config, lpcomp_event_handler);
    APP_ERROR_CHECK(err_code);
    nrf_drv_lpcomp_enable();
}




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_2;
		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(&amp;amp;config, lpcomp_event_handler);
    APP_ERROR_CHECK(err_code);
		nrf_drv_lpcomp_enable();
}

int main(void)
{
    bsp_board_init(BSP_INIT_LEDS);

    nrf_gpio_cfg_output(WAVE_ON_PIN_NUMBER); // on this pin 2Hz wave will be generated

#ifdef BSP_BUTTON_0
    // configure pull-up on first button
    nrf_gpio_cfg_input(BSP_BUTTON_0, NRF_GPIO_PIN_PULLUP);
#endif

    uint32_t err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_DEFAULT_BACKENDS_INIT();

    lpcomp_init();

    NRF_LOG_INFO(&amp;quot;LPCOMP driver usage example started.&amp;quot;);

    int i = 0;

    while (true)
    {

      if(i == 10)
        { 
          nrf_drv_lpcomp_uninit();
          level_set(NRF_LPCOMP_REF_SUPPLY_13_16, NRF_LPCOMP_DETECT_DOWN);
          NRF_LOG_INFO(&amp;quot;New reference voltage set to 13_16&amp;quot;);
        
        }
        
        if(i == 20)
        { 
          i = 0;
          nrf_drv_lpcomp_uninit();
          level_set(NRF_LPCOMP_REF_SUPPLY_9_16, NRF_LPCOMP_DETECT_DOWN);
          NRF_LOG_INFO(&amp;quot;New reference voltage set to 9_16&amp;quot;);
        
        }

        i++;
        print_statistics();
        bsp_board_led_on(BSP_BOARD_LED_1);
        NRF_GPIO-&amp;gt;OUTCLR = (1 &amp;lt;&amp;lt; WAVE_ON_PIN_NUMBER);
        nrf_delay_ms(100); // generate 100 ms pulse on selected pin
        print_statistics();
        bsp_board_led_off(BSP_BOARD_LED_1);
        NRF_GPIO-&amp;gt;OUTSET = (1 &amp;lt;&amp;lt; WAVE_ON_PIN_NUMBER);
        nrf_delay_ms(400);
        NRF_LOG_FLUSH();
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;regards&lt;/p&gt;
&lt;p&gt;Jared&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>