DELAYS


Here is a set of delay sub-routines that will cover almost every need. You can call a delay routine a number of times to increase the delay or combine any delays to create the desired time. Each delay routine needs 1, 2, 3 or 4 files and these are named d1, d2, d3, and d4.
At the beginning of your assembly program, assign d1, d2, d3, and d4 as follows:

d1     equ   21h
d2     equ   22h
d3     equ   23h
d4     equ   24h

Clock frequency = 4MHz means the instructions are processed at one million instructions per second. One instruction is also called one cycle in this discussion.

Note: d1, f means the result obtained when file d1 is decremented, is placed in the file d1.
Note:  0xC7 means the hex value: C7 and this is equal to 199.

Note:  goto   $+1 means to go to the next instruction and this takes 2 cycles.
Note:  goto   $+2 means jump the next instruction and this takes 2 cycles.
Note:  goto   $+3 means jump two instructions and this takes 2 cycles.

Do not copy and paste the text below. It contains formatting that will upset the assembler. Click HERE for a .txt file that can be copied and pasted.

 
; Delay = 0.0001 seconds = 100uS
			
	movlw	0x20
	movwf	d1
Delay_0
	decfsz	d1, f
	goto	Delay_0
	retlw	00
; Delay = 0.001 seconds = 1,000uS
			
	movlw	0xC7
	movwf	d1
	movlw	0x01
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0
	retlw	00

; Delay = 0.01 seconds = 10,000uS
			
	movlw	0xCF
	movwf	d1
	movlw	0x08
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0
	retlw	00
 
; Delay = 0.05 seconds
			
	movlw	0x0F
	movwf	d1
	movlw	0x28
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0
	retlw	00
; Delay = 0.1 seconds
			
	movlw	0x1F
	movwf	d1
	movlw	0x4F
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0
	retlw	00
 
; Delay = 0.2 seconds

	movlw	0x3F
	movwf	d1
	movlw	0x9D
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0
	retlw	00
; Delay = 0.25 seconds
			
	movlw	0x4F
	movwf	d1
	movlw	0xC4
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0
	retlw	00
; Delay = 0.5 seconds

			
	movlw	0x03
	movwf	d1
	movlw	0x18
	movwf	d2
	movlw	0x02
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0
			
	goto	$+1
	goto	$+1
	retlw	00

 

; Delay = 1 second = 1,000,000 cycles
			
	movlw	0x08
	movwf	d1
	movlw	0x2F
	movwf	d2
	movlw	0x03
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0
	nop			
	retlw	00

; Delay = 2 seconds = 2,000,000 cycles
			
	movlw	0x11
	movwf	d1
	movlw	0x5D
	movwf	d2
	movlw	0x05
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0
			
	goto	$+1
	retlw	00
; Delay = 10 seconds = 10,000,000 cycles

			
	movlw	0x5A
	movwf	d1
	movlw	0xCD
	movwf	d2
	movlw	0x16
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0
			
	goto	$+1
	nop
	retlw	00

; Delay = 60 seconds = 60,000,000 cycles
			
	movlw	0x23
	movwf	d1
	movlw	0xCB
	movwf	d2
	movlw	0x83
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0
			
	goto	$+1
	goto	$+1
	retlw	00
; Delay = 10 minutes = 600 seconds = 600,000,000 cycles

			
	movlw	0xA9
	movwf	d1
	movlw	0x41
	movwf	d2
	movlw	0xFA
	movwf	d3
	movlw	0x04
	movwf	d4
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	$+2
	decfsz	d4, f
	goto	Delay_0
			
	goto	$+1
	goto	$+1
	goto	$+1
	retlw	00

 
21/6/08