Searching \ for 'Challenge: 16bit delay loop' 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/timers.htm?key=delay
Search entire site for: 'Challenge: 16bit delay loop'.

Truncated match.
PICList Thread
'Challenge: 16bit delay loop'
1999\05\06@204033 by Scott Dattalo

face
flavicon
face
Guys & Alice

It seems like there's been several questions about delay loops in the
last few weeks. So I thought it'd be interesting to see if we could turn
this into a challenge:

Create a delay loop that can loop for up to 2^16 times while mininmizing
the number of cycles per iteration. For example:

 clrf   lo
 clrf   hi

 movlw  1

loop:

 addwf  lo,f    ;increment the low byte
 skpnc          ;if low byte rolls over
  addwf hi,f    ; then increment the high byte

 skpc           ;We're done if the high byte rolls over.
  goto  loop


This will loop 2^16 times and each pass through the loop is 6 cycles.

I've got a solution that takes 5 cycles per iteration and doesn't use W
(oh I shouldn't have said that - now you know the answer!).

Any takers?

Scott

1999\05\06@213532 by Regulus Berdin

picon face
Hi Scott,

How about, 5 cycles per loop, doesn't use W:

       clrf    lo
       clrf    hi

loop    incfsz  lo,f
        goto   $+2
       incfsz  hi,f
        goto   loop

regards,
Reggie

--
e-mail: spam_OUTrberdinTakeThisOuTspambigfoot.com
ICQ#:   31651436
URL:    http://www.bigfoot.com/~rberdin


Scott Dattalo wrote:
{Quote hidden}

1999\05\06@215649 by Sean Breheny

face picon face
Actually, this has an AVERAGE number of cycles per loop of very close to 3,
since the first goto is only skipped 1/256th of the time.

Sean

At 09:42 AM 5/7/99 +0800, you wrote:
{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
shb7spamKILLspamcornell.edu ICQ #: 3329174

1999\05\06@215919 by Sean Breheny

face picon face
Should have included this in the previous message:

In case anyone wants to use this loop, the more general form is:


            movlw x
            movwf xf
            movlw y
            movwf xy
            delay(A)
aloop        delay(B)
            decfsz xf,f
            goto aloop
            delay(C)
            decfsz yf,f
            goto aloop

Which has the timing expression:

total cycles = (770+256*B+C)*Y + (B+3) * X + A + 2

In the case given by Reggie, where A=B=C=0:

total cycles = 770*Y + 3*X + 2

Sean


At 09:42 AM 5/7/99 +0800, you wrote:
{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
EraseMEshb7spam_OUTspamTakeThisOuTcornell.edu ICQ #: 3329174

1999\05\06@221414 by Bob Fehrenbach
picon face
Scott Dattalo wrote:
{Quote hidden}

       clrf    lo
       clrf    hi
loop:
       incf    lo, f
       skpnz
       incfsz  hi, f
       goto    loop

Bob Fehrenbach
Wauwatosa, WI

1999\05\06@221637 by Regulus Berdin

picon face
Hi Sean,

I think this is not the case, my routine has a "goto $+2" not a "goto
loop" after the first increment (to be isochronous to 5 cycles per
loop).  The total cycles should be:

Cycles = (65536 - (hi * 256 + lo)) * 5

Except when hi and lo is zero which is 65536*5 cycles.

regards,
Reggie

--
e-mail: rberdinspamspam_OUTbigfoot.com
ICQ#:   31651436
URL:    http://www.bigfoot.com/~rberdin


Sean Breheny wrote:
{Quote hidden}

1999\05\06@222714 by Dennis Plunkett

flavicon
face
At 21:58 6/05/99 -0400, you wrote:
{Quote hidden}

Perhaps I am being a bit picky here but I think that the equation for total
cycles is wrong
770*7 + 3*X + 2. What happens if X or Y is 0.


Dennis

1999\05\06@224235 by Sean Breheny

face picon face
Hi Reggie,

Doh! Yes, of course, you are right, I was a bit over zealous in my analysis
there!

However, both methods are valid loops (although, mine is not isochronous).
Yours gives a greater maximum number of cycles than mine,too.

Dennis also pointed out that my expression is wrong for the case where Y=0
or X=0. Yes, although you can count Y=0 as Y=256 and X=0 as X=256,and then
it works. I should have mentioned that.

Thanks for the corrections.

Sean

At 10:22 AM 5/7/99 +0800, you wrote:
{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
KILLspamshb7KILLspamspamcornell.edu ICQ #: 3329174

1999\05\07@110015 by Scott Dattalo

face
flavicon
face
On Thu, 6 May 1999, Bob Fehrenbach wrote:

>         clrf    lo
>         clrf    hi
> loop:
>         incf    lo, f
>         skpnz
>         incfsz  hi, f
>         goto    loop

That's exactly my solution. And

On Fri, 7 May 1999, Regulus Berdin wrote:

{Quote hidden}

Yep!

Here's another solution that gives two extra cycles for other stuff
(like CLRWDT or testing an I/O bit)

   clrf   lo
   clrf   hi

loop1

   nop             ;2 free cycles executed 255 out of 256
   nop             ;iterations

loop2

   decfsz  lo,f   ;or incfsz
    goto   loop1
   decfsz  hi,f   ;if low byte rolls over, dec hi byte,
    goto   loop2  ;but don't branch to the beginning.

1999\05\08@193003 by Scott Dattalo

face
flavicon
face
On Fri, 7 May 1999, Scott Dattalo wrote:

> Here's another solution that gives two extra cycles for other stuff
> (like CLRWDT or testing an I/O bit)
>
>     clrf   lo
>     clrf   hi
>
> loop1
>
>     nop             ;2 free cycles executed 255 out of 256
>     nop             ;iterations
>
> loop2
>
>     decfsz  lo,f   ;or incfsz
>      goto   loop1
>     decfsz  hi,f   ;if low byte rolls over, dec hi byte,
>      goto   loop2  ;but don't branch to the beginning.

After a little churning, I found a 3 cycle solution:


   clrf    lo
   clrf    hi

loop

   decfsz  lo,f
    goto   loop

  ;lo is zero right now.

   decf    hi,f
   skpnz
    goto   done      ;or perhaps return

  ;okay, we've gotta lot of choices here
  ;
  ;we could do this:
  ;  incf  lo,f
  ;  nop           ;or clrwdt
  ;  incf  lo,f
  ;  goto  loop
  ;
  ;But I like this:

   bsf   lo,1      ;Equivalent to 2 incf's

   nop             ;Two free cycles
   nop             ;everytime lo rolls over

   goto  loop


  ;Or if you wanted more free cycles:
  ;   bsf   lo,2   ;Equivalent to 4 incf's
  ;   goto  $+1    ;Eight free cycles
  ;   goto  $+1    ;everytime lo rolls
  ;   goto  $+1    ;over
  ;   goto  $+1
  ;
  ;   goto  loop
  ;
  ;Of course, you could preset the lo byte
  ;to any value and then have the remaining
  ;cycles evailable for other stuff.
  ;

Like everything I comment, this untested :).

The time at which the lo byte is incremented does not happen exactly every
third cycle after it rolls over. In fact, some values of lo are skipped
altogether. However, the total number of cycles between the beginning of
the loop and the 'goto done' instruction is 2^16 * 3.

This kind of loop would be useful if you wanted to have a fixed delay AND
still be able to do other things too.

Scott

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