Fully-portable time delay of specified length

M

Martin Wells

I'm doing an embedded systems project and I'm programming it as fully-
portable C89 (except of course for setting the pin values).

I need to put delays in the program, in the vicinity of 250 ms. (They
don't need to be exact).

Does C have a built-in method of putting in delays?

If not, I was thinking of defining a macro called "FLOPS" (i.e.
floating-point operations per second) and then doing something like:

void Delay(unsigned const fraction_of_full_second)
{
long unsigned amount_flop = FLOPS / fraction_of_full_second;

float x = 56.3;

do x /= x;
while (--amount_flop);
}

(Obviously I'll have to take into account the extra delay of looping
but you get the general idea)

Martin
 
K

keith

I'm doing an embedded systems project and I'm programming it as fully-
portable C89 (except of course for setting the pin values).

I need to put delays in the program, in the vicinity of 250 ms. (They
don't need to be exact).

Does C have a built-in method of putting in delays?

Assuming you have a reasonably standard set of libraries, take a look
at nanosleep(3RT). You'll need to link librt. An older alternative
is usleep(3C).
 
C

Chris Hills

I'm doing an embedded systems project and I'm programming it as fully-
portable C89 (except of course for setting the pin values).

I need to put delays in the program, in the vicinity of 250 ms. (They
don't need to be exact).

Does C have a built-in method of putting in delays?


No. You are best off using a Hw timer.
Write a function called delay which takes a parameter in ms.
You can then do your own library module which will contain the not
standard code.

Try comp.arch.embedded and say what your MCU is


If not, I was thinking of defining a macro called "FLOPS" (i.e.
floating-point operations per second) and then doing something like:

void Delay(unsigned const fraction_of_full_second)
{
long unsigned amount_flop = FLOPS / fraction_of_full_second;

float x = 56.3;

do x /= x;
while (--amount_flop);
}

(Obviously I'll have to take into account the extra delay of looping
but you get the general idea)

This will be fraught with problems as the compiler may or may not
optimise it differently as you build the application.
 
J

Joachim Schmitz

Assuming you have a reasonably standard set of libraries, take a look
at nanosleep(3RT). You'll need to link librt. An older alternative
is usleep(3C).
Neither is part of C89.

Bye, Jojo
 
A

Army1987

I'm doing an embedded systems project and I'm programming it as fully-
portable C89 (except of course for setting the pin values).

I need to put delays in the program, in the vicinity of 250 ms. (They
don't need to be exact).

Does C have a built-in method of putting in delays?
No, but you could use a loop and clock() if you have it:
clock_t start = clock();
while(clock() - start < 0.250 * CLOCKS_PER_SEC)
continue;
If not, I was thinking of defining a macro called "FLOPS" (i.e.
floating-point operations per second) and then doing something like:

void Delay(unsigned const fraction_of_full_second)
{
long unsigned amount_flop = FLOPS / fraction_of_full_second;

float x = 56.3;
Make it volatile or a sufficiently good compiler will remove the
next assignment.
 
K

Keith Thompson

Army1987 said:
No, but you could use a loop and clock() if you have it:
clock_t start = clock();
while(clock() - start < 0.250 * CLOCKS_PER_SEC)
continue;

You need to be aware that the value returned by clock() is specified
to be an indication of CPU time, not of wall clock time. They may
well be the same in your embedded environment, but you should confirm
that before depending on it.

On the other hand, a freestanding implementation isn't required to
provide the clock() function.

The best way to do this is likely to depend heavily on the nature of
your target environment. It might provide some function that does
exactly what you want. I can imagine that using a busy loop rather
than invoking some kind of sleep or delay function might cause
problems (on a multi-processing system, it needlessly gobbles CPU
time; on a single-process embedded system, it might needlessly gobble
power).

Somebody suggested posting to comp.arch.embedded; that's probably your
best bet.
 
T

Thad Smith

Chris said:
No. You are best off using a Hw timer.
Write a function called delay which takes a parameter in ms.
You can then do your own library module which will contain the not
standard code.

Try comp.arch.embedded and say what your MCU is

I agree. I also use C89 as much as practical on embedded targets, but
timer functions is one that often makes sense to use target-specific
code, especially on smaller processors. Your library may already
contain suitable time-related functions for your target.
 
U

user923005

I'm doing an embedded systems project and I'm programming it as fully-
portable C89 (except of course for setting the pin values).

I need to put delays in the program, in the vicinity of 250 ms. (They
don't need to be exact).

Does C have a built-in method of putting in delays?

If not, I was thinking of defining a macro called "FLOPS" (i.e.
floating-point operations per second) and then doing something like:

void Delay(unsigned const fraction_of_full_second)
{
long unsigned amount_flop = FLOPS / fraction_of_full_second;

float x = 56.3;

do x /= x;
while (--amount_flop);

}

(Obviously I'll have to take into account the extra delay of looping
but you get the general idea)
From the C-FAQ:

19.37: How can I implement a delay, or time a user's response, with
sub-
second resolution?

A: Unfortunately, there is no portable way. V7 Unix, and derived
systems, provided a fairly useful ftime() function with
resolution up to a millisecond, but it has disappeared from
System V and POSIX. Other routines you might look for on your
system include clock(), delay(), gettimeofday(), msleep(),
nap(), napms(), nanosleep(), setitimer(), sleep(), times(),
and
usleep(). (A function called wait(), however, is at least
under
Unix *not* what you want.) The select() and poll() calls (if
available) can be pressed into service to implement simple
delays. On MS-DOS machines, it is possible to reprogram the
system timer and timer interrupts.

Of these, only clock() is part of the ANSI Standard. The
difference between two calls to clock() gives elapsed
execution
time, and may even have subsecond resolution, if
CLOCKS_PER_SEC
is greater than 1. However, clock() gives elapsed processor
time
used by the current program, which on a multitasking system
may
differ considerably from real time.

If you're trying to implement a delay and all you have
available
is a time-reporting function, you can implement a CPU-
intensive
busy-wait, but this is only an option on a single-user,
single-
tasking machine as it is terribly antisocial to any other
processes. Under a multitasking operating system, be sure to
use a call which puts your process to sleep for the duration,
such as sleep() or select(), or pause() in conjunction with
alarm() or setitimer().

For really brief delays, it's tempting to use a do-nothing
loop
like

long int i;
for(i = 0; i < 1000000; i++)
;

but resist this temptation if at all possible! For one thing,
your carefully-calculated delay loops will stop working
properly
next month when a faster processor comes out. Perhaps worse,
a
clever compiler may notice that the loop does nothing and
optimize it away completely.

References: H&S Sec. 18.1 pp. 398-9; PCS Sec. 12 pp.
197-8,215-
6; POSIX Sec. 4.5.2.
 
K

karthikbalaguru

I'm doing an embedded systems project and I'm programming it as fully-
portable C89 (except of course for setting the pin values).

I need to put delays in the program, in the vicinity of 250 ms. (They
don't need to be exact).

Does C have a built-in method of putting in delays?

If not, I was thinking of defining a macro called "FLOPS" (i.e.
floating-point operations per second) and then doing something like:

void Delay(unsigned const fraction_of_full_second)
{
long unsigned amount_flop = FLOPS / fraction_of_full_second;

float x = 56.3;

do x /= x;
while (--amount_flop);

}

(Obviously I'll have to take into account the extra delay of looping
but you get the general idea)

nanosleep is a good bet.
But, it further depends on the type of MPU you use and the compiler
supported sleep routines
for giving you the desired delays. Check your specification/datasheet
also.

Karthik Balaguru
 
K

karthikbalaguru

I'm doing an embedded systems project and I'm programming it as fully-
portable C89 (except of course for setting the pin values).

I need to put delays in the program, in the vicinity of 250 ms. (They
don't need to be exact).

Further, How do you expect the delay to be portable ?
It has dependencies on the architecture (Processor Clk) and so it will
be varying from
architecture to architecture.
Maybe, you need to know list of all the Processor clocks and program
it accordingly.
But, thats not an easy job .

Karthik Balaguru
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top