;******************************************************************
; *
; DelayMS() and DelayUS() Macros Mike McLaren, K8LH, Jun'07 *
; *
; simple 'front end' macros for 24-bit DelayTcy() routines that *
; allow you to specify delays in 'usecs' and 'msecs'. *
; *
; the delay parameter range is based on clock frequency *
; *
; 4 MHz, 1 cycle /usec, 21..16,777,215 us, 1..16,777 ms *
; 6 MHz, 1.5 cycles/usec, 14..11,184,810 us, 1..11,184 ms (1) *
; 8 MHz, 2 cycles/usec, 11...8,388,607 us, 1...8,388 ms *
; 10 MHz, 2.5 cycles/usec, 9...6,710,886 us, 1...6,710 ms (1) *
; 12 MHz, 3 cycles/usec, 7...5,592,405 us, 1...5,592 ms *
; 16 MHz, 4 cycles/usec, 6...4,194,303 us, 1...4,194 ms *
; 20 MHz, 5 cycles/usec, 5...3,355,443 us, 1...3,355 ms *
; 24 MHz, 6 cycles/usec, 4...2,796,202 us, 1...2,796 ms *
; 28 MHz, 7 cycles/usec, 3...2,396,745 us, 1...2,396 ms *
; 32 MHz, 8 cycles/usec, 3...2,097,151 us, 1...2,097 ms *
; 36 MHz, 9 cycles/usec, 3...1,864,135 us, 1...1,864 ms *
; 40 MHz, 10 cycles/usec, 3...1,677,721 us, 1...1,677 ms *
; 48 MHz, 12 cycles/usec, 2...1,398,101 us, 1...1,398 ms *
; *
; (1) 6 and 10 MHz clocks produce inprecise timing *
; *
; DelayMS() and DelayUS() macros require a 'clock' equate; *
; *
clock equ 32 ; insert your clock freq (MHz)
; *
; DelayMS() and DelayUS() macros; *
; *
DelayMS macro msecs ; delay range: see list above
DelayUS(msecs*1000)
endm
DelayUS macro usecs ; delay range: see list above
DelayTcy(usecs*(10000/(4000/clock))/10)
endm
; *
;******************************************************************
; *
; DelayTcy(), 24-bit Mike McLaren, K8LH, Jun-07 *
; *
; simple macro for simulation testing; *
; *
DelayTcy macro delay ; parameter 21..16777215
if delay < 256
movlw delay ; 2 instructions, 3 cycles
rcall DelayTcy.byte ;
else
if delay < 65536
movlw ~(high(delay)) ; 4 instructions, 5 cycles
movwf TMRH ;
movlw low delay ;
rcall DelayTcy.16 ;
else
movlw ~(upper(delay)) ; 6 instructions, 7 cycles
movwf TMRU ;
movlw ~(high(delay)) ;
movwf TMRH ;
movlw low delay ;
rcall DelayTcy.24 ;
endif
endif
endm
; *
; code for simulation testing; *
; *
SimTest DelayMS(2000) ; DelayTcy(), DelayUS(), DelayMS()
nop ; put simulator breakpoint here
; *
; *
;******************************************************************
; *
; DelayTcy() subroutine Mike McLaren, K8LH, Jun-07 *
; *
; 20 words, 2 RAM variables (16 bit core) *
; ^^^^^^^^^^^ *
DelayTcy.byte
clrf TMRH ;
comf TMRH,F ; compliment TMRH
DelayTcy.16
clrf TMRU ;
comf TMRU,F ; compliment TMRU
DelayTcy.24
addlw -24 ; subtract "overhead" + 1 cycle
bnc DelayHi ; borrow? yes, branch, else
addlw 1 ; replace unused 'borrow' cycle
DelayLo
addlw -3 ; subtract 3 cycle loop time
bc DelayLo ;
DelayHi
addlw -3 ; subtract 3 cycle loop time
incfsz TMRH,F ; TMRH done? yes, skip, else
bra DelayLo ; loop again
addlw -3 ; subtract 3 cycle loop time
incfsz TMRU,F ; TMRU done? yes, skip, else
bra DelayLo ; loop again
;
; account for delay%3 timing (3, 4, or 5 cycles before return)
;
xorlw 0xF7 ; F7 (1), F8 (1), F9 (1)
bz DelayXit ; F7 (2), F8 (1), F9 (1)
xorlw 0xF9^0xF7 ; F8 (1), F9 (1)
bz DelayXit ; F8 (1), F9 (2)
DelayXit ; ----------------------
return ; F7=(3), F8=(4), F9=(5)
; *
;******************************************************************