This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Delay Loop in C51

Dear Friends,
How to create a delay loop in C51.Say for example I need a delay loop for 35mS.How to do this.I tried a code taken from this forum.It is
void delay(void)
{
unsigned long msdl;
msdl = 35L * 1000L;
while(--msdl)
{
_nop_();
}
}
Logically I think it should produce a 35mS delay code.But When I see in the Performance Analyzer window the delay produced is 0.547275 seconds.
Can anybody help me in solving this problem.
Thanx in Advance

Parents
  • Dear Friends, Thank you all for the useful reply.I am a beginner to this KEIL.I dont know how to get a delay loop for the 35ms.I dont need a accurate value.I just want to use this delay for button debouncing.Please guide me to generate a delay loop in c51.Sorry for stealing your valuable time.

Reply
  • Dear Friends, Thank you all for the useful reply.I am a beginner to this KEIL.I dont know how to get a delay loop for the 35ms.I dont need a accurate value.I just want to use this delay for button debouncing.Please guide me to generate a delay loop in c51.Sorry for stealing your valuable time.

Children
  • "I am a beginner to this KEIL"

    None of this is specific to Keil; you cannot directly predict the execution time of any 'C' source line compiled with any 'C' compiler for any target!

    "Please guide me to generate a delay loop in c51"

    Please re-read all the preceding replies: You cannot predict the execution time of any 'C' source line!
    Therefore, trying to create a known delay in 'C' source code is folly!

    You need to write the loop in assembler: then you know exactly what machine instructions will be used, and you can calculate exactly how long they will take to execute.

    The timing details for each instruction will be given in your processor Datasheet. The times will be given in terms of Clock Cycles, so you will also need to know your clock frequency.

    To easily make an assembler function callable from C51, proceed as follows:

    1. Write a "skeleton" of the function in 'C'; eg,

    void wait_35ms( void )
    {
       // Nothing!
    }
    2. Compile this using the SRC directive (see the Manual) - the compiler will generate an assembler source file that corresponds to your skeleton 'C' function.
    (because the compiler has generated it, you know that it's done all the right things to make it callable from 'C'!)

    3. Throw the 'C' source file away!
    It has served its purpose now; it is of no further value.

    4. Write a suitable loop in assembler to give the required delay.
    (In general, you must remember to allow for the overhead of calling the function - although this is unlikely to be significant for a 35ms delay!)

    5. Add the assembler file to your project, and just call the wait_35ms() function wherever required!

  • "I just want to use this delay for button debouncing"

    I think a timer-based approach would be far better for this application.

    Do a bit of work with your favourite internet search engine, and I'm sure you'll find plenty...