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

How use timer to delay in a function?

A delay function is defined normally as:

void delay(int n)
{
   int i;
   for (i= 0; i<n; i++)
   _nop_();
}


when delay() performed,ALU get in loop but do nothing.Can use a timer counting to delay and release ALU to do something ?For example ,after input commands to erase I28F128 flash,enable timer0 to counting and program go do something else,when counting to 5's(flash is erased ),program go back and begin to write data to flash.

Best regards

Parents
  • A delay function is defined normally as:

    Actually, no. Delay functions are "normally" not defined or used at all, and for precisely the reason you just described: they hog the CPU and keep it from doing useful things in the meantime. The only exception are delays that are so short that a) it doesn't hurt to hog the CPU for their duration, and b) they can't be implemented other than by busy-waiting anyway.

    Proper delays are implemented as timers. Either by polling a free-running timer to see when the requisite time has passed, or by setting a timer interrupt (think "alarm clock") that signals completion of the waiting time.

Reply
  • A delay function is defined normally as:

    Actually, no. Delay functions are "normally" not defined or used at all, and for precisely the reason you just described: they hog the CPU and keep it from doing useful things in the meantime. The only exception are delays that are so short that a) it doesn't hurt to hog the CPU for their duration, and b) they can't be implemented other than by busy-waiting anyway.

    Proper delays are implemented as timers. Either by polling a free-running timer to see when the requisite time has passed, or by setting a timer interrupt (think "alarm clock") that signals completion of the waiting time.

Children