Delay 8051
Delay 8051
Delay 8051
Example 3-22
From the above discussion we conclude that use of the instruction in generating time delay is not the
most reliable method. To get more accurate time delay we use timers as described in Chapter 9.
Meanwhile, to get an accurate time delay for a given 8051 microcontroller, we must use an
oscilloscope to measure the exact time delay.
SUMMARY
The flow of a program proceeds sequentially, from instruction to instruction, unless a control transfer
instruction is executed. The various types of control transfer instructions in Assembly language
include conditional and unconditional jumps, and call instructions.
The looping action in 8051 Assembly language is performed using a special instruction, which
decrements a counter and jumps to the top of the loop if the counter is not zero. Other jump
instructions jump conditionally, based on the value of the carry flag, the accumulator, or bits of the I/O
port. Unconditional jumps can be long or short, depending on the relative value of the target address.
Special attention must be given to the effect of LCALL and ACALL instructions on the stack.
; Programa: Retardos
; calculo de retardo
org
0000h
inicio:
cpl
a
mov
P0,a
call
seg05
ajmp
inicio
; * * * * * * * * * * * * * * * * * * * * * * * *
; retardo de 500 micro segundos (500us)
; * * * * * * * * * * * * * * * * * * * * * * * *
; tiempo =
sec
*
12 periodos
;
12MHz
1 ciclo de maquina
; * * * * * * * * * * * * * * * * * * * * * * * *
; tiempo =
0.5 s = 500000us
;
ciclos de maquina
; * * * * * * * * * * * * * * * * * * * * * * * *
seg05:
;(2)
=
2
mov r6,#0fah
;(1)
=
1
xx:
mov R7,#0f9h ;(1)*r6[250]
=
250
nop
;(1)*r6(250)
=
250
nop
;(1)*r6(250)
=
250
nop
;(1)*r6(250)
=
250
nop
;(1)*r6(250)
=
250
nop
;(1)*r6(250)
=
250
xxx:
nop
;(1)*r6[250]*r7[249] = 62250
nop
;(1)*r6[250]*r7[249] = 62250
nop
;(1)*r6[250]*r7[249] = 62250
nop
;(1)*r6[250]*r7[249] = 62250
nop
;(1)*r6[250]*r7[249] = 62250
nop
;(1)*r6[250]*r7[249] = 62250
djnz R7,xxx
;(2)*r6[250]*r7[249] = 124500
djnz r6,xx
;(2)*r6[250]
=
500
ret
;(2)
=
2
;
---------------------------------;
500005
end
MOV P1,#00000000B
MOV TMOD,#00000001B
MAIN: SETB P1.0
ACALL DELAY
CLR P1.0
ACALL DELAY
SJMP MAIN
DELAY: MOV TH0,#0FEH
MOV TL0,#00CH
SETB TR0
HERE: JNB TF0,HERE
CLR TR0
CLR TF0
SETB P1.0
RET
END
2 KHz Square wave using 8051 timer.
MOV P1,#00000000B
MOV TMOD,#00000001B
MAIN: SETB P1.0
ACALL DELAY
CLR P1.0
ACALL DELAY
SJMP MAIN
DELAY: MOV TH0,#0FCH
MOV TL0,#018H
SETB TR0
HERE:JNB TF0,HERE
CLR TR0
CLR TF0
SETB P1.0
RET
END
10 KHz square wave using 8051 timer.
MOV P1,#00000000B
MOV TMOD,#00000001B
MAIN: SETB P1.0
ACALL DELAY
CLR P1.0
ACALL DELAY
SJMP MAIN
DELAY: MOV TH0,#0FFH
MOV TL0,#0CEH
SETB TR0
HERE:JNB TF0,HERE
CLR TR0
CLR TF0
SETB P1.0
RET
END
Program for generating 1mS delay using 8051 timer.
The program shown below can be used for generating 1mS delay and it is written as a subroutine so that
you can call it anywhere in the program. Also you can put this in a loop for creating longer time delays
(multiples of 1mS). Here Timer 0 of 8051 is used and it is operating in MODE1 (16 bit timer).
DELAY: MOV TMOD,#00000001B // Sets Timer 0 to MODE1 (16 bit timer). Timer 1 is
not used
MOV TH0,#0FCH // Loads TH0 register with FCH
MOV TL0,#018H // LOads TL0 register with 18H
SETB TR0 // Starts the Timer 0
HERE: JNB TF0,HERE // Loops here until TF0 is set (ie;until roll over)
CLR TR0 // Stops Timer 0
CLR TF0 // Clears TF0 flag
RET
The above delay routine can be looped twice in order to get a 2mS delay and it is shown in the program
below.
Once timer flag (TF) is set, the programmer must clear it before it can be set again.
The timer does not stop after the timer flag is set. The programmer must clear the TR bit in order
to stop the timer.
Once the timer overflows, the programmer must reload the initial start values to the TH and TL
branching instruction.
Maximum delay possible using a single 8051 timer is 65536S and minimum is 1S provided that
;
;
;
;
;