nRF5 SDK v15.3.0

The Comparator (COMP) driver includes two layers: the hardware access layer (HAL) and the driver layer (DRV).

The hardware access layer provides basic APIs for accessing the registers of the COMP peripheral. All functions in this layer are implemented as inline functions. See the API documentation for the COMP HAL for details.

The driver layer provides APIs on a higher level than the HAL. See the API documentation for the COMP driver - legacy layer for details.

Key features include:

Driver configuration

The COMP driver default configuration is located in sdk_config.h. The COMP peripheral must be explicitly enabled in sdk_config.h before the driver can be used. If nrf_drv_comp_init is called with a NULL pointer to the configuration structure, the default configuration from sdk_config.h is used. You must provide an event handler function during initialization.

The configurable parameters include:

If you want to initialize the peripheral with the default configuration, but with a different input pin, use the macro NRF_DRV_COMP_DEFAULT_CONFIG with the pin number as parameter to create the configuration structure. Alternatively, you can initialize the peripheral with a NULL pointer as first parameter to use the default configuration and then call nrf_drv_comp_pin_select to change the selected comparator input.

The following code example shows how to initialize the driver to use NRF_COMP_INPUT_0. Note that you must provide a valid event handler (comp_event_handler in the example).

// Configure threshold voltages.
comp_config.threshold.th_down = VOLTAGE_THRESHOLD_TO_INT(0.9, 1.8);
comp_config.threshold.th_up = VOLTAGE_THRESHOLD_TO_INT(1.5, 1.8);
err_code = nrf_drv_comp_init(&comp_config, comp_event_handler);

Using the COMP driver

The COMP driver can be initialized in one of two main operation modes:

Capacitive sensing

Using the comparator as a relaxation oscilator, as described in this section, is not supported on nRF52810 due to hardware limitations.

The COMP peripheral supports capacitive sensing. To use the comparator as relaxation oscillator, configure the driver to enable a current source on the analog input (nrf_drv_comp_config_t::isource). The frequency of this oscillator is f_OSC = I_SOURCE / (2C*(VUP-VDOWN)), where VUP = (THUP+1)/64*VREF and VDOWN = (THDOWN+1)/64*VREF. Use timers to measure the period of the oscillator and calculate capacity changes based on this equation. When the pad that is connected to the analog input is touched, the capacity increases.

The following code example shows how to start the COMP as relaxation oscillator:

// Use the default configuration, but set isource to 5uA source.
// Initialize the driver.
err_code = nrf_drv_comp_init(&comp_config,comp_event_handler);
if (err_code != NRF_SUCCESS)
// Initialization failed. Take recovery action.
// Start the COMP peripheral without enabling any interrupts or shorts.
When using the comparator as relaxation oscillator, you should not set any interrupts from the comparator. The frequency of this oscillator is usually very high (around 500 kHz). Therefore, the processor might not be able to complete all instructions in the interrupt handler before the next interrupt.

Documentation feedback | Developer Zone | Subscribe | Updated