Thank you everyone for your help. I am trying to implement it now. Thanks
again
thomas
----- Original Message -----
From: "Dwayne Reid" <.....dwaynerKILLspam.....PLANET.EON.NET>
To: <EraseMEPICLISTspam_OUTTakeThisOuTMITVMA.MIT.EDU>
Sent: Sunday, July 07, 2002 1:57 PM
Subject: Re: [PIC]: Delay loop calculation
> At 09:40 AM 7/7/02 -0700, Thomas N wrote:
> >Hello everyone,
> >
> >I think many people use the nested loop as below to code the delay time.
Is
> >there a formula to calculate Delay1 & Delay2 variables given a number of
> >clock cycle? What I am doing right now is just "trial and error" until I
> >get the desired delay time. How do I calculate or approximate the value
of
> >Delay1 & Delay2 variables?
>
> Simple loops like you have here are easy to figure out. Start with the
> most inner loop: the one at label DELAY1ms2. Nice and simple: there are
> only 2 instructions in the loop. Each pass through the loop requires 3
> clock cycles except for the last loop. Observe: decfsz (1 cycle)
> decrements the register, the 'goto' requires 2 cycles. Total: 3
> cycles. Now look what happens when the loop is supposed to terminate.
The
{Quote hidden}> decfsz test is now true - the following 'goto' is now replaced with a
> NOP. This loop takes 2 cycles: 1 cycle less than all the other
> loops. Recognizing this, the easy method is to just figure out how many
> cycles all the loops would take if they were all 3 cycles long, then
> subtract 1 for the final loop.
>
> The outer loop is only slightly more complicated. Count how many cycles
> are consumed within the loop, multiply by the number of times the loop
> executes, subtract 1 for the last loop termination. Now add any
> instructions that are outside of the loop.
>
> I've added some comments to your original code:
>
> DELAY1MS
> MOVLW D'3'
> MOVWF Delay1
>
> DELAY1ms1
> MOVLW D'109'
> MOVWF Delay2
>
> DELAY1ms2
> DECFSZ Delay2,F ;loop takes 3 cycles for each
> GOTO DELAY1ms2 ; except for the last which is 2
> ;based upon the D'109' above, that is (109 * 3) - 1 = 326 cycles
>
> ;this loop delays the above time PLUS 2 cycles at label DELAY1ms1
> ; PLUS 3 cycles for the stuff below all times number at DELAY1MS,
> ; less 1 for last pass through the loop
> DECFSZ Delay1,F
> GOTO DELAY1ms1 ;
>
> ;so delay time is: ((326 + 2 + 3) * 3) -1 = (331*3) -1 = 992
> ;now add the 2 instructions at label DELAY1MS: 992 + 2 = 994
> ;now add call and return times: 994 + 2 + 2 = 998 cycles
>
> RETURN
>
> Finding the numbers in the first place is not much harder: just work
> backwards from the description I've just given. In other words, write the
> code first so that you have a framework. Then subtract the total number
of
> cycles that are executed only once. Now you know how long the total loop
> time has to be. You now have to break that up into the inner loop time
and
{Quote hidden}> outer loop time - that is largely trial and error but easy to do and takes
> very little effort. Then pad with NOPs as required if the time has to be
> exact.
>
> Or you can use some of the readily available tools out there to figure out
> the loop constants for you. The one at PICLIST.COM works well. But you
> should know how to figure it out yourself for those times when you are out
> making changes at a client's site and do not have an internet connection
> available.
>
> dwayne
>
> --
> Dwayne Reid <
dwaynerspam_OUTplanet.eon.net>
> Trinity Electronics Systems Ltd Edmonton, AB, CANADA
> (780) 489-3199 voice (780) 487-6397 fax
>
> Celebrating 18 years of Engineering Innovation (1984 - 2002)
> .-. .-. .-. .-. .-. .-. .-. .-. .-. .-
> `-' `-' `-' `-' `-' `-' `-' `-' `-'
> Do NOT send unsolicited commercial email to this email address.
> This message neither grants consent to receive unsolicited
> commercial email nor is intended to solicit commercial email.
>
> --
>
http://www.piclist.com hint: The list server can filter out subtopics
> (like ads or off topics) for you. See
http://www.piclist.com/#topics
>
--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics