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
  • Yes, you can use the timer to count time in the background:

    start_long_operation();
    start_timer();
    while (!timer_end) {
        do_something();
    }
    

    Programs that does not make use of an RTOS normally has a super-loop that constantly scans inputs, timer flags, ...

    They may perform too much concurrent operations to be able to use a timer for each of them, so instead they may have a single timer that ticks time. Let's say in 10ms steps:

    extern unsigned current_ticks;
    unsigned flash_start_time;
    unsigned char flash_op;
    for (;;) {
        if (flash_op != FLASH_IDLE) {
            switch (flash_op) {
                case FLASH_ERASE:
                    start_flash_erase();
                    flash_start_time = current_ticks;
                    flash_op = FLASH_ERASE_WAIT;
                    break;
                case FLASH_ERASE_WAIT:
                    if ((current_ticks - flash_start_time) > FLASH_ERASE_TIME) {
                        do_flash_job();
                        flash_op = FLASH_NEXT_STATE;
                    }
                    break;
                case FLASH_NEXT_STATE:
                    ...
            }
        }
        if (other_job_needed) {
            do_other_job();
        }
        if (yet_another_job_needed) {
            do_yet_another_job();
        }
        if ((other_timer - current_ticks) > OTHER_INTERVAL) {
            do_other_timed_job();
            other_timer = current_ticks;
        }
    }
    

Reply
  • Yes, you can use the timer to count time in the background:

    start_long_operation();
    start_timer();
    while (!timer_end) {
        do_something();
    }
    

    Programs that does not make use of an RTOS normally has a super-loop that constantly scans inputs, timer flags, ...

    They may perform too much concurrent operations to be able to use a timer for each of them, so instead they may have a single timer that ticks time. Let's say in 10ms steps:

    extern unsigned current_ticks;
    unsigned flash_start_time;
    unsigned char flash_op;
    for (;;) {
        if (flash_op != FLASH_IDLE) {
            switch (flash_op) {
                case FLASH_ERASE:
                    start_flash_erase();
                    flash_start_time = current_ticks;
                    flash_op = FLASH_ERASE_WAIT;
                    break;
                case FLASH_ERASE_WAIT:
                    if ((current_ticks - flash_start_time) > FLASH_ERASE_TIME) {
                        do_flash_job();
                        flash_op = FLASH_NEXT_STATE;
                    }
                    break;
                case FLASH_NEXT_STATE:
                    ...
            }
        }
        if (other_job_needed) {
            do_other_job();
        }
        if (yet_another_job_needed) {
            do_yet_another_job();
        }
        if ((other_timer - current_ticks) > OTHER_INTERVAL) {
            do_other_timed_job();
            other_timer = current_ticks;
        }
    }
    

Children
No data