One could also use a TIMER. Pre-load the timer and pre-scaler and turn it on and let it start counting. When the timer reaches its limit it will generate a interupt. Just like Kens solution, you would have to generate the numbers used for the delay by knowing the oscillator and then dividing it down accordingly. Both methods work, both have their pros and cons. It may depend on why you need the 5mS delay as to what method is better. Ken Pergola wrote: >Wan Zulhelmi Wan Ahmad Kamar wrote: > > > >>What I meant by exact time means that for example >>I want 5 mS of delay, how would I make sure my >>loop will only delay exactly for 5 mS and no >>more or less than that. >> >> > >Hi Wan, > >It depends on the frequency (Fosc) that the PIC is running at and the number >of clock cycles (always an integer value) your delay routine uses -- >sometimes you will nail the time delay perfectly (for all practical >purposes) and sometimes you won't (but it's close enough). If you are using >a subroutine, you might also need to account for the overhead of the CALL >and RETURN instructions -- it all depends on how precise you need things. > >Another thing to consider is that if you are using interrupts and they are >enabled, they will preempt your software delay if any interrupts occur >during the software delay routine -- this will extend the time of software >delay beyond what you originally calculated it to be. > > >Back to your original question: > >To generate a 5 mS delay, you need to code a delay to execute the following >number of clock cycles: > > Fosc > ---- * 5 mS > 4 > >And like I said before, you might want to account for the 2-cycle overhead >of the CALL and the 2-cycle overhead of the RETURN instruction if you are >using a subroutine for this delay (as opposed to in-line code). > >There are utilities that people have written will aid you in this process. >If I remember correctly, Andrew Warren had a QuickBasic program for helping >one with software delays many years ago (pre-PIC18). There's probably a slew >of other utilities as well that others have written. You might want to >search the PICList website (www.PICList.com) -- you should be able to find >some good information there to help you out. > _______________________________________________ http://www.piclist.com View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist