Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Exact Delay Time calculations for Delay(10ms)

Status
Not open for further replies.
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.

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.
 
The basic delay
Code:
cnt = initial_value
loop:
   decfsz cnt
   goto loop
will delay for initial_value*3-1 instructions. So for your example, the first inner loop takes innerloop(251) = 251*3-1 cy, and every subsequent inner loop takes innerloop(256) = 256*3-1 cy. The outer loop takes 13*3-1 + innerloop(251) + 12*innerloop(256) = 13*3-1 + 251*3-1 + 12*(256*3-1). You then have the 4 instructions before the loops, and the 2 instructions after. The total is therefore 13*3-1 + 251*3-1 + 12*(256*3-1) + 4 + 2 = 10000 cy.
 
Hi,

Working out your own software delay times is a useful grey matter exercise, however you might find this link easier.
https://www.golovchenko.org/cgi-bin/delay

If you use MPLABs Sim you can check every single step of your delay loop and monitor both time and instructions.

000062.jpg
 
The basic delay
Code:
cnt = initial_value
loop:
   decfsz cnt
   goto loop
will delay for initial_value*3-1 instructions. So for your example, the first inner loop takes innerloop(251) = 251*3-1 cy, and every subsequent inner loop takes innerloop(256) = 256*3-1 cy. The outer loop takes 13*3-1 + innerloop(251) + 12*innerloop(256) = 13*3-1 + 251*3-1 + 12*(256*3-1). You then have the 4 instructions before the loops, and the 2 instructions after. The total is therefore 13*3-1 + 251*3-1 + 12*(256*3-1) + 4 + 2 = 10000 cy.

Hello dougy83, you are right. I still don't understand why. Thank you.
Thankyou WP, it takes a time to understand how the stopwatch works.

Both of you are right. My brain is dry at this point.
 
Finally! I got it.

The first loop counts from 251 to 0. The register R13 rolls to 255 and the following inner loops count from 0 to 0. I was very confused. Now it is clear. First loop is (251*3) - 1, the following are (256*3)-1 because it rolls from 0 to 255, 254,253, ...2,1,0.

Thanks again.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top