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

How to increase the number every certain time?

Hello all,

I have a question about nRF52832.

What I want to do is increase a number every 20 micro seconds.

In other words, count how much time has passed .

(ex. int count = 0; every 20 microseconds, count++;)

If I use delay function, I can do it.

But, I want that process while other programs are ongoing, without pause.

Can you guys recommend the way or related example?

Thanks so much and have a nice day!

  • Hello,

    What I want to do is increase a number every 20 micro seconds.

    In other words, count how much time has passed .

    (ex. int count = 0; every 20 microseconds, count++;)

    What you are describing sounds a lot like a general TIMER application - I highly recommend taking a look at the timer peripheral example from the SDK, which demonstrates how you may go about setting up and using the timer driver. You could also see the nrfx_timer API Reference documented here. While the example uses the legacy nrf_drv_timer, I highly recommend that you make use of the nrfx_timer driver instead. If you compare the two, you will see that most functions have an updated version in the nrfx_ driver, which can be used in the same manner as the legacy version.

    To modify the timer example to do exactly what you describe above, all you need to do is change the time_ms to 2 ms ( or use the nrfx_timer_us_to_ticks function ), and change the contents of the timer event handler ( timer_led_event_handler ) to instead increment a variable.
    If you make such changes, I must recommend that you also change all function and variable names to something appropriate for their modified usage / functionality.

    If I use delay function, I can do it.

    But, I want that process while other programs are ongoing, without pause.

    Great! This is definitely a better way to go about it, since using a delay is seldom the best option for these kinds of operations.
    A timer is a much better way to go about it, since it is more accurate, while at the same time freeing the CPU to perform other tasks ( or sleep! ) in the meantime.

    Thanks so much and have a nice day!

    Thank you, and I hope you have a nice day as well!

    Please do not hesitate to ask if anything should be unclear, or if you should encounter any issues or questions in the future.

    Best regards,
    Karl

Related