Here's a simple DelayUS(microseconds) macro "front-end" for my DelayTcy routines. Remember to define or equate the "clock" variable as shown in the listing below.
Areas for improvement? How about parameter limit checking within the macro with gentle 'error' messages telling us where we went wrong?
;****************************************************************** ; * ; DelayUS() Macro Mike McLaren, K8LH, June '07 * ; * ; the DelayUS() macro simply scales the "delay" parameter value * ; passed to your favorite DelayTcy routine and doesn't generate * ; any assembler instructions on its own. * ; * ; the 'usec' delay range for a 16-bit target DelayTcy() routine * ; is based on clock frequency; * ; * ; 4 MHz, 1 cycle /usec, 17..65551 usecs * ; 6 MHz, 1.5 cycles/usec, 12..43700 usecs * ; 8 MHz, 2 cycles/usec, 9..32775 usecs * ; 10 MHz, 2.5 cycles/usec, 7..26220 usecs * ; 12 MHz, 3 cycles/usec, 6..21850 usecs * ; 16 MHz, 4 cycles/usec, 5..16387 usecs * ; 20 MHz, 5 cycles/usec, 4..13110 usecs * ; 24 MHz, 6 cycles/usec, 3..10925 usecs * ; 28 MHz, 7 cycles/usec, 3.. 9364 usecs * ; 32 MHz, 8 cycles/usec, 3.. 8193 usecs * ; 36 MHz, 9 cycles/usec, 2.. 7283 usecs * ; 40 MHz, 10 cycles/usec, 2.. 6555 usecs * ; 48 MHz, 12 cycles/usec, 2.. 5462 usecs * ; * ; the DelayUS() macro requires the following two equates; * ; * clock equ 32 ; see list above cycles_per_usec equ (10000/(4000/clock)) ; * ; * ; * ; DelayUS() macro * ; * DelayUS macro usecs ; delay range: see list above DelayTcy((usecs*cycles_per_usec)/10) endm ; * ;****************************************************************** ;****************************************************************** ; * ; DelayTcy(), 16..65551 Tcy Mike McLaren, K8LH, June'07 * ; * ; requires the use of constant operands known at assembly time! * ; * ; 12 words, 1 RAM variable, 14-bit core * ; ^^^^^^^^^^^ * ; macro produces 4 instructions; * ; * DelayTcy macro delay ; parameter range 16..65535 movlw ~(high(delay-16)) movwf TMRH movlw low(delay-16) call DelayLo-(delay%4) endm ; * ; * ; example code for simulation testing; * ; * SimTest DelayUS(2048) ; delay 2048 usecs nop ; put simulator break point here ; * ; * ;****************************************************************** nop ; entry point for delay%4 == 3 nop ; entry point for delay%4 == 2 nop ; entry point for delay%4 == 1 DelayLo addlw -4 ; subtract 4 cycle loop time skpnc ; borrow? yes, skip, else goto DelayLo ; do another loop nop ; DelayHi addlw -4 ; subtract 4 cycle loop time incfsz TMRH,F ; done? yes, skip, else goto DelayLo ; do another loop goto $+1 ; return ; ;******************************************************************
Comments: