Delay function in C using P89c51RD2HBP

How to calculate the exact delay without the use of the timer?

For example if i want a 10ms delay, i need to achieve this delay using the for loop not with help of the in built timer.

Is there any formula or calculation to do the delay?

I dont know how to get it. I am using P89c51RD2HBP microcontroller.

Currently i am using a random delay which is worst case delay.

Anybody can say how to do the correct delay using the loop method.

Parents
  • There have been a number of threads in the forum that go into detail on the subject. I'd suggest searching the Knowledgebase for old threads.

    In short, you can count instruction execution cycles and execute code that takes a known amount of time in a loop. (To be precise, you also need to count the time it takes for the loop overhead.)

    This sort of code is best done in assembly so that you can maintain control over the exact sequence of instruction and therefore timing. If you write a timing loop in C, then you can't be sure that the compiler will always generate exactly the same code from release to release, or optimization to optimization.

Reply
  • There have been a number of threads in the forum that go into detail on the subject. I'd suggest searching the Knowledgebase for old threads.

    In short, you can count instruction execution cycles and execute code that takes a known amount of time in a loop. (To be precise, you also need to count the time it takes for the loop overhead.)

    This sort of code is best done in assembly so that you can maintain control over the exact sequence of instruction and therefore timing. If you write a timing loop in C, then you can't be sure that the compiler will always generate exactly the same code from release to release, or optimization to optimization.

Children
More questions in this forum