PIC Microcontoller Time Method

Delays

Code generator  for Delays by Nikolai Golovchenko Wow!

Timer@

General Delay Methods

Counting Hours Minutes and Seconds

Stack Recursive Delay

PIC 16 Delay Tutorial by Vasily Koudymov

Delay Subroutines

Are there simulation problems with delays?

Macro Examples

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:

Comments: