Thread: how to add a delay??

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    27

    how to add a delay??

    Any way to put a delay into this.:

    my code is this:

    Code:
    HR1 = 92160/pulseperiod;
    ADDR1 = 1;
    
    if(HR1 < 40)
    {
    }
    else
    {
    TXBUF0 = ADDR1;
                    <---------------------------------// i want to put a delay of 0.1sec here.
    TXBUF0 = HR1;
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    What platform/OS are you using?
    For windows there is Sleep(). For linux usleep() (maybe and others). In both you enter the milliseconds of the delay.

    Otherwise you can use a for-loop. Just make sure you add an instruction, because if you do something like this
    Code:
    for(i=0; i < 10000; ++i) ;
    your compiler might see that it does nothing and not run it at all. You could do something like
    Code:
    for(i=0; i < 10000; ++i) a++;
    But of course you don't know exactly how much time needed for it to execute, you would have to estimate by measuring it.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    27
    im using window.ya if i use sleep();

    what do i need to use for the header? is it conio.h? or some other thing?because i try putting in the sleep(); bu they prompt me with warning.

    "function sleep declared implicitly"...

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by C_ntua View Post
    Otherwise you can use a for-loop. Just make sure you add an instruction, because if you do something like this
    Code:
    for(i=0; i < 10000; ++i) ;
    your compiler might see that it does nothing and not run it at all. You could do something like
    Code:
    for(i=0; i < 10000; ++i) a++;
    Most compilers would still optimize the loop to just a+=10000;
    Or if it sees that a is never used after the loop it might not even do the assignment at all.

    Quote Originally Posted by shifu View Post
    im using window.ya if i use sleep();

    what do i need to use for the header? is it conio.h? or some other thing?because i try putting in the sleep(); bu they prompt me with warning.

    "function sleep declared implicitly"...
    #include <windows.h>
    Sleep(<time>); (capital S)

  5. #5
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by shifu View Post
    im using window.ya if i use sleep();

    what do i need to use for the header? is it conio.h? or some other thing?because i try putting in the sleep(); bu they prompt me with warning.

    "function sleep declared implicitly"...
    Just an FYI, conio.h is for keyboard functions...was more for DOS.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For DOS it's sleep(seconds you want), and include dos.h

    For Windows it's Sleep(milliseconds you want), and include windows.h

    It's confusing sometimes!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I doubt it, since 99% of all people don't DOS at all anymore!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    27
    thank guy for the help once again.i will try it out and let u get know. im not using visual c to program it im using IAR workbench to program my code.so i do not know it will work not. so will update you guy again. thank anyway.

  9. #9
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    I do not want to be a pain, but for me the code looks like asking for problems. You want to write a value in variable, sleep, and write another value to same variable. Do not tell me you're planning to read this value during the sleep from some other process? If you do, then i hope you have real good realtime os underneath. Else this sounds like asking for problems. I suggest you to forget the sleep, and build some proper synchronization, where the reader tells when new value can be written.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-23-2009, 01:55 PM
  2. Modify mod_auth_http to add a time parameter.
    By NewBProgmr in forum C++ Programming
    Replies: 9
    Last Post: 07-16-2009, 05:10 PM
  3. Vector vs. array.
    By matsp in forum C++ Programming
    Replies: 37
    Last Post: 06-23-2008, 12:41 PM
  4. Delay
    By CanadianOutlaw in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 06:28 AM