| nRF5 SDK v12.0.0 |

Choose documentation: | nRF5 SDK | S130 SoftDevice API | S132 SoftDevice API | S212 SoftDevice API | S332 SoftDevice API

All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
COMP
This information applies to the nRF52 Series only.

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 for details.

Key features include:

- Triggering compare operations and returning comparison results
- Using the comparator as relaxation oscillator that supports capacitive sensing

# 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:

- Reference selection: VDD, external reference from AIN0 or AIN1 (between 0 V and VDD), or internal references 1.2 V, 1.8 V, or 2.4 V.
- Main operation mode: Single-ended or differential (see Using the COMP driver).
- Threshold voltages: The difference between the up threshold and the down threshold determines hysteresis. Used in single-ended mode.
- Comparator hysteresis: 50 mV. Can be used in differential mode.
- Speed and power mode: Low power, normal, or high-speed.
- Current source: Can be used to form a relaxation oscillator (see Capacitive sensing).
- Interrupt priority level: Any available interrupt priority level.

If you want to initialize the peripheral with the default configuration, but with a different input pin, use the macro NRF_DRV_COMP_CONF_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).

nrf_drv_comp_config_t comp_config = NRF_DRV_COMP_CONF_DEFAULT_CONFIG(NRF_COMP_INPUT_0);
// 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:

- Single-ended mode uses one channel as input voltage and the other channel as reference voltage. In this mode, you can fully configure hysteresis through the TH register using a 64-level reference ladder. The threshold voltages VUP and VDOWN are calculated based on the THUP and THDOWN values that you specify during initialization. The comparator will change from ABOVE to BELOW when VIN+ becomes smaller than VDOWN and from BELOW to ABOVE when it becomes bigger than VUP. If an external analog reference is used, it must be AIN0 or AIN1.
- Differential mode uses one channel as input voltage and the other channel as threshold voltage. The threshold voltage must be provided on AIN0 or AIN1. In this mode, you can enable or disable a fixed hysteresis (50 mV). If hysteresis is enabled, the output of the comparator will change from ABOVE to BELOW when VIN+ becomes 25 mV (50 mV / 2) smaller than the threshold voltage VIN-. Similarly, it changes from BELOW to ABOVE when VIN+ becomes 25 mV larger than VIN-.

## Capacitive sensing

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.
nrf_drv_comp_config_t comp_config = NRF_DRV_COMP_CONF_DEFAULT_CONFIG(NRF_COMP_INPUT_2);
comp_config.isource = NRF_COMP_ISOURCE_Ien5uA;
// 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.
nrf_drv_comp_start(0,0);

Note
  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.