EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

DELAY and the 8051

Started by Lodewicus Maas October 1, 2009
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

Can someone please help me to understand this, I don't just want to use this 
without knowing what I do

Kind Regards
Lodewicus Maas 

"Lodewicus Maas" <wicus.maas@gmail.com> wrote in message 
news:TbqdnYr0FuoROVnXnZ2dnUVZ8lOdnZ2d@saix.net...
> 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 > > Can someone please help me to understand this, I don't just want to use > this without knowing what I do > > Kind Regards > Lodewicus Maas
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...
In article <TbqdnYr0FuoROVnXnZ2dnUVZ8lOdnZ2d@saix.net>, 
wicus.maas@gmail.com says...
> 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 > > Can someone please help me to understand this, I don't just want to use this > without knowing what I do
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.
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 delay=512us(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

"Lodewicus Maas" <wicus.maas@gmail.com> wrote in message 
news:ucqdnXTKXe4ikljXnZ2dnUVZ8sidnZ2d@saix.net...
> 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 delay=512us(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 ?
YES....
> > 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 ?
YES, ABSOLUTELY, BUT U MAY NEED R5 AS WELL
> > 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 >
Thank you TTman - much appreciated

"TTman" <someone.pc@ntlworld.com> wrote in message 
news:6N9xm.238795$H%7.143540@newsfe02.ams2...
> > "Lodewicus Maas" <wicus.maas@gmail.com> wrote in message > news:ucqdnXTKXe4ikljXnZ2dnUVZ8sidnZ2d@saix.net... >> 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 delay=512us(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 ? > > YES.... > >> >> 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 ? > > YES, ABSOLUTELY, BUT U MAY NEED R5 AS WELL > >> >> 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 >> > > >
Setting the register to #0ff only is 255
If you start with #00 then it will be 256

"Lodewicus Maas" <wicus.maas@gmail.com> wrote in message 
news:ucqdnXTKXe4ikljXnZ2dnUVZ8sidnZ2d@saix.net...
> 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 delay=512us(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 >
On Oct 1, 5:45=A0am, "Lodewicus Maas" <wicus.m...@gmail.com> wrote:
> 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 describ=
ed
> 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 =3D 1Mhz > Therefore one machine cycle is =A0is 1/1Mz =3D 1us (1 micro second) > > For 12Mhz the delay is as follows for this example: > DELAY : MOV R3 ,#200 =A0 =A0;=3D 1 Machine Cycle > REPEAT: =A0DJNZ R3,REPEAT ;=3D 2 Machine Cycles > =A0 =A0 =A0 =A0 =A0RET =A0 =A0 =A0 =A0 ;=3D 2 Machine Cycles > =A0 =A0 =A0 =A0 =A0END > > Therefore the above routine equals to a delay of 403us > > "#200" is equal to a delay of =A0, [(200x2)+1+2]x1us =3D 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 > > Can someone please help me to understand this, I don't just want to use t=
his
> without knowing what I do > > Kind Regards > Lodewicus Maas
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

The 2024 Embedded Online Conference