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

Reading digital waveform

Hi, I need to read a digital signal with varying pulse widths (Min ~120µs) over ~30ms. Currently (nRF52832) I'm doing this using a timer interrupting every ~15µs and reading the GPIO value in the timer handler, then processing this data after all samples are taken.

Is there a better way to read this signal? I will need to read it with the S140 softdevice running. I have considered the timeslot API however I'm not sure that this is suitable as I have no control of when the external device outputs the waveform. Thanks.

  • Hello muon

    One possibility is using GPIOTE, PPI and a timer. For this specific implementation you would need to use two pins connected to your signal source in parallel, and 2 PPI channels.

    One pin will generate the HITOLO event, while the other generates the LOTOHI. You could then use PPI to clear the timer on a positive pulse edge, and capture the timer value on the negative pulse edge. The value captured from the timer, divided on the timer frequency would give you the duration of the pulse. You could then use a GPIOTE handler on the HITOLO event to store the captured data. Since the capture task is triggered by PPI, it should always finish before the handler is called to store the data.

    You still risk losing pulses though due to the Softdevice, but you now only dedicate CPU time to it when the pulse is finished and as long as the Softdevice finishes before the next pulse ends (which is when the compare value is overwritten) you should be good.

    I have attached a project to illustrate. It is for SDK14, with the nRF52832 devkit (pca10040). It uses button 1 as pulse source. The GPIOTE events are generated from pin 13 (button 1) and 11. Falling edge on pin 13 clears timer 1, while rising edge on pin 11 captures the value. The GPIOTE interrupt handler is called on pin 11 event just after that, and plots the timer value in console through NRF_LOG. The handler also toggles LED 1, this is only so I could see when the handler was called with a logic analyzer.

    For it to work you need to connect pin 13 and pin 11.

    Since the buttons connect the pins to ground the pulses are low while default value is high, so if the pulses you want to measure are high you would need to do it the other way around (clear on positive edge, capture on negative).

    You might see that sometimes the number is very low after a button press (less than 100). This is due to the lack of button debounce. The timer frequency default value is here set to 250kHz in sdk_config.h. You should make sure it doesn't count fast enough to overflow during a pulse.

    Best regards

    Jørn Frøysa

    gpiote - pulse width.zip

  • Hi Jørn,

    Thank you for your reply, this works as expected on the pca10040.

  • Is there an SDK 16 version of this example? I can see the LED toggling but no LOG info is displayed... 

Related