Delay calculation for 8051

Thread Starter

teen devil

Joined Apr 6, 2009
47
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....
 
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.
 

HallMark

Joined Apr 3, 2011
89
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

now your Total Cycles * 1.085uS = your desired time.
 

Nvade

Joined Jan 20, 2011
7
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.
 
Top