;******************************************************************
; *
; DelayMS(), DelayUS(), DelayCy() Mike McLaren, K8LH, Jun'07 *
; *
; requires the use of constant operands known at assembly time! *
; *
; simple macro "front ends" and a small 16-bit delay subroutine *
; which allows specifying delays in msecs, usecs, or cycles. *
; *
; the delay range is based on the microprocessor clock and the *
; delay subroutine loop time (a 4 cycle loop in this case). *
; *
; 4 MHz, 1 cycles/usec, 16..262144 usecs, 1..262 msecs *
; 8 MHz, 2 cycles/usec, 8..131072 usecs, 1..131 msecs *
; 12 MHz, 3 cycles/usec, 6...87381 usecs, 1...87 msecs *
; 16 MHz, 4 cycles/usec, 4...65536 usecs, 1...65 msecs *
; 20 MHz, 5 cycles/usec, 4...52428 usecs, 1...52 msecs *
; *
; example: a 1 msec delay can be produced the following ways *
; *
; DelayMS(1) ; delay 1 msec *
; DelayUS(1000) ; delay 1 msec *
; DelayCy(1*msecs) ; delay 1 msec *
; DelayCy(1000*usecs) ; delay 1 msec *
; *
; the latter two methods allow you to add or subtract cycles to *
; maintain precise (clock independent) isochronous loop timing. *
; *
;******************************************************************
radix dec
[COLOR=magenta]
clock equ 4 ; user clock freq in MHz[/COLOR]
usecs equ clock/4 ; cycles per microsecond
msecs equ clock/4*1000 ; cycles per millisecond
; *
; DelayMS(), DelayUS(), and DelayCy() macros *
; *
DelayMS macro pDelay ; msec range from table above
DelayCy(pDelay*msecs) ; convert to cycles
endm ;
DelayUS macro pDelay ; usec range from table above
DelayCy(pDelay*usecs) ; convert to cycles
endm
DelayCy macro pCycles ; cycles, 16..262159 range
movlw high((pCycles-16)/4)+1
movwf TMRH
movlw low ((pCycles-16)/4)
call DelayLo-(pCycles%4)
endm
; *
; example code for simulation testing; *
; *
SimTest DelayUS(16000) ; remember to set 'clock' equate
nop ; put simulator break point here
; *
;******************************************************************
; *
; Delay(16..262159 Tcy) subroutine Mike McLaren, K8LH, Jun'07 *
; *
; 12 words, 1 RAM variable, 14-bit core *
; *
Delay.16
nop ; entry point for delay%4 == 3
nop ; entry point for delay%4 == 2
nop ; entry point for delay%4 == 1
DelayLo addlw -1 ; subtract 4 cycle loop time
skpnc ; borrow? yes, skip, else
goto DelayLo ; do another loop
nop ;
DelayHi addlw -1 ; subtract 4 cycle loop time
decfsz TMRH,F ; done? yes, skip, else
goto DelayLo ; do another loop
goto $+1 ; burn off 2 cycles
return ;
; *
;******************************************************************