Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 1

Example to calculate delay of delay loop subroutine

Copyright reserved Department of Electrical, Electronic and Computer Engineering University of Pretoria Prof T Hanekom E !"#$ % &$$' Example to calculate delay of delay subroutine for a PIC microcontroller if the PIC is running from its internal RC oscillator (4 MHz ! "he instruction cycle (IC is thus 4 MHz#4 $ % MHz& i!e! each IC ta'es % us! (et " $ % us& )alue% $ *h and )alue* $ +h! i!e! the outer loop repeats three times and the inner loop t,o times! % -elay.loop * mo/l, + mo/,f 4 1o% 2 mo/l, 3 mo/,f 4 1o* 5 decfsz 6 goto %7 %% %* decfsz goto return )alue* -elay* )alue% -elay% 0 mo/e the number in register /alue* ($ +h into 0 the -elay* register 0 mo/e the number in register /alue% ($ *h into 0 the -elay% register

-elay%&f 0 -ecrement -elay % and store the /alue in -elay %! 1o* 0 Execute the goto 1o* statement after the decfsz statement till 0 -elay% is 7& then 8ump o/er the goto statement to -elay*&f 0 decrement -elay* and go bac' to 1o%! "he initial /alue of 1o% 0 )alue% is reloaded other,ise -elay% ,ill decrement from 0 99h ,hen the loop is executed the second and third times!

:hen the subroutine is entered& lines *&+&2&3 each ta'es one IC $ %"! "he inner loop ,ill decrement once to a /alue of % (line 5 $ % IC & go bac' to 1o.off* (line 6 $ * ICs & decrement once more to a /alue of 7 (line 5 $ * IC0 if the /alue in -elay% is zero the next instruction is s'ipped& in ,hich case the decfz instruction ta'es * ICs ! "he e;uation for the inner loop is thus (%" < *" ()alue% = % < *" $ -%! "he next instruction to be executed ,ill be line %7& ,here -elay* ,ill be decremented to * (line %7 $ % IC and the program ,ill branch to 1o% (line %% $ * ICs ! -elay% ,ill be reloaded ,ith )alue% (lines 2&3 $ * ICs ! >ext the inner loop ,ill execute again (-% ! "his ,ill repeat ,hile -elay* is not zero& i!e! )alue*?% times! "he last )alue* loop (i!e! the loop ,here )alue * becomes zero ,ill consist of all the delays included in the first ()alue*?% loops! Ho,e/er& instead of the + ICs added for lines %7 and %%& only * ICs ,ill be added for line %7 because of the zero condition! "he e;uation for the t,o loops (excluding the initial loading of the -elay /ariables and the return instruction ,ill be@ (-% < +"<*" ()alue* = % < (-%<*" ,here the +" is for lines %7 and %%& the first *" is for lines 2 and 3 and the last *" is for line %7 ,hen the /alue in -elay* becomes zero! "he delay for the complete subroutine& including the initial 4" (lines *&+&2&3 and the *" for the return instruction& is thus 4" < (-% < +" < *" ()alue* = % < (-%<*" < *" $ %7"<+"()alue%?% ()alue* < 4"()alue*?%

You might also like