-delay time coding make me puzzled

delay time coding make me puzzled.
for example:

void delay()
{
int x=20000;
do{x=x-1;}while(x>1);
}

how to understand it's delay.and how long time???
sincere your in advance!

Parents
  • Please read the instructions immediately above where you typed-in your question - they tell you what to do when posting code.
    Also see the tips for posting messages:
    http://www.keil.com/forum/tips.asp

    When you follow the instructions, your code will retain its formatting, like this:

    void delay()
    {
       int x=20000;
       do
       {
          x=x-1;
       } while(x>1);
    }
    "how to understand it's delay. and how long time???"

    All instructions take a finite time to execute, and a lot of instructions (or some instructions repeated a lot of times in a loop) will take a considerable time to execute.

    However, be warned that the loop as shown may take no time at all to execute - as the compiler is likely to see that it does nothing useful, and optimise it away!
    You should use the volatile keyword in your definition of 'x' to prevent this.

Reply
  • Please read the instructions immediately above where you typed-in your question - they tell you what to do when posting code.
    Also see the tips for posting messages:
    http://www.keil.com/forum/tips.asp

    When you follow the instructions, your code will retain its formatting, like this:

    void delay()
    {
       int x=20000;
       do
       {
          x=x-1;
       } while(x>1);
    }
    "how to understand it's delay. and how long time???"

    All instructions take a finite time to execute, and a lot of instructions (or some instructions repeated a lot of times in a loop) will take a considerable time to execute.

    However, be warned that the loop as shown may take no time at all to execute - as the compiler is likely to see that it does nothing useful, and optimise it away!
    You should use the volatile keyword in your definition of 'x' to prevent this.

Children
More questions in this forum