52832 COMP — Comparator

hi, I want to use 52832 Comparator function, SDK only has "low power comparator" demo code. Setting reference voltage in MCU, just compare one GPIO with this voltage.

But I want to use "differential mode"(set two GPIOs as "AN+", “AN-”, compare two GPIO's voltage), "high speed" mode, could you provide some demo code for my requirement? I don't know how to write code for setting my requirement.

thanks 

  • Hi,

    What SDK are you using and what version of the SDK?

    Regards,
    Jonathan

  • And I used your "lp-comp" demo code, connected pin 0.02 to GND, can't into interrupt, could you help me to check where I should change it? I want to print"LPCOMP_EVENT_UP" when pin 0.02 connected VCC,  print"LPCOMP_EVENT_DOWN" when pin 0.02 connected GND.

    thanks  

    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++;
    				printf("\r\nLPCOMP_EVENT_DOWN\r\n");
        }
    	  else if (event == NRF_LPCOMP_EVENT_UP)
        {
    				printf("\r\nLPCOMP_EVENT_UP\r\n");
        }
    		bsp_board_led_off(BSP_BOARD_LED_0);
    		printf("\r\nlpcomp_event_handler\r\n");
    }
    
    
    /**
     * @brief Print out detection statistics.
     */
    static void print_statistics(void)
    {
        while (voltage_falls_detected)
        {
            voltage_falls_detected--;
            //NRF_LOG_INFO("#%d fall detected", (int)voltage_falls_total);
    				printf("\r\nfall detected = %d\r\n",(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; // <3>---4/8 of 1.8V = 0.9V, <2>---down, lower 0.9V into interrupt
        config.input = NRF_LPCOMP_INPUT_2;															// <2>---P0.02
        // 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();
    	
    }
    
    
    
    int main(void)
    {
    	 uint32_t err_code;
    	
    	  NRF_CLOCK->EVENTS_HFCLKSTARTED=0;
    	  NRF_CLOCK->TASKS_HFCLKSTART=1;
    	  while(NRF_CLOCK->EVENTS_HFCLKSTARTED==0)
    		{
    		}
    		
        const app_uart_comm_params_t comm_params =
          {
              RX_PIN_NUMBER,
              TX_PIN_NUMBER,
              RTS_PIN_NUMBER,
              CTS_PIN_NUMBER,
              UART_HWFC,
              false,
    #if defined (UART_PRESENT)
              NRF_UART_BAUDRATE_115200
    #else
              NRF_UARTE_BAUDRATE_115200
    #endif
          };
    
        APP_UART_FIFO_INIT(&comm_params,
                             UART_RX_BUF_SIZE,
                             UART_TX_BUF_SIZE,
                             uart_error_handle,
                             APP_IRQ_PRIORITY_LOWEST,
                             err_code);
    
        APP_ERROR_CHECK(err_code);
    
    #ifndef ENABLE_LOOPBACK_TEST
        printf("\r\nCOMP example started.\r\n");
    #endif	   
    	
    	
        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_code1 = NRF_LOG_INIT(NULL);
    //    APP_ERROR_CHECK(err_code1);
    
    //    NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        lpcomp_init();
    
    		//bsp_board_led_on(BSP_BOARD_LED_0);
    	
        while (true)
        {
    //        print_statistics();
    //        bsp_board_led_on(BSP_BOARD_LED_1);
    //        NRF_GPIO->OUTCLR = (1 << 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->OUTSET = (1 << WAVE_ON_PIN_NUMBER);
            //nrf_delay_ms(1000);
            //NRF_LOG_FLUSH();
    			
    			//printf("\r\nUP\r\n");
    			//bsp_board_led_on(BSP_BOARD_LED_0);
        }
    }
     

  • Could you tell me what is the fastest sampling frequency for “Comparator different and high speed” mode?

    My "AN+" connected 10K hz 1.8V square wave, "AN-" connected 1.25V, but the output pin didn't output, I think frequency is faster than MCU comparator. thanks 

      

  • Hi,

    Looking at the LPCOMP chapter in the PS,  https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/lpcomp.html?cp=4_2_0_38_3_0#unique_1187726518 , 

    We have that "Time from VIN crossing (>=50mV above threshold) to ANADETECT signal generated." is typ 5us, i.e. max frequency is 200kHz.

    K-night said:
    My "AN+" connected 10K hz 1.8V square wave, "AN-" connected 1.25V, but the output pin didn't output, I think frequency is faster than MCU comparator. thanks 

    Could you show the code you use for this?

Related