Notes of PhiL    About    Categories    Tags    Archive    Feed    GitHub Repo

A Glance at the Delay Function

Delay function is an important part of a program, especially in embedded systems. Thus, understanding and being capable of programming a delay function is vital for coders, specifically beginners (like me).

The first thing we should know is the formula for the delay time:


DELAY_TIME: how long we want the program to delay (in seconds)
NUMBER_OF_LOOPS: how many loops needed to delay (in loops)
NUMBER_OF_CYCLES_PER_LOOPS: how many cycles the loop needs (in cycles/loop)
TIME_PER_CYCLE: how long it takes to complete one clock cycle (in seconds/cycle)


We set our clock frequency to be 80 MHz, so TIME_PER_CYCLE = 1.25e-8.

However, we’ve already had DELAY_TIME (we’ve decided how long the program will delay), NUMBER_OF_CYCLES_PER_LOOPS (according to the microcontroller’s datasheet, equals 5), and TIME_PER_CYCLE (calculated to be 1.25e-8). As a result, in order to delay 1 milliseconds,

NUMBER_OF_LOOPS = 0.001 / 5 / 1.25e-8 = 16000

Finally, here is my implementation of the Delay() function:

 * Delays in units of milliseconds
 * @param  ms   Number of milliseconds to delay
 * @assumption  80-MHz clock
void Delay(unsigned long ms) {
    unsigned long count;

    while (ms) {
        count = 16000;  // approximately 16000 to delay 1 ms

        while (count) {