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
  • Is this even possible?

    Yep. I do this kind of thing all the time.

    I'd guess I will need to write a delay loop in assembly and call it from C correct?

    Why? There's no need to do that. You can do everything in C.

    You should create a separate C file for each function. For example:


    DELAY.C

    void Delay(void)
    {
    /* insert code here */
    }
    

    DELAY_MS.C

    #include <intrins.h>
    
    void DelayMS(unsigned char ms)
    {
    unsigned long us = 1000*ms;
    
    while (us--)
      {
      _nop_();
      }
    }

    DELAY_US.C

    #include <intrins.h>
    
    void DelayUS(unsigned char us)
    {
    while (us--)
      {
      _nop_();
      }
    }

    Then, compile the files into .OBJ files. Then, use LIB51 to build a library. Finally (or maybe as you go), create a header file for the routines in the library. You may even include the library header file in the library source files as a double-check of function prototypes.

    You can, of course, create a uVision2 project to create a library.

    Obviously, these routines must be tuned.

    Jon

Reply
  • Is this even possible?

    Yep. I do this kind of thing all the time.

    I'd guess I will need to write a delay loop in assembly and call it from C correct?

    Why? There's no need to do that. You can do everything in C.

    You should create a separate C file for each function. For example:


    DELAY.C

    void Delay(void)
    {
    /* insert code here */
    }
    

    DELAY_MS.C

    #include <intrins.h>
    
    void DelayMS(unsigned char ms)
    {
    unsigned long us = 1000*ms;
    
    while (us--)
      {
      _nop_();
      }
    }

    DELAY_US.C

    #include <intrins.h>
    
    void DelayUS(unsigned char us)
    {
    while (us--)
      {
      _nop_();
      }
    }

    Then, compile the files into .OBJ files. Then, use LIB51 to build a library. Finally (or maybe as you go), create a header file for the routines in the library. You may even include the library header file in the library source files as a double-check of function prototypes.

    You can, of course, create a uVision2 project to create a library.

    Obviously, these routines must be tuned.

    Jon

Children