Maker Pro
Maker Pro

how to calculate the delay [8051]

kawtar

Apr 15, 2016
4
Joined
Apr 15, 2016
Messages
4
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
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
why do we have to multiply it by 255
consider this statement right before the loop sarts:
DELAY: MOV R2,#255
and this statement which terminates the loop:
DJNZ R2,HERE

why is the time delay outside the loop [1660.05 us+1+1)]x1.085 and not just 1+1*1.085
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.
 
Top