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

using PWM to measure input period and pulse width

I have an input signal of unknown period and pulse width on a GPIO. I want to use the PWM capture capabilities to  retrieve those parameters.

I see in the documentation there is

pwm_pin_configure_capture, pwm_pin_enable_capture, pwm_pin_disable_capture, pwm_pin_capture_usec, pwm_capture_callback_handler

I did not see an example how to use these. could you show me a simple example of what I need to do to  get the period and pulse width from an input signal using a PWM. The functions seem to be exactly what I need but the implementation is not clear in the documentation and I am getting a lot of compile errors. I am sure it is something simple I am missing. I appreciate any help you can give.

  • You need to use timer for this purpose. You shall use GPIO pin which can create an interrupt. Create an interrupt when the pulse changes high low and start the counter. Stop the counter when the pulse goes  low. This will give you the pulse width

    Similarly to measure period, start the counter when the pusle goes from high to low and stop it in the next hight to low

    This is standard procedure for any  micro 

  • Vkadal

    thank you for your quick response. what counter are you referring to? I start a timer when the GPIO goes from low to high and stop the timer when it goes from high to low. I was expecting to be able to see how much time was left when I stopped it.

    for example if I get a low to high edge I set the timer to 1000ms with wait forever (one shot). when the high to low edge comes at 100ms I stop the timer. I expected to be able to see that 900ms was left on my timer and then I would know my pulse width was 1000 - 900 = 100ms. I could not find out how to tell how much time is left or what counter your are talking about. I do get an interrupt on both edges of the GPIO and I do see the timer stop function called.

  • What I am writing is from the general knowledge of micro contriller and it shall be the same for Nordic too

    The micro controller can not measure time between two events directly. Let us assume  the micro controller operates an 1 MHz  clock speed. This has been given as the source to the timer. Then the timer will increment every clock cycle. That every 1 micro second, the timer will be  incremented

    Assume it is an 8 bit timer. the counter can increment from 0 to 255 and come back to zero again,

    Allow  the incoming signal  to interrupt the processor when the signal goes  low to high. Then start the counter . The counter will be counting from zero. The incoming signal shall generate another interrupt while it  is going from lhifh to low. Then stop the counter . Read the counter value

    Say the counter reads 100, then the  pulse width is 100 uS

    There are other complication to deal with. Will explain once you are done with this 

    To know the time between two events

  • Vkadal

    Thank you for your response. I used the Nordic built in timers and can get the pulse width and the period using interrupts.

  • Vkadal

    I need to measure pulse width and periods in the microsecond range.(20 - 400 us pulse width). I do not see a way to get the nRF9160 timers to count that fast with interrupts and the latency involved. I do not see a way to send an external clock into the nRf9160 timers. they need to be started with a given expire time. using the timers I can measure pulse width in the order of ~10ms. I set a timer expiration time of 1000ms and start the timer on the leading edge interrupt and stop the timer on the falling edge interrupt. I get the remaining ticks and subtract it from  (1000 * ticks/sec) this gives me the pulse width. this fails as I get below 10 ms.

    How can I get microsecond resolution?

Related