How to get pulse time with microsecond level?

Hi Nordic,

I need to read pulse time which is microsecond level in different channel, I have consider combining timers with GPIOE but the timer is millisecond level. Is there any example to achieve it?

BTW, I'm using SDK17.02 on nRF52810.

Parents Reply Children
  • static const nrfx_timer_t micros_sample_timer = NRFX_TIMER_INSTANCE(4); //select timer instance
    
    uint32_t micros;
    
    static void micros_timer_handler(nrf_timer_event_t event_type, void * p_context)
    {
        micros++;
    }
    
    static int timer_init_micros(void)
    {
        nrfx_err_t err;
        nrfx_timer_config_t timer_cfg = {
            .frequency = NRF_TIMER_FREQ_16MHz,
            .mode = NRF_TIMER_MODE_TIMER,
            .bit_width = NRF_TIMER_BIT_WIDTH_32,
        };
    
        err = nrfx_timer_init(&micros_sample_timer, &timer_cfg, micros_timer_handler);
        if (err != NRFX_SUCCESS) {
            LOG_INF("nrfx_timer_init failed with: %d\n", err);
            return -EAGAIN;
        }
    /* For Zephyr*/
    /*
        IRQ_CONNECT(DEFAULT_TIMER_IRQ, DEFAULT_TIMER_PRIORITY,
                DEFAULT_TIMER_IRQ_HANDLER, NULL, 0);
    */
    
        nrfx_timer_extended_compare(&micros_sample_timer,
                NRF_TIMER_CC_CHANNEL4,
                nrfx_timer_us_to_ticks(&micros_sample_timer, 1), // 1 us tic
                NRF_TIMER_SHORT_COMPARE4_CLEAR_MASK,
                true);
    
        nrfx_timer_enable(&micros_sample_timer);
    
        return 0;
    }

    use micros for pulse period calc

  • The app timer tick frequency depends on the config you set in sdk_config.h (APP_TIMER_CONFIG_RTC_FREQUENCY). If you set it to 0 you get the max tick rate of 32768 ticks per seconds which equals about 30.5us per tick.

    You cannot use any ticks less than 5 in app_timer_start, so the shortest delay you can get is around 152.5us. Getting smaller than that using app_timer is not possible. You should try to use high frequency TIMERS instead for that.

Related