jamesr
Newbie level 6
- Joined
- Dec 4, 2013
- Messages
- 14
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 136
Hi,
I have looked around at Delay examples and many of the forums suggest using a nested loop to create large delays. Most of the forums point to a Delay generator tool which creates the code for you based on the clock freq and delay. This is great for implementation but for those of us learning how that works it's not as these generators seldom explain the theory. I am attempting to understand it myself so please correct my workings out where necessary so I can get an idea of where I may be going wrong.
I used the generator to create a 1 sec delay for a 20mhz clock and this is what I got;
Now my question is around how this gets to 4999993 cycles. Based on this being for a PIC device, I understand that a 20Mhz clock / 4 gives 5Million cycles per sec, so the figure below is near enough.
Each loop takes 7 cycles (from my working out).
The first counter will give;
(44 * 7) -5 = 303 - The minus 5 is for the incomplete loop as only 2 cycles have occurred on the last loop when cnt1=0
Second counter gives;
(255 * 231 * 7) -4 = 412,335 - The minus 3 is for the incomplete loop as 4 have executed on the last loop when cnt2=0
Third Counter:
Now this is where I get lost, essentially the outer loop counts 11 times, but it's not as simple as 11 * 412,335 as this doesn't stack up, and besides the counters (cnt1/cnt2) will still count backwards from 255 through to 0 again, so this means for each of the 11 loops the value is;
(255 *7-5) * (255*7 -3) * 11, but that gives 34,891,560 which clearly isn't right.
Can anyone shed some light on how the theory of calculating this works?
Thanks
I have looked around at Delay examples and many of the forums suggest using a nested loop to create large delays. Most of the forums point to a Delay generator tool which creates the code for you based on the clock freq and delay. This is great for implementation but for those of us learning how that works it's not as these generators seldom explain the theory. I am attempting to understand it myself so please correct my workings out where necessary so I can get an idea of where I may be going wrong.
I used the generator to create a 1 sec delay for a 20mhz clock and this is what I got;
Now my question is around how this gets to 4999993 cycles. Based on this being for a PIC device, I understand that a 20Mhz clock / 4 gives 5Million cycles per sec, so the figure below is near enough.
Each loop takes 7 cycles (from my working out).
The first counter will give;
(44 * 7) -5 = 303 - The minus 5 is for the incomplete loop as only 2 cycles have occurred on the last loop when cnt1=0
Second counter gives;
(255 * 231 * 7) -4 = 412,335 - The minus 3 is for the incomplete loop as 4 have executed on the last loop when cnt2=0
Third Counter:
Now this is where I get lost, essentially the outer loop counts 11 times, but it's not as simple as 11 * 412,335 as this doesn't stack up, and besides the counters (cnt1/cnt2) will still count backwards from 255 through to 0 again, so this means for each of the 11 loops the value is;
(255 *7-5) * (255*7 -3) * 11, but that gives 34,891,560 which clearly isn't right.
Can anyone shed some light on how the theory of calculating this works?
Thanks
Code:
dlay_loop
;4999993 cycles
movlw d'44'
movwf cnt1
movlw d'231'
movwf cnt2
movlw d'11'
movwf cnt3
dlay_loop_0
decfsz cnt1, f
goto $+2
decfsz cnt2, f
goto $+2
decfsz cnt3, f
goto dlay_loop_0
;3 cycles,
goto $+1
nop
;4 cycles (including call)
return