Counter using timer

I am using timer 3 as a counter for my capacitor discharge time. Currently, the value of is always problematic because it shows with too many deviation values ​​sometimes 15837, but other times 239 while the environment for the hr202 sensor remains unchanged. I am using nrf_drv_timer_capture() function to get that time value. So what am I encountering here?

this is my code and may schematic. Can someone help me?

void timer_event_handler(nrf_timer_event_t event_type, void *p_context) {}

const nrf_drv_timer_t TIMER = NRF_DRV_TIMER_INSTANCE(3);

static void config_timer()
{
    uint32_t err_code = NRF_SUCCESS;

    nrf_drv_timer_config_t timer_cfg;

    timer_cfg.frequency = NRF_TIMER_FREQ_1MHz;
    timer_cfg.mode = NRF_TIMER_MODE_TIMER;
    timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_16;
    timer_cfg.interrupt_priority = 6;
    timer_cfg.p_context = NULL;

    err_code = nrf_drv_timer_init(&TIMER, &timer_cfg, timer_event_handler);
}

static void human_begin_read(uint8_t type)
{
    nrf_gpio_cfg_output(HUMI_PIN);
    nrf_gpio_cfg_output(REF_PIN);
    nrf_gpio_cfg_output(READ_PIN);

    // clear pin
    nrf_gpio_pin_write(HUMI_PIN, 0);
    nrf_gpio_pin_write(REF_PIN, 0);
    nrf_gpio_pin_write(READ_PIN, 0);
    nrf_delay_ms(1);

    humi_state = HUMI_NORMAL;
    ovf_count = 0;
    humi_time = 0;

    nrf_gpio_cfg_input(READ_PIN, NRF_GPIO_PIN_NOPULL);
    if (type == REF)
    {
        nrf_gpio_cfg_default(HUMI_PIN);
        nrf_gpio_pin_write(REF_PIN, 1);
    }
    else
    {
        nrf_gpio_cfg_default(REF_PIN);
        nrf_gpio_pin_write(HUMI_PIN, 1);
    }

    uint32_t time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER, 1000);
    nrf_drv_timer_extended_compare(
        &TIMER, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);

    nrf_drv_timer_enable(&TIMER);
    while (nrf_gpio_pin_read(READ_PIN) == 0)
    {
    }

    humi_time = nrf_drv_timer_capture(&TIMER, NRF_TIMER_CC_CHANNEL0);

    nrf_drv_timer_clear(&TIMER);
    nrf_drv_timer_disable(&TIMER);

    humi_state = HUMI_SUCCESS;

    nrf_gpio_cfg_output(HUMI_PIN);
    nrf_gpio_cfg_output(REF_PIN);
    nrf_gpio_cfg_output(READ_PIN);

    // clear pin
    nrf_gpio_pin_write(HUMI_PIN, 0);
    nrf_gpio_pin_write(REF_PIN, 0);
    nrf_gpio_pin_write(READ_PIN, 0);
    nrf_delay_ms(1);
}

Parents
  • Hi,

    There are a few potential error sources here:

    • You are measuring an analog input signal by reading it as a digital input. This will be  inaccurate as there is a significant undefined region (see GPIO Electrical Specification). You should use the comparator for such tasks.
    • You have a while loop where you read. If there is an interrupt in between here that could cause additional delay. You should solve this using PPI.

    As you see, the proper way to do this, which would solve both these issues is to use the comparator to get an event when the voltage passes a threshold, and use PPI to capture the timer when that happens. This way this will happen all in HW, and you can read the value later when the CPU has time. Any interrupts at a bad time will not affect the measurements.

  • Hi,

    Great for your suggestion. But I am having a question when using PPI what will be the current consumed? Since our device has to optimize the power level in the best way, I want to know this

Reply Children
Related