DELAY uS RCM6700

Hi, how I can make delays about milliseconds (us)? I am using an RCM6700 with Dynamic C 10.72

I use either a costate or a blocking function, depending on the circumstance, for millisecond (ms) delays. Preferably the multitasking costate so that other processing can happen during the wait:


    costate progress_Task always_on {
        while (updating) {  // update the bar graph until the update reboots
          waitfor (DelayMs(100));
          hBar (3, 0, 159, 0.0, 10000.0, (float) progress);
          }
        }


/* START FUNCTION DESCRIPTION **************************************************
delay_ms                                                           
SYNTAX: void delay_ms (long dur);

DESCRIPTION: Delay loop

PARAMETER1: long dur -  number of milliseconds to delay

RETURN VALUE: None

END DESCRIPTION ***************************************************************/

__nodebug void delay_ms (long dur) {
int i;
int n =0;

  while (dur--)
    for (i=0; i<200; i++)
      n ^= i;
  }

The loop length is already tuned for the clock speed of an RCM6750.

I use a small, blocking function for microsecond (us) delays. I specify root so it doesn’t get bumped to extended memory where it might run more slowly. Spin(15); gives me about 10 microseconds on an RCM6750.


/* START FUNCTION DESCRIPTION **************************************************
spin                                                               

SYNTAX: void spin (int revs);
DESCRIPTION: Variable delay nop loop

PARAMETER1: revs, number of times through the loop
RETURN VALUE: None

SEE ALSO:
KEYWORDS:

END DESCRIPTION ***************************************************************/

nodebug root void spin (int revs) {
int i;
  for (i=0; i
1 Like

You don’t have to use costates just to use the DelayMS() function. There is a similar function that can be used outside of costates that is based on the MS_TIMER macro:

void msDelay(unsigned int delay)
{
auto unsigned long done_time;

done_time = MS_TIMER + delay;
while( (long) (MS_TIMER - done_time) < 0 );
}

1 Like

And to delays in microseconds? (us)