Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

delay routine calculation?

Status
Not open for further replies.

garg29

Advanced Member level 1
Advanced Member level 1
Joined
Nov 17, 2004
Messages
443
Helped
25
Reputation
50
Reaction score
10
Trophy points
1,298
Activity points
3,593
how to calculate delay in 8051

dear friends
how to calculate delay in this routine?

mov r1, #92h
mov r2,#03h
acall delay
------
------
-----
delay:
djnz r1,delay
djnz r2,delay
ret

by taking these values of r1 and r2 how much is the delay value....can anyone explain me how to calculate it?

with best regards
amit
 

8051 delay routine

Everything depends on a particular microcontroller you use.
In basic 8051 some instructions take 12 clock pulses and more, in other microcontorllers the same instruction will occure withi one clock pulse.
In data sheets of each microcontroller you will find the number of clock pulses required to performe single instruction.
Then you will have to take into account the clock frequency, and armed with this information you will be able to calculate how much time a deley subroutine may take ..

Added after 3 minutes:

PS: if you want to ever leave your DELAY subroutine you rather use the following code:

delay:
DJNZ R1, $
DJNZ R2, $
RET
 

delay calculation in 8051

Delay = ( 12 * ( r1 + ( 257 * r2i ) - 254 )) / Freq

U can calculate the delay based on this folmula.

Added after 34 minutes:

kuldeep said:
Delay = ( 12 * ( r1 + ( 257 * r2i ) - 254 )) / Freq

U can calculate the delay based on this folmula.


first assume r1=0x00, and find value of r2 such that actual delay time doesnt exeed required time delay, then find value of r1.
 

delay routine

thanks for replying ianP im using 89c52 and with 11.0592mhz clock frequency.then how to calculate?
also please can u explain me what does $ mean. i have seen this many times but dont understand it.

with best regards
amit
 

8051 microcontroller delay calculation

garg29 said:
by taking these values of r1 and r2 how much is the delay value....can anyone explain me how to calculate it?

The $ means the current value of program counter.
As you probably knew the program counter is in charge with selecting the instruction that must be executed.
When you write "DJNZ R1, $" the assembler take care for you where the jump is executed if required.
Thus if the instruction is placed in code memory at say 1FF2H, when the program counter access that memory location the microcontroller will execute a test of register R1 after decrementing his value with one. If the R1 register has a value different of zero, the program counter will be loaded with 1FF2H (remeber the value choosen as example) and the microcontroller will execute again the instruction "DJNZ R1, $" until R1 get the zero value.

If you use 89C52 with 11.0592 MHz clock frequency that means aprox. 1 micro second oscillator period.
The microcontroller needs 24 oscillator period in order to execute "DJNZ R1, DELAY".
If R1 has a value of 92H (146 decimal) the amount of time it takes until the R1 goes to zero is 146 X 24 = 3504 microseconds.
Then the microcontroller executes "DJNZ r2,delay" that takes another 24 microseconds.
Since the R2 has now a value of 2 decimal (remember he starts at 3) the jump is executed and the instruction DJNZ R1, DELAY takes another 256 x 24 = 6144 microseconds. This time the R1 register starts from 00H instead 92H because you left him at 00H from previous pass.
Next the DJNZ R2, delay is executed and takes another 24 microseconds, leaving the R2 with value of 01H.
Another 6120 microseconds for "DJNZ R1, DELAY".
Now after decrementing the R2 it gets a 00H value and the jump is not executed at label delay, but instead a RET from CALL.
If you count now the amount of time spent during the delay routine you get:
3504 + 24 + 6144 + 24 + 6144 = 15840 microseconds or aprox 16ms.
You can add the time required for call delay (24 microseconds) and RET another 24 microseconds.

I didn't choose a mathematical formula just to understand better what happens.
Hope do not be more confusing this way.
 
  • Like
Reactions: jck

    jck

    Points: 2
    Helpful Answer Positive Rating
calculate time routine in 8051

here is one for AVR
 

calculating delay in 8051

garg29 said:
thanks for replying ianP im using 89c52 and with 11.0592mhz clock frequency.then how to calculate?
also please can u explain me what does $ mean. i have seen this many times but dont understand it.

with best regards
amit

example of $

DJNZ R1, $

it is same as

delay: DJNZ R1, delay
 

calculation of delay in 8051

Hi,

Use simulator to measure number of execution cycles (N) for delay routine. The delay is N*1/fclk, fclk=fxtal/n where n is number of xtal cycles for one machine execution cycles.

For RISC architectures n=1, for standard 8051 n=12, for 8051 X2 n=6.
 

8051 delay calculation

hi Hero,
thanks for replying, as you said "Use simulator to measure number of execution cycles"......which simulator can i use....
Thanks once again,
With best regards,
Amit
 

DELAY:MOV R5, #81H ;1 m.c
AGAIN:DJNZ R5, AGAIn ;2 m.c
RET ;2 m.c


Determine the time delay indicated by the DELAY subroutine in the above program. Assuming the crystal frequency used is 12MHz.


please solve it for me as soon as posible

---------- Post added at 22:12 ---------- Previous post was at 22:01 ----------

DELAY:MOV R5, #81H ;1 m.c
AGAIN:DJNZ R5, AGAIn ;2 m.c
RET ;2 m.c


Determine the time delay indicated by the DELAY subroutine in the above program. Assuming the crystal frequency used is 12MHz.


please solve it for me as soon as posible
 

hi silvio,
can you explain to me why the oscillator period is 24 for 8051 microcontroller?
for my understanding, the machine cycle for "DJNZ" is 2, hence the amount of time it takes for"DJNZ R1, DELAY" is 2X 146= 292 microseconds.
i just dont understand why ur calculation havent include the machine cycle.
 

hi silvio,
can you explain to me why the oscillator period is 24 for 8051 microcontroller?
for my understanding, the machine cycle for "DJNZ" is 2, hence the amount of time it takes for"DJNZ R1, DELAY" is 2X 146= 292 microseconds.
i just dont understand why ur calculation havent include the machine cycle.

Hi ktwei,

You're correct. The amount of time it takes for DJNZ R1, DELAY is 292 microseconds.

It was my mistake. If the 8051 microcontroller is driven by 11,0592 Mhz XTAL, the 1 microsecond is the amount of time spent during one machine cycle, 12 oscillator period (and not one oscillator period like mentioned in my previous post).
I apologize for my mistake. Probably I was drunk in 2005.
 

delay calculation for 8051

DELAY:MOV R5, #81H ;1 m.c
AGAIN:DJNZ R5, AGAIn ;2 m.c
RET ;2 m.c


Determine the time delay indicated by the DELAY subroutine in the above program. Assuming the crystal frequency used is 12MHz.


please solve it for me as soon as posible

---------- Post added at 22:12 ---------- Previous post was at 22:01 ----------

DELAY:MOV R5, #81H ;1 m.c
AGAIN:DJNZ R5, AGAIn ;2 m.c
RET ;2 m.c


Determine the time delay indicated by the DELAY subroutine in the above program. Assuming the crystal frequency used is 12MHz.


please solve it for me as soon as posible

if u r using 8051 mc den first devide ur crystal freq by 12..
bec der is 12 oscillator period in one machine cycle..
den u get d freq..
so for 12 mhz crystal..
12/12=1 khz..so den apply formula of time t=1/f=1/1=1 micro sec..
now here R5=81
& machine cycle of djnz is 2
so this loop is rotates for 81 times
so total yime delay =81*2(machin cycle of djnz)=162micro sec...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top