2

I'm trying to determine if the AVR delay functions _delay_us() and _delay_ms() use timers in such a way that I can't use those timers for other functionality in my code. If so, what timer does it use?

For reference I'm using an Arduino Uno with ATmega328, but I imagine if it's an AVR function it must work similarly on other devices.

2 Answers 2

3

No, these macros expand to calls to __builtin_avr_delay_cycles(), which are compiled into delay loops. It should be noted that:

  • the arguments to these macros should be compile-time constants, they can be floating point
  • the macros are cycle-accurate, e.g. _delay_us(0.125) will take exactly two CPU cycles on an Uno
  • whereas the CPU time used by the call is exactly what you ask for, the physical time will be larger if the call gets interrupted.
1
  • Thank you! I was worried I wouldn't be able to use one of the timers if I used the delay functions.
    – lemontwist
    Jun 22, 2017 at 14:41
0

No.

The long answer is that you can delay via timers, a little bit more painfully within the Arduino framework, or much more efficiently if you are willing to move outside of the arduino framework.

I wrote a set of systick clones for many of the popular 8bit mcus precisely for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.