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
  • Of course you can. all you need to do is the following:
    * start/reset your hardware timer.
    * poll its value in your main loop.
    * don't take any action until the required delay has elapsed.
    * do whatever you need to do once the delay has elapsed.
    * repeat this procedure whenever required.

    for increased accuracy you might want to have your timer generate an interrupt, but that might not be required at all.

Reply
  • Of course you can. all you need to do is the following:
    * start/reset your hardware timer.
    * poll its value in your main loop.
    * don't take any action until the required delay has elapsed.
    * do whatever you need to do once the delay has elapsed.
    * repeat this procedure whenever required.

    for increased accuracy you might want to have your timer generate an interrupt, but that might not be required at all.

Children
No data