1. 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.
    Dismiss Notice

Delay for led

Discussion in '8051/8951' started by Parth86, Oct 5, 2013.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    512
    Likes:
    5
    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 ?
     
  2. alec_t

    alec_t Well-Known Member Most Helpful Member

    Joined:
    Jul 10, 2011
    Messages:
    9,251
    Likes:
    1,218
    Location:
    Cardiff, Wales
    What are resistors R1 and R2 connected to? Or did you mean registers?
     
  3. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,154
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE

    No!! I said R2 and R1 are REGISTERS in the 8051 core not RESISTORS.....
    Code (asm):

    delay:
        clr  R1   ; Register 1
        clr  R2   ; Register 2
    dly:
         djnz   R1, dly
         djnz   R2, dly
         ret
     
     
  6. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    512
    Likes:
    5
    F
    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: Oct 5, 2013
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,154
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    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 (asm):

    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: Oct 5, 2013
    • Like Like x 1
  8. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    512
    Likes:
    5
    ho
    How did you calculated R1 =77
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,154
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    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...
     

Share This Page