Searching \ for '[PIC]: Delays got me beat' 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: 'Delays got me beat'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Delays got me beat'
2000\05\26@040258 by Tim Hamel

picon face
Hi Friends,

I'm once again at everyone's mercy. I usually try to exhaust all my resources
and fight with something before posting a problem to the list; but I'm stuck.
I consider myself pretty handy with PICs, but this topic has me buried in
books and sites.

I can't, for the life of me look at a piece of delay code and tell you how
long it delays for. I've tried breaking it down, reading over David Benson's
piece about it over and over, but still have a big "?" on my forehead. The
simple one-shot delays I can figure, but this gets me:

From David Benson's "Easy PIC'n," it's been tweaked a bit:

        Movlw "value"
        Movwf  M
Pause Movlw  "value2"
         Movwf  N

Delay1 Decfsz N
        goto Delay1
        decfsz  M
        goto Pause
        return

Ok, for the finale, he gives the formula for the delay as "3MN," how!?  Could
some kind soul break down into bits and pieces so I could figure it out? I'm
going to be doing a weather project, and I'm sure I'll need this knowledge.
Hopefully, I'll be able to complete the project without being dependant on
you guys =)

Much thanks in advance,

Tim Hamel

2000\05\26@072615 by Scott Dattalo

face
flavicon
face
On Fri, 26 May 2000, Tim Hamel wrote:

{Quote hidden}

Sean breaks this down on his site. Perhaps he'll elaborate.


First, there are two nested loops. The inner one:

loop_inner
  decfsz  some_reg,f
   goto   loop_inner

The time it takes for this loop to execute depends on the contents of
"some_reg":

 some_reg   loop time
-----------------------
    1           2
    2           5
    3           8
    x           2 + 3*(x-1)   1 <= x <= 255

This says that it takes three cycles for each pass through the loop except for
the very last one which only takes two cycles.

x=0 is a special case. The first time through the loop, x will decrement to 255.
So what happens is that the maximum delay time occurs for x=0. And in fact, the
time for the loop to execute is 2 + 3*(256-1) cycles. So for x=0 you substitute
x=256 and the general formula:

 loop_time = 2 + 3*(x-1)

still applies.

The outer loop can be abstracted like so:

loop_outer:

  <some pic code>

  decfsz  some_other_reg,f
   goto   loop_outer

In other words, it's structurally identically to the inner loop with some extra
code in the loop. Let's say that the time for this extra code to execute is
'Q'. Then the total time for the outer loop is:

some_other_reg   loop time
-----------------------------
     1             Q + 2
     2             2*Q + 5
     3             3*Q + 8
     y             y*Q + 2 + 3*(y-1)   1 <= y <= 255

The same caveat for y = 0 applies. This equation can be simplified:

outer_loop_time = y*(Q+3) + 2 - 3
               = y*(Q+3) - 1

Now to combine the two loops, all we have to do is find the value for Q. This is
the time it take for this snippet to execute:

> Pause Movlw  "value2"
>           Movwf  N
>
> Delay1 Decfsz N
>          goto Delay1

There are two cycles for initializing N and then there's the loop. So the total
time is:

 Q = 2 + (2 + 3*(N-1))     , 1 <= N <= 255
   = 4 + 3*N - 3
   = 1 + 3*N

The total time for both loops is:

 total = M*(Q+3) - 1     , 1 <= M <= 255
       = M*(1+3*N +3) - 1
       = 3*M*N + 4*M - 1

(keep in mind, if M or N is 0 then we need to replace the 0 with 256)

Benson is wrong.

Scott

> some kind soul break down into bits and pieces so I could figure it out? I'm
> going to be doing a weather project, and I'm sure I'll need this knowledge.
> Hopefully, I'll be able to complete the project without being dependant on
> you guys =)

Is this why it's so hard to predict the weather?

2000\05\26@112744 by pandersn

flavicon
face
Just a thought. Why not use one of the timers for an exact interrupt delay
rather than working with code loops? Example: use TMR2 and set pre- post-
and main counters so that you get the delay you want, then interrupt or
sample on that...count it in a routine.

Hope that is helpful.....

Phil

On Friday, May 26, 2000 3:02 AM, Tim Hamel [SMTP:spam_OUTTekPhobiaTakeThisOuTspamAOL.COM] wrote:
> Hi Friends,
>
> I'm once again at everyone's mercy. I usually try to exhaust all my
resources
> and fight with something before posting a problem to the list; but I'm
stuck.
> I consider myself pretty handy with PICs, but this topic has me buried in
> books and sites.
>
> I can't, for the life of me look at a piece of delay code and tell you
how
> long it delays for. I've tried breaking it down, reading over David
Benson's
> piece about it over and over, but still have a big "?" on my forehead.
The
{Quote hidden}

Could
> some kind soul break down into bits and pieces so I could figure it out?
I'm
> going to be doing a weather project, and I'm sure I'll need this
knowledge.
> Hopefully, I'll be able to complete the project without being dependant
on
> you guys =)
>
> Much thanks in advance,
>
> Tim Hamel

2000\05\27@173336 by Sean Breheny

face picon face
Hi Tim and Scott,

As Scott says, I have information about PIC loops on my page at

http://www.people.cornell.edu/pages/shb7/lop.html

I recently found out that the section on a certain type of loop (called N2
on my page) was incorrect, so I just corrected that, and added some great
suggestions from Scott and Reggie Burdin. Long overdue update of that page!

It looks like you did a better job of explaining this than I could do,
Scott. The only thing is that I get 3*M*N + 4*M + 1, instead of -1 at the
end. I think you may have forgotten the two instructions at the very top of
the loop, but it is possible that I could be the one making the mistake.

Sean


At 06:24 AM 5/26/00 -0500, you wrote:
{Quote hidden}

for
>the very last one which only takes two cycles.
>
>x=0 is a special case. The first time through the loop, x will decrement
to 255.
>So what happens is that the maximum delay time occurs for x=0. And in
fact, the
>time for the loop to execute is 2 + 3*(256-1) cycles. So for x=0 you
substitute
{Quote hidden}

extra
{Quote hidden}

This is
>the time it take for this snippet to execute:
>
>> Pause Movlw  "value2"
>>           Movwf  N
>>
>> Delay1 Decfsz N
>>          goto Delay1
>
>There are two cycles for initializing N and then there's the loop. So the
total
{Quote hidden}

| Sean Breheny
| Amateur Radio Callsign: KA3YXM
| Electrical Engineering Student
\--------------=----------------
Save lives, please look at http://www.all.org
Personal page: http://www.people.cornell.edu/pages/shb7
.....shb7KILLspamspam@spam@cornell.edu ICQ #: 3329174
_____________________________________________
NetZero - Defenders of the Free World
Click here for FREE Internet Access and Email
http://www.netzero.net/download/index.html

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