Hello All.
I've a question regarding delay loops.
1) The book I am using gives this fragment of code as a means to produce a 42 microsecond delay with a PIC using a 4MHz crystal:
;=======================
; Procedure to delay
; 42 microseconds
;=======================
delay_125
movlw D'42' ; Repeat 42 machine cycles
movwf count1 ; Store value in counter
repeat
decfsz count1,f ; Decrement counter
goto repeat ; Continue if not 0
return ; End of delay
2) This makes sense to me: instructions run at 1/4 clock frequency, f, (here 4MHz / 4 = 1 MHz).
T (period) = 1 / f
So the period to execute each instruction is 1 microsecond.
Since the file count1 contains decimal 42, it should take 42 microseconds to count down to zero.
So far, so good (I think).
3) Now the confusion begins:
The same program then gives this fragment to produce a 5 millisecond delay. It relies on calling the 42 microsecond delay reproduced above 41 times:
;=======================
; Procedure to delay
; 5 milliseconds
;=======================
delay_5
movlw D'41' ; Counter = 41
movwf count2 ; Store in variable
delay
call delay_125 ; Delay
decfsz count2,f ; 40 times = 5 milliseconds
goto delay
return ; End of delay
4) It seems to me that calling a 42 microsecond delay 41 times produces this result:
(42x10-6 seconds) x 41 = 1.722x10-3 seconds. - That's not 5 milliseconds: it's too short.
-If I recall correctly, executing a "goto" instruction takes longer than one cycle. - Is that having an effect, or can anyone explain what I've missed, please?
Thanks.
Analogue.