Thread: How to determin a delay

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    6

    How to determin a delay

    How would i determine how long it takes to complete an instruction?

    for the below function i can pass an unsigned char "count" so i can adjust the delay ….so lets say I pass 1 how long would it take to complete both for loops especially the second where j exceeds 112 and exits the for loop ?

    Code:
    void delay(unsigned int count)  
    {
     int i,j;
     for(i=0;i<count;i++)
     for(j=0;j<112;j++);
    }
    Last edited by OutThere; 10-18-2020 at 12:29 PM.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    126
    In this case, delay will take no time at all - all good compilers will see that the code does nothing, and optimize it away. What platform are you targeting? If Linux, usleep() might be what you need.

    Maybe if you added an (non-portable) inline assembly instruction to the loop you might get some form of delay. If you do want to count clock cycles to give short pauses you would most likely be better off coding the function in assembler.

    When I have needed to time precise delays, I either call the function a million times, and time the overall program (e.g. so if it takes 10s to run, each call takes 10 microseconds), or if this is a microcontroller project I hook a logic analyzer to the hardware, and measure it that way.
    Last edited by hamster_nz; 10-18-2020 at 02:09 PM.

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    6
    it's for an embedded microcontroller to interface w/ an LCD. When the LCD is powered on it takes 10ms to initialize where no instructions can be written. Durring this time the Line D7 is high then goes low after 10ms. So my choices are to either make a 10ms delay or read D7 for when it goes low.

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    126
    Does the environment you are using have a 'millis()' (or equivalent) function you can call, to monitor a timer?

    millis() - Arduino Reference

    Code:
       end_time = millis()+11;
       while(millis() != end_time) 
          ;  // do nothing
    Or maybe better (due to timer rollover and the possibility of long interrupts)

    Code:
       start_time = millis();
       while(millis() - start_time <= 10) 
          ;  // do nothing
    Last edited by hamster_nz; 10-18-2020 at 07:05 PM.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    3,920
    You should read up on the timer and delay support for your embedded hardware! This is one area that changes a lot from one hardware platform to another.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to add a delay??
    By shifu in forum C Programming
    Replies: 8
    Last Post: 02-05-2010, 01:22 PM
  2. how to delay
    By llinocoe in forum C Programming
    Replies: 1
    Last Post: 09-22-2008, 06:09 AM
  3. Determin compiler with preprocessor
    By xErath in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2004, 06:56 PM
  4. Delay
    By Perica in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2002, 10:45 PM
  5. Delay
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2002, 12:53 PM

Tags for this Thread