using pwm input to measure frequency

I have an NRF9160 system running and I see mention of "The nRF9160 PWM peripheral can capture the period and pulse width of an external PWM signal" but  was a little confused as to how to implement it. I have used and I am using PWM as an output so that I understand. I have used a generic GPIO as input and used interrupts to start and stop a timer to measure time between edges. This uses two items a generic GPIO and a timer. as I understand the capture capability it just used a single PWM to do the measurement.

can you point me to any examples how to implement this if I am correct in assuming all I need is a single PWM pin.

Parents Reply Children
  • Sorry for delayed response Timothy, 

    Timothy said:
    1. is PWM capture possible for my device?

    The PWM peripheral in itself is output so it cannot do a input capture. But you can use a TIMER-GPIOTE in event mode + PPI to make a capture like configuration, 

    Timothy said:
    2. how can I get a timer to time at 1 usec?

    In nrf connect SDK, you cannot get RTOS timer to have a better resolution than 1ms using RTOS timer API. So you would have to use nrfx_timer HAL API directly for measuring time with 1us resolution, you can do something like this in your code (use as template)

    // top of src/jfet_init.c
    #include <zephyr.h>
    #include <nrfx_timer.h>
    
    
    #define TIMER_ID 1
    static const nrfx_timer_t timer = NRFX_TIMER_INSTANCE(TIMER_ID);
    
    
    void jfet_init(void)
    {
        /* Configure the timer for 1 MHz (1 µs ticks) */
        nrfx_timer_config_t cfg = NRFX_TIMER_DEFAULT_CONFIG;
        cfg.frequency = NRF_TIMER_FREQ_1MHz;
        cfg.mode      = NRF_TIMER_MODE_TIMER;
        cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
    
        /* Init & start */
        nrfx_timer_init(&timer, &cfg, NULL);
        nrfx_timer_enable(&timer);
    
        /* …rest of your JFET setup… */
    }
    
    
    uint32_t read_now_us(void)
    {
        nrfx_timer_capture(&timer, NRF_TIMER_CC_CHANNEL0);
        return nrfx_timer_capture_get(&timer, NRF_TIMER_CC_CHANNEL0);
    }
    

Related