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!).
>
> 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
>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
>
>
>email: .....rberdinKILLspam@spam@bigfoot.com
>ICQ#: 31651436
>URL: http://www.bigfoot.com/~rberdin
>
>
>Scott Dattalo wrote:
>>
>> 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
>

>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
>
>
>email: .....rberdinKILLspam.....bigfoot.com
>ICQ#: 31651436
>URL: http://www.bigfoot.com/~rberdin
>
>
>Scott Dattalo wrote:
>>
>> 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
>

>
> 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
clrf lo
clrf hi
loop:
incf lo, f
skpnz
incfsz hi, f
goto loop
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.
> 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:
> >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
>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
>
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.
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.
>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
>
>
>email: @spam@rberdinKILLspambigfoot.com
>ICQ#: 31651436
>URL: http://www.bigfoot.com/~rberdin
>
>
>Sean Breheny wrote:
>> 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:
>> >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
>

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