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

delay of the order of micro second

i am writing a code(in C language) in which i need a delay of the order of few micro seconds. and this delay is not constant, i.e., i am using a variable and i should get a delay of value equal to that variable and it would be of the order of micro seconds.. the minimum value of that variable is 0.5 and max is 150,i.e., the delay would have to be changing from 0.5 microsec to 150 microsec. the inbuilt functions in the AVR compilers are not giving such short delays. i have no knowledge about keil. can any one suggest me a microcontroller for the delay of this order and also, is there any inbuilt function in keil to give such delays?If yes, then what is it? if not then how to get such delays?

Parents
  • depending on the processor, you don't necessarily have to subtract the current time from latest timer counter;
    this loop for example wait for 2 milliseconds on an ARM7 at 72 MHz with a clock input of 18MHz. it waits for the hardware timer to automatically stop when the timeout elapses. changing the constant 36363 yields shorter/longer delays.

    // 2 millisecond delay
    
            // use T1, MR1 for delays
            T1TCR = 2 ; // stop and reset timer
            T1MCR = 0x20 ; // stop timer when value in MR1 reached
            T1MR1 = T1TC + 36363 ;
            T1TCR = 1 ; // start the timer
    
            while ( (T1TCR & 1) == 1) ;
    

Reply
  • depending on the processor, you don't necessarily have to subtract the current time from latest timer counter;
    this loop for example wait for 2 milliseconds on an ARM7 at 72 MHz with a clock input of 18MHz. it waits for the hardware timer to automatically stop when the timeout elapses. changing the constant 36363 yields shorter/longer delays.

    // 2 millisecond delay
    
            // use T1, MR1 for delays
            T1TCR = 2 ; // stop and reset timer
            T1MCR = 0x20 ; // stop timer when value in MR1 reached
            T1MR1 = T1TC + 36363 ;
            T1TCR = 1 ; // start the timer
    
            while ( (T1TCR & 1) == 1) ;
    

Children
  • Yes, but the question is if the setup code wouldn't increase the initial setup time and affect the minimum MIPS required to correctly handle the 0.5us case.

    It also makes sure that the timer can't be used for anything else in other parts of the application. If using an arm with 32-bit timers, a free-running timer at 18MHz would turn around every 238 seconds so it would be suitable for other parts of the application that may need up to a few minutes delay.