Вы находитесь на странице: 1из 1

Example to calculate delay of delay loop subroutine

Copyright reserved
Department of Electrical, Electronic and Computer Engineering
University of Pretoria

Prof T Hanekom
EMK310 – Last revision 1 March 2010

Example to calculate delay of delay subroutine for a PIC18 series microcontroller if the PIC is running
from its internal RC oscillator set at 4 MHz. The instruction cycle (IC) is thus 4 MHz/4 = 1 MHz, i.e.
each IC takes 1 us. Let T = 1 us, Value1 = 2h and Value2 = 3h. i.e. the outer loop repeats three times and
the inner loop two times.

1 Delay_loop
2 movlw Value2 ; move the number in register value2 (= 3h) into
3 movwf Delay2 ; the Delay2 register
4 Go1
5 movlw Value1 ; move the number in register value1 (= 2h) into
6 movwf Delay1 ; the Delay1 register
7 Go2
8 decfsz Delay1,f ; Decrement Delay 1 and store the value in Delay 1.
9 goto Go2 ; Execute the goto Go2 statement after the decfsz statement till
; Delay1 is 0, then jump over the goto statement to
10 decfsz Delay2,f ; decrement Delay2 and go back to Go1. The initial value of
11 goto Go1 ; Value1 is reloaded otherwise Delay1 will decrement from
; FFh when the loop is executed the second and third times.
12 return

When the subroutine is entered, lines 2,3,5,6 each takes one IC = 1T.

The inner loop will decrement once to a value of 1 (line 8 = 1 IC), go back to Go2 (line 9 = 2 ICs),
decrement once more to a value of 0 (line 8 = 2 IC; if the value in Delay1 is zero the next instruction is
skipped, in which case the decfsz instruction takes 2 ICs if the next instruction is a 1IC instruction or 3
ICs if the next instruction is a 2 IC instruction like goto). The equation for the inner loop is thus

D1 = (1T + 2T)(Value1 – 1) + 3T

The next instruction to be executed will be line 10, where Delay2 will be decremented to 2 (line 10 = 1
IC) and the program will branch to Go1 (line 11 = 2 ICs). Delay1 will be reloaded with Value1 (lines 5,6
= 2 ICs). Next the inner loop will execute again (D1). This will repeat while Delay2 is not zero, i.e.
Value2-1 times. The last Value2 loop (i.e. the loop where Value2 becomes zero) will consist of all the
delays included in the first (Value2-1) loops. However, instead of the 3 ICs added for lines 10 and 11
(nonzero decfsz plus goto), 3 ICs will be added for line 10 because of the decfsz zero condition plus the
skipped 2 IC goto. The equation for the two loops (excluding the initial loading of the Delay variables
and the return instruction) will be: (D1 + 3T+2T) (Value2 – 1) + (D1+3T) where the 3T in the first factor
is for lines 10 and 11, the 2T is for lines 5 and 6 and the 3T in the last brackets is for line 10 when the
value in Delay2 becomes zero and the goto is skipped.

The delay for the complete subroutine, including the initial 4T (lines 2,3,5,6) and the 2T for the return
instruction, is thus

Dtotal = 4T + (D1 + 3T + 2T)(Value2 – 1) + (D1+3T)+ 2T

Вам также может понравиться