# Delay calculation for 8051

Discussion in 'Embedded Systems and Microcontrollers' started by teen devil, Mar 27, 2011.

1. ### teen devil Thread Starter Active Member

Apr 6, 2009
47
0
hi,
i am new to 8051 and i want to generate delay ... i am having problem in caalculating delay ... can u please tell me the exact way for calculation..
lets say i want to calculate delay of 0.1sec....

2. ### teen devil Thread Starter Active Member

Apr 6, 2009
47
0
by the calculation of frequency and machine cycle the vaalue is 1.085x10^-6

3. ### TierOne New Member

Feb 5, 2011
16
2
pre/post_scalar*12/oscillator_frequency*machine_cycle_per_instruction

4. ### PyramidComm New Member

Apr 1, 2011
1
0
If you want to generate a delay without using one of the timers, your code won't be able to do anything else during the delay. If this is OK, use the following code:

mov r7,#100 ;100mSec
acall delay ;call delay subroutine

delay mov r2,#230 ;2 cyc
delay1 nop ;1 cyc
nop ;1 cyc
djnz,r2 delay1 ;2 cyc consume 230x4 + 2 instr cycles=922 cyc
djnz,r7 delay
ret

If you are using an 11.059 MHz xtal, you will need to consume approx 921 instruction cycles per millisecond of delay. You could write a routine to consume 92,100 cyc, but that would only generate a delay of 100mSec. The delay subroutine consumes 922 cyc for each count in R7, so you have a subroutine that can delay between 1 and 255 mSec.

5. ### HallMark Member

Apr 3, 2011
89
5
I think you are writing code in assembly language.

Then first of all find the Machine cycles your Code needs you can find the cycles from the Datasheet for the particular instruction.

Then you have to calculate the time taken for the cycle execution if the frequency is 11.0592MHz. 8051 uses 1/12 of oscilator frequency, So now frequency is 921.6kHz

Now Cycle execution time becomes 1/f = 1/921.6kHz = 1.085uS

Jan 20, 2011
7
0
Hi there.
It is quite simple to create a delay using timer.
As such oscillator frequency is 12(specifically 11.0592)MHz.
So the clock frequency will be Xtal freq/12=1MHz(In case of 12MHz).
i.e. pulse width=1μs.
So operating timer in mode 1 (16 bit timer) gives a delay of .066s
To generate a delay of ur demand create a loop that makes the timer to count in the multiples of .066
i.e. for a single second delay, the loop needs to be executed 15 times and so on.
I guess u can figure out the approach and convert it in assembly language.
Anyhow if u need the code also, then i will give u that as well.

7. ### teen devil Thread Starter Active Member

Apr 6, 2009
47
0
thnx guys for u help