# DELAY and the 8051

#### Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

•  Subject
• Author
• Posted on

Hi All

I've obtained the "The 8051 Micro Controller and Embedded Systems Using
Assembly and C-2nd-Ed -", and in this manual the DELAY routine is described
as below, which really confuses me a lot

######################################

In original 8051, one machine cycle lasts 12 oscillator periods
So, 1 oscilator period for a 12Mhz Crystal will be 12.0 Mhz / 12 = 1Mhz
Therefore one machine cycle is  is 1/1Mz = 1us (1 micro second)

For 12Mhz the delay is as follows for this example:
DELAY : MOV R3 ,#200    ;= 1 Machine Cycle
REPEAT:  DJNZ R3,REPEAT ;= 2 Machine Cycles
RET         ;= 2 Machine Cycles
END

Therefore the above routine equals to a delay of 403us

"#200" is equal to a delay of  , [(200x2)+1+2]x1us = 403us (403
Microseconds)

######################################

If the above is 100% correct, how will a 1 Second delay look like ?

I see a few examples showing a 1 sec delay as " MOV R1, #0FFH" - but don't
seem to "reverse engineer" the "#0FFH" back to 1 Second using the above
information

without knowing what I do

Kind Regards
Lodewicus Maas

Re: DELAY and the 8051

mov r3,#0ff
mov r4,#0ff
mov r5,#08
delay: djnz r3,delay        256x2uS ( 2 instructions)= approx 512uS
djnz r4,delay        256xx512uS = 131 mS approx
djnz r5,delay        8 x 131 mS = appex 1 sec
ret
sort of thing...
Much better to use a timer counter set to time at say 1mS and do the
'counting ' in the RTC interrupt routine...

Re: DELAY and the 8051

Hi  TTman

You're completely lossing me here - Hex FF is equal to 255 - is this 0 to
255 which is in fact 256 ?:

In line 1) you set delay51%2us(2us x ff), and then in line 2) you multiply
the delay set in line 1) with 256 (ff), and then in line 3) you multiple the
new delay value set in line 2) with 8

Do I understand this correctly ?

Can I build up a few "DELAY" routines to obtain different delay timings like
DELAY_1, DELAY_2 ........ DELAY_N, everytime using its own r3,r4,
.........rn  value , or is there any limit to the total DELAY routines which
you can declare ?

mov r3,#0ff
mov r4,#0ff
mov r5,#08
1) delay: djnz r3,delay        256x2uS ( 2 instructions)= approx 512uS
2)            djnz r4,delay        256xx512uS = 131 mS approx
3)            djnz r5,delay        8 x 131 mS = appex 1 sec

Lodewicus Maas

Re: DELAY and the 8051

YES....

YES, ABSOLUTELY, BUT U MAY NEED R5 AS WELL

Re: DELAY and the 8051

Thank you TTman - much appreciated

Re: DELAY and the 8051

Setting the register to #0ff only is 255

Re: DELAY and the 8051

snipped-for-privacy@gmail.com says...

The delay routine timing you posted is correct.
The information about the 1 sec delay is, obviously, missing some
information.
The 'standard' way of making longer delays is to make a fixed delay and
call it multiple times, think of nested loops, or calling a fixed delay
in a loop.

Re: DELAY and the 8051

Now that others have shown you how to do it, you should also know that
you should almost never waste processor cycles in long delays, at
least in "real" (as opposed to educational or hobby) applications.
For example, in your case of a 1 second delay your processor could
have done a million clock cycles worth of useful work.

So go ahead now and understand how to nest delays, but also understand
that in real life a 1 second delay would usually be achieved in some
other, more efficient manner (typically involving timers and
interrupts).

Mike