Hardware Reference
In-Depth Information
4.9 Subroutines for Creating Time Delay
We have learned how to create a time delay using program loops. These program loops can
be converted into subroutines so that they can be called from any place in a program.
Example 4.11
Write a subroutine that can create a time delay of 100 ms.
Solution: By adding an rts instruction to the end of the instruction sequence of Example 2.25, a
subroutine that creates a delay of 100 ms can be created.
delay100ms
pshx
ldx
#60000
; 2 E cycles
iloop
psha
; 2 E cycles
pula
; 3 E cycles
psha
; 2 E cycles
pula
; 3 E cycles
psha
; 2 E cycles
pula
; 3 E cycles
psha
; 2 E cycles
pula
; 3 E cycles
psha
; 2 E cycles
pula
; 3 E cycles
psha
; 2 E cycles
pula
; 3 E cycles
psha
; 2 E cycles
pula
; 3 E cycles
nop
; 1 E cycle
nop
; 1 E cycle
dbne
x,iloop
; 3 E cycles
pulx
rts
This subroutine will be more useful if it is made parameterized; that is, the delay time can
be set to be a multiple of 100 ms instead of a fixed value. The following subroutine can generate
a time delay that is a multiple of 100 ms with the multiple passed in Y:
delayby100ms
pshx
eloop3 ldx
#60000
; 2 E cycles
iloop3
psha
; 2 E cycles
pula
; 3 E cycles
psha
; 2 E cycles
pula
; 3 E cycles
psha
; 2 E cycles
pula
; 3 E cycles
psha
; 2 E cycles
pula
; 3 E cycles
psha
; 2 E cycles
 
Search WWH ::




Custom Search