Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Delay for led

Status
Not open for further replies.

Parth86

Member
I want to write assembly program for delay
turn on LED
wait 40 ms
turn off LED
8051 operating from 12MHZ crystal

I know I need to use loop , DJNZ condition two resistor R1, R2 The values loaded into R1 and R2 will determine the duration of the delay.
how to decide what is value of R1, R2
how to make loop ?
 
What are resistors R1 and R2 connected to? Or did you mean registers?
 
I know I need to use loop , DJNZ condition two resistor R1, R2 The values loaded into R1 and R2 will determine the duration of the delay.
how to decide what is value of R1, R2
how to make loop ?

No!! I said R2 and R1 are REGISTERS in the 8051 core not RESISTORS.....
Code:
delay:
    clr  R1   ; Register 1
    clr  R2   ; Register 2
dly:
     djnz   R1, dly
     djnz   R2, dly
     ret
 
F
No!! I said R2 and R1 are REGISTERS in the 8051 core not RESISTORS.....
Code:
delay:
    clr  R1   ; Register 1
    clr  R2   ; Register 2
dly:
     djnz   R1, dly
     djnz   R2, dly
     ret
FOR 40ms what will the value of resistor R1 and resistor R2
what should I know before calculate the delay
 
Last edited by a moderator:
Lets start with YOUR setup Which chip are you using and what crystal have you used..

For instance... A 12Mhz crystal will give you a 1uS clock cycle ( most instructions are 1 clock cycle)

So look at this delay
Code:
delay:
    mov    R1, #2
    mov    R2, #0
dly:    djnz    R2, dly
    djnz    R1, dly
    ret

mov = 1 clock = 2 x 1= 2uS( two move statements )
djnz = 2 clocks = 2 x 512uS = 1024uS (R2 counts down twice )
djnz = 2 clocks = 2uS (R1 counts two )
ret = 2 clocks = 2 x 1 = 2uS
1030uS or 1mS 30uS

so for a 40mS delay your looking to load R1 with 77 (ish)
 
Last edited:
ho
Lets start with YOUR setup Which chip are you using and what crystal have you used..

For instance... A 12Mhz crystal will give you a 1uS clock cycle ( most instructions are 1 clock cycle)

So look at this delay
Code:
delay:
    mov    R1, #2
    mov    R2, #0
dly:    djnz    R2, dly
    djnz    R1, dly
    ret

mov = 1 clock = 2 x 1= 2uS( two move statements )
djnz = 2 clocks = 2 x 512uS = 1024uS (R2 counts down twice )
djnz = 2 clocks = 2uS (R1 counts two )
ret = 2 clocks = 2 x 1 = 2uS
1030uS or 1mS 30uS

so for a 40mS delay your looking to load R1 with 77 (ish)
How did you calculated R1 =77
 
Its a guesstimate.... If when R2 holds 2 and we get 1030uS then when R2 = 80 it should yield 41.2mS so if you take 1030 ( R2 = 78 ) you'll get around 40.17mS so if R2 = 77. Then you should get 39.65mS To get it spot on you need to put in a couple of nop's...
 
Status
Not open for further replies.

Latest threads

Back
Top