Pause

puts the controller into a "do-nothing" delay loop for 1 to 65535 units of 1000 instruction cycles (milliseconds at 4 MHz).

Precise time delays are an important part of most control program, since they allow the controller to synchronize with slower, real-world events.

Pause accepts a 16-bit number in hiB and lowB, and puts the controller into a do-nothing loop for that number of 1000-instruction-cycle units. With a 4-MHz clock (as in the BASIC Stamp) this amounts to time delays ranging from 1 to 65,535 milliseconds.

The routine requires a little processing time in addition to the internal delay loop, so the actual delay produced will always be 16 instruction cycles longer than specified. This amounts to an error of less than 2 percent for a 1-unit delay, and 0.24 parts per million for a 65,535-unit delay.

Demonstrating Pause.

To see Pause in operation, either run the program with the PSIM simulator, or connect the circuit below to an erasable PIC or PIC emulator, such as the Parallax downloader. Assemble and run PAUSE.SRC. When you apply power to the PIC, the LED will toggle once per second (assuming a 4 MHz clock). Try changing the value passed to Pause to adjust the flashing rate.


; PAUSE time
; A general-purpose delay routine that puts the PIC into a do-nothing
; loop for a 16-bit number of milliseconds (1 to 65535) at 4 MHz. Requires 
; 16 cycles of overhead for call, return and other processing. 

        org     8
hiB     ds      1       ; MSB of time. 
lowB    ds      1       ; LSB of time. 
temp    ds      1       ; temporary counter for Pause

; Device data and reset vector
        device  pic16c55,xt_osc,wdt_off,protect_off
        reset   start
        org     0

start   mov     !rb, #0 ; Output through RB. 
        mov     hiB,#3  ; Length of delay =
        mov     lowB,#0E8h      ; 3E8h (1000 ms @ 4MHz).
        XOR     rb,#255 ; Toggle LED(s).
        call    Pause   ; Wait 1 second. 
        jmp     start   ; Do it again. 

; Upon entry, variables hiB and lowB hold the MSB and LSB of the 
; number of milliseconds (1 to 65535) to delay. 

Pause   NOT     hiB     ; Take twos complement
        NEG     lowB    ; of the 16-bit counter
        snz             ; If zero, lowB overflowed,
        inc     hiB     ; so carry into hiB.
:load   mov     temp,#248       ; Set up for 1-ms loop. 
:loop   nop             ; Do nothing.
        djnz    temp,:loop      ; Do more nothing. 
        jmp     $+1     ; 2-cycle "nop"
        inc     lowB    ; lowB = lowB+1. 
        snz             ; Overflow in lowB? 
        incsz   hiB     ; Yes: hiB=hiB+1, overflow?. 
        jmp     :load   ; No overflow: do it all again. 
        ret             ; Overflow. Return to caller.