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
  • Hi,

    2 questions here:

    (1) I tried the nop way as you suggested, how come the delay I get does not change much if I give 'us' 2 very different values?

    (2) I use the following code:

    void delay_us() {
     
        SysTick->VAL = 9000;
     
        while(SysTick->VAL != 0) {}
     
    }
     

    and delay also does not change much for VAL 2000 and 9000.

    Does it have sth to do with code optimization? I am using IDE's default setting.

Reply
  • Hi,

    2 questions here:

    (1) I tried the nop way as you suggested, how come the delay I get does not change much if I give 'us' 2 very different values?

    (2) I use the following code:

    void delay_us() {
     
        SysTick->VAL = 9000;
     
        while(SysTick->VAL != 0) {}
     
    }
     

    and delay also does not change much for VAL 2000 and 9000.

    Does it have sth to do with code optimization? I am using IDE's default setting.

Children
No data