Hello, I'm rather new to Keil C and just getting geared up to use it on my next project. ATM, I'm overwhelmed with the number of options and extensions that are available. I intend to write a C "library" that consists of 3 functions:
Delay() DelayMS() DelayUS()
Delay(2); // creates a 2 second delay DelayMS(500); // creates 500ms delay DelayUS(10); // creates a 10us delay
Just for the sake of variety: If you have a timer running, you can read the timer value to implement your delay loop. You don't have to set a timer value and code up an interrupt handler, or interfere with the existing use of the timer for another purpose. It's sufficient to read the timer, calculate your desired end value, and just spin while polling the timer value until the timer gets to the end. As with anything time-related, this is easiest to code if you know your delay time will be less than half the maximum range of the timer value.
Some time ago Jon Ward posted this macro here: http://www.keil.com/forum/docs/thread1940.asp You may find it useful for short delays:
#define NOPS(x) \ (((x) & 1) ? _nop_() : 0),\ (((x) & 2) ? _nop_(),_nop_() : 0),\ (((x) & 4) ? _nop_(),_nop_(),_nop_(),_nop_() : 0),\ (((x) & 8) ? _nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_() : 0)
Oh dear! My post did not do that on preview!