How to program a delay in assembly?

Thread Starter

MLCC

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

beenthere

Joined Apr 20, 2004
15,819
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.
 

Ratch

Joined Mar 20, 2007
1,070
MLCC,

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?
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
 

Thread Starter

MLCC

Joined Aug 15, 2008
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)
 

dbogusone

Joined Jul 20, 2008
1
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)
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.
 

Arm_n_Legs

Joined Mar 7, 2007
186
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
 
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?
 

Mark44

Joined Nov 26, 2007
628
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.
 
Top