Thread: creating delay

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    creating delay

    for(i=0; i<=15; i++){
    //do nothing
    }

    How many processor cycles is this delay?
    Is there a more efficient way to create a delay in my embedded system?

    2) I want a delay of 1000 ns. My processor runs at 12Mhz.
    How many steps sould i create in the for loop?

  2. #2
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    im pretty sure there is a way

    #include <windows.h>
    sleep(/*number of ms*/);

    if that doesnt work try looking for it(the sleep thing) on msdn
    guns dont kill people, abortion clinics kill people.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005

    Re: creating delay

    >How many processor cycles is this delay?
    It will depend on what i is. Take a look at the generated assembly. For example, this is what I get with an int i.
    Code:
    stmt level    source
    
       1          void main(void)
       2          {
       3   1          int i;
       4   1          for(i = 0; i < 16; ++i)
       5   1          {
       6   2          }
       7   1      }
    
                 ; FUNCTION main (BEGIN)
                                               ; SOURCE LINE # 1
                                               ; SOURCE LINE # 2
                                               ; SOURCE LINE # 4
    ;---- Variable 'i' assigned to Register 'R6/R7' ----
    0000 E4            CLR     A
    0001 FF            MOV     R7,A
    0002 FE            MOV     R6,A
    0003         ?C0001:
                                               ; SOURCE LINE # 5
                                               ; SOURCE LINE # 6
    0003 0F            INC     R7
    0004 BF0001        CJNE    R7,#00H,?C0005
    0007 0E            INC     R6
    0008         ?C0005:
    0008 EF            MOV     A,R7
    0009 6410          XRL     A,#010H
    000B 4E            ORL     A,R6
    000C 70F5          JNZ     ?C0001
                                               ; SOURCE LINE # 7
    000E         ?C0004:
    000E 22            RET     
                 ; FUNCTION main (END)
    Now comes the fun of looking up the instruction cycle times.

    >Is there a more efficient way to create a delay in my embedded system?
    Timer interrupts come to mind. You might find some useful information here.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding delay in the connection
    By byatin in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-19-2008, 02:59 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  4. Networking (queuing delay, avg packet loss)
    By spoon_ in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-05-2005, 11:23 AM
  5. Delay
    By CanadianOutlaw in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 06:28 AM