Delay Macro (For Small PIC Chips) - Joe's Cat Website

If you need to do some simple time delays (approximately 8 to 18000+ cycles), this Delay Macro can be used by low-power PICs (like the pic10, pic12, pic16) which have a shallow call stack of 2 levels for routines such as long delays. An example using this sort of delay is shown in RS232 PIC Software USART code example.


The timing granularity of this delay macro is about 3 cycles, but it can be refined to 1 cycle accuracy if you wish to improve this macro further. Additional improvements can be made by putting the main loop into a call routine to save on program-code space (saving yourself a few program codes each time the macro is used).


You can download DelayM Macro, or you can copy and paste the code shown below into your ASM code editor.

;Delay Macro
;Wait approximately delCntVar cycles. ~ 1998oct - by J Da Silva
;enter: delCntVar=number of cycles to wait
;exit :
;ram  : DlyCnt[0,1]
DelayM	macro	delCntVar
	local	DelayLp
	local	majCnt
	local	minCnt
	variable majCnt = 1
	variable minCnt = delCntVar - 8
	if minCnt < 771
	movlw	(delCntVar-2)/3+1
	movwf	DlyCnt
DelayLp decfsz	DlyCnt,f
	goto	DelayLp
	while	minCnt > 768
minCnt -= 771
majCnt += 1
	movlw	majCnt
	movwf	DlyCnt
	movlw	minCnt/3+1
	movwf	DlyCnt+1
DelayLp decfsz	DlyCnt+1,f
	goto	DelayLp
	decfsz	DlyCnt,f
	goto	DelayLp

DlyCnt	res	2	;delay wait counter (2 bytes)

If you find this macro useful, I will appreciate a little credit for providing it here for your use/project (for example, in your source code comments, or even a web link back to this web page is great if you have a project that uses it that you want to show/demonstrate). Thanks.

Other PIC Related Pages On This Web Site
Home Page A/B Long Term Timer ASCII To MorseCode Clear RAM Macro Cylon Eye
Random Number Generator (RNG) 7.96Hz Sinewave Generator Software RS232 Table Array FSR Macro PicDis Disassembler

Copyright© 2000..2017 Joe's Cat