# how to calculate the delay 

Discussion in 'Microcontrollers, Programming and IoT' started by kawtar, May 1, 2017.

1. ### kawtar

4
0
Apr 15, 2016
Hi
I didnt understand how to compute time delays ill give an example ( I got this example from the internet
DELAY: MOV R2,#255
HERE: NOP
NOP
NOP
NOP
DJNZ R2,HERE
RET
the time delay inside the HERE loop is 255(1+1+1+1+2)] x 1.085 us = 1660.05 us I mean why dont we do 1+1+1+1+2 *1.085 why do we have to multiply it by 255
also why is the time delay outside the loop [1660.05 us+1+1)]x1.085 and not just 1+1*1.085

2. ### Harald KappModeratorModerator

11,710
2,718
Nov 17, 2011
consider this statement right before the loop sarts:
DELAY: MOV R2,#255
and this statement which terminates the loop:
DJNZ R2,HERE

What do you mean by "time delay outside the loop"?
When you write the code in a bit more structured manner:
Code:
```DELAY: MOV R2,#255

HERE: NOP       ; this is the actual start of the loop
NOP
NOP
NOP
DJNZ R2,HERE    ; this is the end of the loop

RET
```
you can easily see that there is the loop plus 2 instructions outside the loop. This should answer your 2nd question - once you have understood the answer to the first question.
[1660.05 us+1+1)]x1.085 is the delay of the total cde, not just the code outside the loop.  