How to program a delay in assembly?

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

  1. MLCC

    Thread Starter New Member

    Aug 15, 2008
    8
    0
    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

    Retired Moderator

    Apr 20, 2004
    15,815
    281
    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

    New Member

    Mar 20, 2007
    1,068
    3
    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

    Thread Starter New Member

    Aug 15, 2008
    8
    0
    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

    New Member

    Jul 20, 2008
    1
    0
    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

    Active Member

    Mar 7, 2007
    183
    10
    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

    Member

    Aug 12, 2008
    13
    1
  8. Developer_Dan()

    Member

    Oct 8, 2007
    17
    0
    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

    Well-Known Member

    Nov 26, 2007
    626
    1
    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.
     
Loading...