 
Code generator for Delays by Nikolai Golovchenko Wow!
Counting Hours Minutes and Seconds
PIC 16 Delay Tutorial by Vasily Koudymov
Delay Subroutines
Are there simulation problems with delays?
James Newton says:
I also wrote this god-awful macro for the Parallax SX key asembler that may be of use to someone: It produces the smallest and/or tightest loop delays inline when you call it with the value and units of time you wish to delay for; calculating the number of cycles based on the processor speed. The CpuMhz equate must be adjusted to what ever is right for your chip. It should probably be called CpuMips vice CpuMhz. The only redeming virtue is that it does not use anything other than w (on the SX you can decrement W); no stack use, no register use. :device pins28, pages1, banks8, turbo, stackx, optionx, carryx reset reset_entry CpuMhz = 50 temp ds 1 usec EQU -6 msec EQU -3 sec EQU 1 cycles EQU 0 mynop MACRO noexpand page $>>8 ENDM cyclefor MACRO 1 _cycles = \1 IF _cycles > 0 _temp = $//4 IF _temp = 2 IF _cycles <5 REPT _cycles expand mynop noexpand ENDR _cycles = 0 ELSE expand mynop noexpand _cycles = _cycles -1 ENDIF ENDIF IF _temp = 1 IF _cycles < 6 REPT _cycles expand mynop noexpand ENDR _cycles = 0 ELSE expand mynop mynop noexpand _cycles = _cycles -2 ENDIF ENDIF IF _cycles > 3 expand mov w, #_cycles / 3 decsz 1 ;dec w clrb 2.1 ;modify PC to jump back noexpand _cycles = _cycles // 3 ;cycles left over ENDIF IF _cycles > 0 REPT _cycles expand mynop noexpand ENDR ENDIF ENDIF ENDM reset_entry mov w,#$7F mov !OPTION,w delayhelp MACRO ERROR 'USAGE: delay value, [usec,msec,sec,cycles]' ENDM delay MACRO 2 noexpand IF (\2=usec OR \2=msec OR \2=sec) AND (\1<1000 AND \1>0) IF \2=sec _cycles = (\1 * 1000000 / (1000/CpuMhz)) ENDIF IF \2=msec _cycles = (\1 * 1000 / (1000/CpuMhz)) ENDIF IF \2=usec _cycles = (\1 / (1000/CpuMhz)) ENDIF IF \2=cycles _cycles = \2 ENDIF IF _cycles = 0 expand ;delay less than one cycle at this processor speed' noexpand ELSE IF _cycles > 255 REPT (_cycles / 256) cyclefor 256 _cycles = _cycles - 256 ENDR ENDIF cyclefor _cycles ENDIF ELSE delayhelp ENDIF ENDM delay 999, usec delay 200, usec delay 250, usec delay 10, usec delay 20, usec delay 100, msec
See Also:
See:
Archive:
Questions:
Code:
try this for 1 to 117million cycle delay
delay	macro	Cycles
 variable CyclesLeft ,LoopCycles,d_used           ;  Keep Track of Remaining Cycle
 local	iloop
CyclesLeft = Cycles
	if CyclesLeft > .327680 ; 0x5 * 0xffff
d_used = 3
	else
	 if Cycles > 3*0xFF
d_used = 2
	 else
d_used = 1
	 endif
	endif
	if	d_used == 3
LoopCycles = (Cycles-6-.10) / 7
CyclesLeft = (Cycles-6-.10) - (LoopCycles*7)
	endif
	if	d_used == 2
LoopCycles = (Cycles-4-6) / 5
CyclesLeft = (Cycles-4-6) - (LoopCycles*5)
	
	endif
  if Cycles >=5
	if	d_used == 1
LoopCycles = (Cycles-2-2) / 3
CyclesLeft = (Cycles-2-2) - (LoopCycles*3)
	endif
 	movlw   (LoopCycles & 0xFF)+1
  	movwf   STACK-0
	if d_used >=2
 	movlw   ((LoopCycles>>8) & 0xFF)+1
  	movwf   STACK-1
	endif
	if d_used >=3
	if ((LoopCycles>>.16) > .255 )
 error "Required Delay is longer than 'delay' Macro can support"
	endif
 	movlw   ((LoopCycles>>.16) & 0xFF)+1
  	movwf   STACK-2
	endif
iloop
 	if d_used >=3
	 goto	$+1 
	endif
 	if d_used >=2
	 goto	$+1 
	endif
  	decfsz  STACK-0,F
   	 goto	iloop
 	if d_used >=2
  	decfsz  STACK-1,F
   	 goto	iloop+1
	endif
	if d_used >=3	
	decfsz  STACK-2,F
   	 goto	iloop+2
	endif
	
  endif
 while CyclesLeft >= 2          ;  Put in 2 Instruction Cycle Delays
  	goto    $ + 1
CyclesLeft = CyclesLeft - 2
 endw
 if CyclesLeft == 1             ;  Put in the Last Required Cycle
  	nop
 endif
        endm+
Comments:
| file: /Techref/microchip/delays.htm, 7KB, , updated: 2021/5/6 14:34, local time: 2025/10/25 22:32, 
 
216.73.216.22,10-3-83-201:LOG IN | 
| ©2025 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://www.piclist.com/Techref/microchip/delays.htm"> PIC Microcontoller Time Method </A> | 
| Did you find what you needed? | 
|  PICList 2025 contributors: o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr. | 
| The Backwoods Guide to Computer Lingo | 
.