How to program a delay in assembly?

Discussion in 'Programmer's Corner' started by MLCC, Aug 16, 2008.

  1. MLCC

    MLCC Thread Starter New Member

    Joined:
    Aug 15, 2008
    Messages:
    8
    Hello,
    I am programming 8051 microcotroller in assembly and I'm trying to stop the program for 10 seconds and then continue with the rest of the program. Any ideas on how to do this?

    -MLCC
  2. beenthere

    beenthere Retired Moderator

    Joined:
    Apr 20, 2004
    Messages:
    15,815
    Location:
    Missouri, USA (GMT -6)
    One classic way to make a delay is to use nested decrement loops. Every time the inner loop counts down to 0, then the next decrements, and so on.

    It's a bit tedious to adjust the timing, and interrupts will mess with the process, but it works.
  3. Ratch

    Ratch New Member

    Joined:
    Mar 20, 2007
    Messages:
    1,068
    MLCC,

    The 8051 has two internal timer/counters. Each timer/counter has two special function registers (SFRs) dedicated to it. That should make a delay a snap to code once you are familiar with the instructions and hardware.

    Ratch
  4. MLCC

    MLCC Thread Starter New Member

    Joined:
    Aug 15, 2008
    Messages:
    8
    The Loop is what seems to do the trick, although, is their a more efficient way to do this? (I need a example with it too, I'm very new to programming microcontrollers)
  5. dbogusone

    dbogusone New Member

    Joined:
    Jul 20, 2008
    Messages:
    1
    There are only two ways to do it and both will make use of a loop.
    1. A loop making use of counters, as mentioned by beenthere.
    - To get the exact timing, you have to know your instruction speed with respect to the clock that you are using.
    2. A loop that waits for a pre-set timer interrupt. Normally most microcontrollers have this feature.
  6. Arm_n_Legs

    Arm_n_Legs Active Member

    Joined:
    Mar 7, 2007
    Messages:
    183
    Location:
    Singapore
    MAIN:
    MOV TMOD,#10H ; Init timer 1
    .......
    .......
    SETB TR1 ; Start timer 1
    LCALL DELAY ;

    DELAY:
    MOV R1,#200D ; 200 x 50 ms = 10 sec
    D1:
    MOV TH1,#3CH
    MOV TL1,#B0H
    D2:
    JNB TF1,D2 ; timer overflow in 50 ms
    CLR TF1
    DJNZ R1,D1 ; loop 200 times
    RET
  7. amolhshah

    amolhshah Member

    Joined:
    Aug 12, 2008
    Messages:
    12
    Location:
    Nasik/Mumbai, India
  8. Developer_Dan()

    Developer_Dan() Member

    Joined:
    Oct 8, 2007
    Messages:
    17
    Location:
    Melbourne, Australia
    Assembly and C/C++ are just the most awesome, greatest languages EVER in programming history...
    I'm not sure if 8051 chip has these type of instructions, but what about a couple of

    NOP ; No Operation instruction

    I supose this might not be enough delay/wait for you anyhow?
  9. Mark44

    Mark44 Well-Known Member

    Joined:
    Nov 26, 2007
    Messages:
    626
    Location:
    Snohomish County, USA
    It would take more than a couple of NOP instructions to wait 10 seconds, and the number needed would depend on the clock speed of the 8051 microcontroller, and how long it takes to process a NOP.
Similar Threads
Forum Title Date
Programmer's Corner PICkit2 2 second program Saturday at 2:14 AM
Programmer's Corner basic question on programming ? Nov 17, 2014
Programmer's Corner Ladder Logic Program Nov 9, 2014
Programmer's Corner ladder diagram for PLC (ADC PROGRAM ) Oct 27, 2014
Programmer's Corner CAN YOU SOLVE THIS PROGRAMMING DILEMNA ? (a puzzle) Sep 30, 2014

Share This Page