Fully-portable time delay of specified length

Discussion in 'C Programming' started by Martin Wells, Oct 8, 2007.

  1. Martin Wells

    Martin Wells Guest

    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
     
    Martin Wells, Oct 8, 2007
    #1
    1. Advertising

  2. Martin Wells

    Guest

    On 8 Oct, 12:43, 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?


    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, 2007
    #2
    1. Advertising

  3. Martin Wells

    Mark Bluemel Guest

    wrote:
    > On 8 Oct, 12:43, 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?

    >
    > 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).
    >
     
    Mark Bluemel, Oct 8, 2007
    #3
  4. Martin Wells

    Chris Hills Guest

    In article <>,
    Martin Wells <> writes
    >
    >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 /\/\/\/\/
    /\/\/ www.phaedsys.org \/\/\
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
     
    Chris Hills, Oct 8, 2007
    #4
  5. <> schrieb im Newsbeitrag
    news:...
    > On 8 Oct, 12:43, 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?

    >
    > 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
     
    Joachim Schmitz, Oct 8, 2007
    #5
  6. Martin Wells

    Army1987 Guest

    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.
     
    Army1987, Oct 8, 2007
    #6
  7. Army1987 <> writes:
    > 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) <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"
     
    Keith Thompson, Oct 8, 2007
    #7
  8. Martin Wells

    Thad Smith Guest

    Chris Hills wrote:
    > In article <>,
    > Martin Wells <> writes
    >>
    >> 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
     
    Thad Smith, Oct 9, 2007
    #8
  9. Martin Wells

    user923005 Guest

    On Oct 8, 4:43 am, 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?
    >
    > 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.
     
    user923005, Oct 9, 2007
    #9
  10. On Oct 8, 4:43 pm, 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?
    >
    > 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
     
    karthikbalaguru, Oct 11, 2007
    #10
  11. On Oct 8, 4:43 pm, 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).
    >


    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
     
    karthikbalaguru, Oct 11, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frederick Gotham

    Fully-portable version of limits.h

    Frederick Gotham, Sep 18, 2006, in forum: C Programming
    Replies:
    4
    Views:
    307
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
    Sep 19, 2006
  2. Tomás Ó hÉilidhe

    SHA-1 in fully-portable C89?

    Tomás Ó hÉilidhe, Jan 1, 2008, in forum: C Programming
    Replies:
    23
    Views:
    744
    Szabolcs Nagy
    Jan 8, 2008
  3. Tomás Ó hÉilidhe

    SHA1 algorithm -- fully portable

    Tomás Ó hÉilidhe, Mar 24, 2008, in forum: C Programming
    Replies:
    4
    Views:
    677
    Bill Reid
    Mar 27, 2008
  4. Bill Reid

    Is this fully portable and/or smart?

    Bill Reid, May 19, 2008, in forum: C Programming
    Replies:
    54
    Views:
    1,086
    Bill Reid
    May 22, 2008
  5. Tomás Ó hÉilidhe

    Fully Portable Big Number Library

    Tomás Ó hÉilidhe, Oct 27, 2008, in forum: C Programming
    Replies:
    11
    Views:
    2,033
    user923005
    Oct 29, 2008
Loading...

Share This Page