Cycle Count Calculation in PIC -
you given subroutine written pic16f877 working @ 4mhz obtain approximate 30ms delay.
1 counter1 equ 0x20 2 counter2 equ 0x21 3 4 delay_loop: 5 6 movlw d'30' 7 movwf counter2 8 9 ; loop1 body takes 1ms 10 loop1: 11 movlw d'200' 12 movwf counter1 13 14 ; loop2 body takes 5us 15 loop2: 16 nop 17 nop 18 decfsz counter1, f 19 goto loop2 20 21 decfsz counter2, f 22 goto loop1 23 return instruction cycles movlw 1 movwf 1 nop 1 decfsz 1* goto 1 return 2 well, not end same values in lines 9 , 14. can you, please, show me detailed calculations?
firstly, instruction cycles not correct. goto 2 cycles , decfsz either 1 or 2 cycles depending on result.
so, loop2:
loop2: nop ; 1 cycle nop ; 1 cycle decfsz counter1, f ; 1 cycle except @ loop end, 2 cycles goto loop2 ; 2 cycles so total 5 cycles, 4mhz clock having 1us cycle time1 5us. counter1 being 200, inner loop takes 200 * 5us = 1000us = 1ms.
now enclosing loop, loop1:
loop1: movlw d'200' ; 1 cycle movwf counter1 ; 1 cycle ; 1ms delay loop2 (removed clarity) decfsz counter2, f ; 1 cycle except @ loop end, 2 cycles goto loop1 ; 2 cycles return ; 2 cycles so total of 5 cycles loop1 housekeeping (ignoring final return), added 1000us inner loop = 1005us. counter2 being 30, total delay 30 * 1005us = 30.15ms (give or take handful of initial housekeeping instructions , final return, idea).
1remember these pics use 4 clock cycles per instruction, 4mhz clock executes @ 1mips, giving instruction time of 1us.
Comments
Post a Comment