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

Comparator not working with nrfx

I have attached the code.

I would like to compare one of the external inputs to an internal reference. Is there anything else to do to make this work? Maybe configure the pin itself to act as analog input or similar?

  • I have attached a potentionmeter to AIN0
  • I verified the voltage on the board at pin P0.03 - Sweeping it from 0 to 3V and back
  • The comparator always returns 0.
  • The ready event is triggered successfully.

Any ideas?

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include "nrf_gpio.h"
#include "nrfx_uart.h"
#include "nrfx_comp.h"
#include "nrfx_systick.h"

nrfx_err_t error = 0;

void comp_callback(nrf_comp_event_t event)
{
  printf(    (event == NRF_COMP_EVENT_READY ? "NRF_COMP_EVENT_READY" : \
    (event == NRF_COMP_EVENT_DOWN  ? "NRF_COMP_EVENT_DOWN"  : \
    (event == NRF_COMP_EVENT_UP    ? "NRF_COMP_EVENT_UP"    : \
    (event == NRF_COMP_EVENT_CROSS ? "NRF_COMP_EVENT_CROSS" : \
                                     "UNKNOWN ERROR")))));
  printf("/n");
}

int main(int argc, char *argv[])
{
    (void) argc;
    (void) argv;

    nrfx_systick_init();
    
    //gpio init
    nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(0,13));
    nrfx_systick_delay_ms(1000);
    nrf_gpio_pin_set(NRF_GPIO_PIN_MAP(0,13));

    // comparator init
    nrfx_comp_config_t comp_config;
    comp_config.reference           = 1;
    comp_config.main_mode           = 0;
    comp_config.threshold.th_down   = NRFX_VOLTAGE_THRESHOLD_TO_INT(0.5, 1.8);
    comp_config.threshold.th_up     = NRFX_VOLTAGE_THRESHOLD_TO_INT(1.5, 1.8);
    comp_config.speed_mode          = 2;
    comp_config.hyst                = 0;
    comp_config.input               = NRF_COMP_INPUT_0;
    comp_config.interrupt_priority  = NRFX_COMP_CONFIG_IRQ_PRIORITY;
    comp_config.ext_ref             = NRF_COMP_INPUT_1;

    nrfx_comp_event_handler_t comp_event_config = comp_callback;
    error = nrfx_comp_init(&comp_config, comp_event_config);
    printf("error: %i \n", error);
    nrfx_comp_start(0x0F,0);

    while(true)
    {
    nrfx_systick_delay_ms(100);
    uint32_t comp_status = 0xFF;
    comp_status = nrfx_comp_sample();
    printf("Comp Status: %i \n", comp_status);
//      nrfx_comp_sample() ? nrf_gpio_pin_set(NRF_GPIO_PIN_MAP(0,13)) : nrf_gpio_pin_clear(NRF_GPIO_PIN_MAP(0,13));
    }
}

Parents Reply Children
No Data
Related