473,395 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Fully-portable time delay of specified length


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

Oct 8 '07 #1
9 2250
On 8 Oct, 12:43, Martin Wells <war...@eircom.netwrote:
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).

Oct 8 '07 #2
ke***@bytebrothers.co.uk wrote:
On 8 Oct, 12:43, Martin Wells <war...@eircom.netwrote:
>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,
I think you missed out the word "POSIX" or similar in this sentence...
... take a look
at nanosleep(3RT). You'll need to link librt. An older alternative
is usleep(3C).
Oct 8 '07 #3
In article <11**********************@y42g2000hsy.googlegroups .com>,
Martin Wells <wa****@eircom.netwrites
>
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.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Oct 8 '07 #4
<ke***@bytebrothers.co.ukschrieb im Newsbeitrag
news:11**********************@k79g2000hse.googlegr oups.com...
On 8 Oct, 12:43, Martin Wells <war...@eircom.netwrote:
>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).
Neither is part of C89.

Bye, Jojo
Oct 8 '07 #5
On Mon, 08 Oct 2007 04:43:46 -0700, Martin Wells wrote:
>
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.
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)
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Oct 8 '07 #6
Army1987 <ar******@NOSPAM.itwrites:
On Mon, 08 Oct 2007 04:43:46 -0700, Martin Wells wrote:
>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;
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 8 '07 #7
Chris Hills wrote:
In article <11**********************@y42g2000hsy.googlegroups .com>,
Martin Wells <wa****@eircom.netwrites
>>
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
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.

--
Thad
Oct 9 '07 #8
On Oct 8, 4:43 pm, Martin Wells <war...@eircom.netwrote:
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

Oct 11 '07 #9
On Oct 8, 4:43 pm, Martin Wells <war...@eircom.netwrote:
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

Oct 11 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Johnny | last post by:
Now I am running PHP in Apache on Windows platform, but I need to migrate it into linux and using IPv6 instead. So I want to know if PHP fully support IPv6. And how about the workload of this...
3
by: Lewis | last post by:
I have "My Documents" redirected to a network share but when I go to open a project it says that the share is not fully trusted. I found this info: > The simplest (least work) in this situation...
4
by: Aashish Patil | last post by:
Hello, Is it possible to obtain the fully qualified name of a class in c#. What I am looking for is something analogous to Box.class.getName() that exists in Java. Its possible to get this by...
5
by: fc2 | last post by:
Hi I have a problem with fully qualified names. According to the C# language specification: "Every namespace and type has a fully qualified name, which uniquely identifies the namespace or...
23
by: Xah Lee | last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee, 2006-03-15 Let me summarize: The LISP notation, is a functional notation, and is not a...
1
by: Erland | last post by:
Hi all, As per my understanding in order to load an assembly using Assembly.Load() you have to provide fully qualified name of the assembly you are trying to load e.g. Assembly...
5
by: thisis | last post by:
Hi All, Hi All, (this is not the same topic as the my previous topic) What objects/methods/properties does VBScript offer for: Assuring/guarantee/make certain that ASP/VBSCript an ELEMENT...
30
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”....
7
by: Tom | last post by:
By my estimate, greater than 90% of the online doco code does not work without additional coding effort. Fully working solutions are invaluable for the student. A guru's work measured in minutes...
6
by: Vince | last post by:
Hello all, I am using Visual Basic to open a saved query and then save information in the query to an array for later use. The problem is that the same query shows different results when opened...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.