Searching \ for '[PIC]: Delay loop calculation' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/time.htm?key=delay
Search entire site for: 'Delay loop calculation'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Delay loop calculation'
2002\07\07@124159 by Thomas N

picon face
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?

DELAY1MS
       MOVLW   D'3'
       MOVWF   Delay1

DELAY1ms1
       MOVLW   D'109'
       MOVWF   Delay2

DELAY1ms2
       DECFSZ  Delay2,F
       GOTO    DELAY1ms2

       DECFSZ  Delay1,F
       GOTO    DELAY1ms1

       RETURN

--
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


2002\07\07@130741 by Tan Chun Chiek

flavicon
face
try this

http://www.piclist.com/techref/piclist/codegen/delay.htm

----- Original Message -----
From: "Thomas N" <spam_OUTthomasn101TakeThisOuTspamHOTMAIL.COM>
To: <.....PICLISTKILLspamspam@spam@MITVMA.MIT.EDU>
Sent: Monday, July 08, 2002 12:40 AM
Subject: [PIC]: Delay loop calculation


> 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
{Quote hidden}

--
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


2002\07\07@142719 by William Shultz

flavicon
face
Hi Thomas

I have been using this software for calculation. Give it a try.

http://www.mnsi.net/~boucher/picloops.html

Good Luck

Bill N8HKI

{Original Message removed}

2002\07\07@170020 by Dwayne Reid

flavicon
face
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
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
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   <dwaynerspamKILLspamplanet.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


2002\07\07@203647 by Bill & Pookie

picon face
After doing the calculations, is it possible to
use the simulator and set break points
before/after the delay to count clock cycles?

Bill

{Original Message removed}

2002\07\07@211648 by Bob Barr

flavicon
face
On Sun, 7 Jul 2002 17:37:33 -0700, Bill & Pookie wrote:

>After doing the calculations, is it possible to
>use the simulator and set break points
>before/after the delay to count clock cycles?
>

Sure, just open up the stopwatch window and you can get both the time
in seconds and the number of machine cycles. :=)

You don't even really need to set breakpoints. You can step to your
routine's call, clear the stopwatch, do a 'step-over' to run the delay
routine, and just read the time when it returns.


Regards, Bob

--
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


2002\07\07@235448 by Thomas N

picon face
Thank you everyone for your help.  I am trying to implement it now.  Thanks
again
thomas
----- Original Message -----
From: "Dwayne Reid" <.....dwaynerKILLspamspam.....PLANET.EON.NET>
To: <EraseMEPICLISTspam_OUTspamTakeThisOuTMITVMA.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}

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}

--
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


2002\07\08@081921 by Olin Lathrop

face picon face
> After doing the calculations, is it possible to
> use the simulator and set break points
> before/after the delay to count clock cycles?

Yes, use the "stopwatch" feature.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


More... (looser matching)
- Last day of these posts
- In 2002 , 2003 only
- Today
- New search...