Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Simplest way to continuously toggle a GPIO pin for a fixed amount of time then stop?

Experienced developer new to embedded C programming here, so please forgive me if I'm missing something obvious. I already went through the GPIOTE example from the nRF52 SDK (v15) and this app timer tutorial.

I want to continuously (say, every 100ms) toggle a GPIO pin between high and low but stop that loop after 20 seconds. I also want to be able to start and stop (don't need the ability to resume) this 20-second sequence on demand.

It looks like either a GPIOTE task or a repeating app timer could handle the continuous toggling for me, but I'm not sure how best to stop it after 20 seconds. Could I start a GPIOTE task to perform the repeated GPIO toggling inside a single-shot 20-second timer then start/stop that timer whenever I please? Given that both GPIOTE tasks and the app timer use the LFCLK, I want to make sure that they will not conflict with one another.

Any high-level example code you could share would be much appreciated. Thanks!

Parents
  • Hi 

    Unless you need extremely consistent timing I agree with Mike: You can just use an app_timer to toggle the LED, and update a counter inside the callback to stop the app_timer after the set period. 

    The app_timer callbacks can be delayed by the SoftDevice (up to some hundred microseconds), so if this is an issue it is a better option to set up a dedicated TIMER or RTC module, and use PPI and GPIOTE to update the pin whenever the TIMER/RTC reaches a preset compare value. 

    Best regards
    Torbjørn 

Reply
  • Hi 

    Unless you need extremely consistent timing I agree with Mike: You can just use an app_timer to toggle the LED, and update a counter inside the callback to stop the app_timer after the set period. 

    The app_timer callbacks can be delayed by the SoftDevice (up to some hundred microseconds), so if this is an issue it is a better option to set up a dedicated TIMER or RTC module, and use PPI and GPIOTE to update the pin whenever the TIMER/RTC reaches a preset compare value. 

    Best regards
    Torbjørn 

Children
Related