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

Creating a not-so-portable Delay library

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()

These will be used as follows (examples):

Delay(2);        // creates a 2 second delay
DelayMS(500);    // creates 500ms delay
DelayUS(10);     // creates a 10us delay

I realize a timer could be used, but I'd like to have a few generic approximated delay functions available that do not occupy a timer module.

Is this even possible? I realize exact accuracy would not be possible. But a 502ms delay (or even as much as 10ms off) would be sufficient for my needs.

I'd guess I will need to write a delay loop in assembly and call it from C correct? And of course if a controller or clock got changed, i'd have to update my library..

John

Parents
  • 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)
    
    What would be really useful is to be able to make the source code sensitive to the target options such as crystal speed (by means of a predefined macro constant) so that the same code could be used again and again for different projects.

    Of course, the compiler would also have to know how many clock cycles are required for a NOP - since the simulator needs to know this for timeing, presumably this information is available somewhere within Keil.

Reply
  • 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)
    
    What would be really useful is to be able to make the source code sensitive to the target options such as crystal speed (by means of a predefined macro constant) so that the same code could be used again and again for different projects.

    Of course, the compiler would also have to know how many clock cycles are required for a NOP - since the simulator needs to know this for timeing, presumably this information is available somewhere within Keil.

Children