_delay_ms() not providing a proper delay

Guest #6342561
Rate this post
useful
not useful
F_CPU can be set as a compiler parameter (-D) or as a definition 
(#define) in your source code. If it is in source code, then it must be 
placed above the #include statement that load the util/delay.h file.

The value of F_CPU must match the real clock frequency of the CPU, but 
it cannot be used to configure the CPU. The CPU derives its clock from 
an external crystal or resonator which should oscillate at 16 MHz. If 
the fuse CLKDIV8 is set, then the initial CPU clock is 1/8 of the 16 
MHz. On Arduino modules, this fuse should normally be off. Check that.

You software can modify the clock divider by writing to the CLKPRE 
register. You can divide the 16 MHz by 1 .. 256.

Something is wired in your case because you say that the time is 1/4 of 
the expected one. So the CPU seems to run at 64 MHz. Needless to say 
that this is technically impossible. So the only thing I could imagine 
is that your F_CPU is not properly set, although you say that it is.
Guest #6343148
Rate this post
useful
not useful
No!

Avr-libc says it is possible to define delays up to 6.5535 seconds 
(independent from CPU frequency).

>void _delay_ms   (   double    __ms  )

>Perform a delay of __ms milliseconds, using _delay_loop_2().

>The macro F_CPU is supposed to be defined to a constant defining the CPU clock 
frequency (in Hertz).

>The maximal possible delay is 262.14 ms / F_CPU in MHz.

>When the user request delay which exceed the maximum possible one, _delay_ms() 
provides a decreased resolution functionality. In this mode _delay_ms() will work 
with a resolution of 1/10 ms, providing delays up to 6.5535 seconds (independent 
from CPU frequency). The user will not be informed about decreased resolution.
Guest #6343245
Rate this post
useful
not useful
Lutz wrote:
> First answer was korrekt: The maximal possible delay is 262.14 ms /
> F_CPU in MHz

How does this fit to the observation of 250ms faster at 16 Mhz?

262.14 ms / 16 = 16.4 ms

Read the paragraph below that in the documentation. As Gerhard wrote, 
much longer delays are possible but then the resolution is only 1/10 
seconds which is not a problem in this case. Most people even did not 
notice that the delay function can be called with a floating point 
parameter to delay fractions a milliseconds.
Guest #6344810
Rate this post
useful
not useful
Please check also this:
Could it be possible, that your Core currently running on internal osc 
with 8MHz?
This would decribe the the Quarter delay:
1. 8MHz instead of 16MHz
2. Clock Divider of 8 is set
This topic is locked and can not be replied to.