_delay_us(10) v. _delay_us(10.0)

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi All,

I've inhereted some code with _delay_ms() and _delay_us() calls, however the author has chosen to use floating point constants as arguments rather than integer constants. I've read that this can bloat the code, but does it adversely affect the run time of these delays?

Usually I write _delay_us(10) for 10us delays and these work fine (verified with a countetr/timer), however what would you expect from _delay_us(10.0)? Does this also affect the _delay_ms() calls by the same degree?

Thanks,
Mark.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
however the author has chosen to use floating point constants as arguments rather than integer constants
If you look at the definitions of _delay_ms() and _delay_us(), they do indeed take floating point value.
Quote:
I've read that this can bloat the code
You've read wrong. Those two functions use floating point regardless of the value or type of the parameter. What bloats the code is when the parameter is not a compile time constant. If it is a compile time constant, then all of the floating point math is done by the compiler, not the AVR. If it is not a compile time constant (or you for some reason choose to compile without optimization), then indeed floating point routines will be used at run time. That will bloat the code if those routines were not already included in the code. It will, however, make the routines inaccurate (particularly _delay_us()).

Regards,
Steve A.

The Board helps those that help themselves.

Last Edited: Tue. May 21, 2013 - 09:47 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

_delay_us(10) should be the same as _delay_us(10.0)

The macros should evaluate the same number of _delay_loop_2()s. After all, 10.0 is the same value as 10. Both are compile-time constants

All the same, I see no advantage in using f-p constants. Do you ever want fractional us ?

However, it is not uncommon to want fractional _delay_ms() arguments. I would choose _delay_us(1234) in preference to _delay_ms(1.234). But whatever floats your boat !!

The important distinction is whether you have a runtime variable argument or a compile-time constant.

Untested. You can try the values for yourself.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
All the same, I see no advantage in using f-p constants. Do you ever want fractional us ?
If you want a delay less than a microsecond, then yes.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the replies, I understand now.

BTW is the reason for having to turn optimizations on because it that figures out if it is compile time constant parameter and optimizes to a constant? I've noticed the list file has calls to multiply routines with optimizations off, and they disappear when on.
Mark.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
BTW is the reason for having to turn optimizations on because it that figures out if it is compile time constant parameter and optimizes to a constant?
Yes.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
All the same, I see no advantage in using f-p constants. Do you ever want fractional us ?
If the processor is running at 14.372 MHz, floating point arithmetic will be involved.
No reason not to allow floating point arguments.
It also prevents a deceptive asymmetry with _delay_ms.

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
If the processor is running at 14.372 MHz, floating point arithmetic will be involved.
F_CPU is usually an integer, not a floating point number. And if you did make it a floating point number, it would be 14372000.0 since the value needs to be in Hertz.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Mark,

If you are ever in doubt about stuff like this just look at the .lss file. You should find that exactly the same code was generated whether you use (10) or (10.0).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

BTW is the reason for having to turn optimizations on because it that figures out if it is compile time constant parameter and optimizes to a constant?

Yes.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]