How to create a delay function in Keil uVision?

Yi Xu Chee picture Yi Xu Chee · Feb 4, 2014 · Viewed 11.3k times · Source

Arduino has a delay(ms) function to pause the program for a certain amount of time. It is very accurate in milliseconds.

I have a delay function in C used in Keil uVision for the AT89C5131 microcontroller:

void delay( unsigned long duration)
{
    while ( ( duration -- )!= 0);
}

This does some delay job but the long value is not accurate like Arduino.

Is there a way to create a function that works like delay() function in Arduino?

The crystal is running at 24Mhz.

Answer

Felipe Lavratti picture Felipe Lavratti · Feb 4, 2014

If you want to do busy wait, this is how it's done in Keil:

#pragma O0
void wait(volatile uint32_t cnt) {
    while(cnt--)
        _nop_();
}

http://www.keil.com/support/docs/606.htm