Pamela Melissa Chip Tune
New Member
Hello.
I came across this code generated by the MikroC Pro for PIC and it delays 10 mS when using an oscillator of 4 Mhz.
According to my calculations when using a 4 Mhz oscillator the PIC will divide the frequency by 4 to obtain an internal clock of 1 Mhz. Is that right?
Each MOVLW and MOVWF operation takes 1 clock cycle.
The DECFSZ operation takes 2 if it skips, or 1 cycle if it does not. right?
The GOTO takes 2 cycles. right?
The delay loop using R13 and R12 will sum a total of
DECFSZ R13+0, 1
GOTO L_pause3 <--- this will catch here most of the time, except when R13 reaches 0. This is 250*3 + 2 = 752 clock cycles
Then the next part
; 752 cycles so far
DECFSZ R12+0, 1 ; 753 cycles if we don't skip
GOTO L_pause3 ; 755 cycles and we repeat this 12 times. 755*12 = 9060 clock cycles.
On the last pass we do another 752+2 cycles because we skip
and finally
NOP
NOP
adds another 2 cycles.
That yields 4+9060 + 754 + 2 = 9816 clock cycles.. or 9.816 mS
Just_Delayed_10ms:
mmm, seems i'm missing some clock cycles.
Please correct me. I feel I must be wrong somewhere.
I came across this code generated by the MikroC Pro for PIC and it delays 10 mS when using an oscillator of 4 Mhz.
Code:
;Lab19_MusicNotes.c,11 :: Delay_ms(10);
MOVLW 13
MOVWF R12+0
MOVLW 251
MOVWF R13+0
L_pause3:
DECFSZ R13+0, 1
GOTO L_pause3
DECFSZ R12+0, 1
GOTO L_pause3
NOP
NOP
Just_Delayed_10ms:
According to my calculations when using a 4 Mhz oscillator the PIC will divide the frequency by 4 to obtain an internal clock of 1 Mhz. Is that right?
Each MOVLW and MOVWF operation takes 1 clock cycle.
The DECFSZ operation takes 2 if it skips, or 1 cycle if it does not. right?
The GOTO takes 2 cycles. right?
The delay loop using R13 and R12 will sum a total of
DECFSZ R13+0, 1
GOTO L_pause3 <--- this will catch here most of the time, except when R13 reaches 0. This is 250*3 + 2 = 752 clock cycles
Then the next part
; 752 cycles so far
DECFSZ R12+0, 1 ; 753 cycles if we don't skip
GOTO L_pause3 ; 755 cycles and we repeat this 12 times. 755*12 = 9060 clock cycles.
On the last pass we do another 752+2 cycles because we skip
and finally
NOP
NOP
adds another 2 cycles.
That yields 4+9060 + 754 + 2 = 9816 clock cycles.. or 9.816 mS
Just_Delayed_10ms:
mmm, seems i'm missing some clock cycles.
Please correct me. I feel I must be wrong somewhere.