line

Jake's Electronics

a place to document

line

Basic Delays

A Microprocessor or Microcontroller executes instructions millions of times per second. The delays below will allow a program to burn up a number of instructions cycles between instructions to allow a specific time delay to occur. These types of delays use loops and are only one of many ways to cause delays in a program, one other method is to use interrupts. However, these basic delays are great for simple programs such as light chasers etc.

Source Code:

To use the delays you must place the following code into the Constant Block (CBLOCK) definition area to define the General Purpose Register labels contained with in each delay routine. See code snippet below. This assigns the register label used in the delay routines (e.g., DELAYGPR1) to a RAM address within the Microcontroller's data memory. You may have to adjust this value depending on where the RAM starts. You can verify this by checking the Data Memory Map under the Memory Organisation section in the Data Sheet for your selected device. If the RAM starts at Hexidecimal address 30, change H'20' to H'30' (or 0x30, D'48', B'00110000' etc). The RAMa address for majority of devices in the Microchip PIC 8 bit mid range family will start at Hex address 20.

CBLOCK  H'20'
DELAYGPR1
DELAYGPR2
DELAYGPR3
ENDC

Place the sub routine at the bottom of your program and simply call the dalay using it's name Label (i.e., DELAY_1S)

1 Second Delay (1/1 Second)

DELAY_1S ; Actual delay = 1 seconds = 1000000 cycles                    1/1 Second
        MOVLW   0X07                    ; 
        MOVWF   DELAYGPR1               ; 
        MOVLW   0X2F                    ; 
        MOVWF   DELAYGPR2               ; 
        MOVLW   0X03                    ; 
        MOVWF   DELAYGPR3               ; 
DELAY_1S_0
        DECFSZ  DELAYGPR1, F            ; 
        GOTO    $+2                     ; 
        DECFSZ  DELAYGPR2, F            ; 
        GOTO    $+2                     ; 
        DECFSZ  DELAYGPR3, F            ; 
        GOTO    DELAY_1S_0              ; 999990 cycles
        GOTO    $+1                     ; 
        GOTO    $+1                     ; 
        GOTO    $+1                     ; 6 cycles
        RETURN                          ; 4 cycles (including call)

250 Millisecond Delay (1/4 Second)

DELAY_250mS ; Actual delay = 0.25 seconds = 250000 cycles               1/4 Second
        MOVLW   0x4E                    ; 
        MOVWF   DELAYGPR1               ; 
        MOVLW   0xC4                    ; 
        MOVWF   DELAYGPR2               ; 
DELAY_250mS_0
        DECFSZ  DELAYGPR1, F            ; 
        GOTO    $+2                     ; 
        DECFSZ  DELAYGPR2, F            ; 
        GOTO    DELAY_250mS_0           ; 249993 cycles
        GOTO    $+1                     ; 
        NOP                             ; 3 cycles
        RETURN                          ; 4 cycles (including call)

100 Millisecond Delay (1/10 Second)

DELAY_100mS  ; Actual delay = 0.1 seconds = 100000 cycles               1/10 Second
        MOVLW   0X1F                    ;
        MOVWF   DELAYGPR1               ;
        MOVLW   0X4F                    ;
        MOVWF   DELAYGPR2               ;
DELAY_100mS_0
        DECFSZ  DELAYGPR1, F            ;
        GOTO    $+2                     ;
        DECFSZ  DELAYGPR2, F            ;
        GOTO    DELAY_100mS_0           ; 
        GOTO    $+1                     ; 99998 CYCLES
        RETURN                          ; 4 cycles (including call)

50 Millisecond Delay (1/20 Second)

DELAY_50mS  ; Actual delay = 0.05 seconds = 50,000 cycles               1/20 Second 
        MOVLW   0X0E                    ;
        MOVWF   DELAYGPR1               ;
        MOVLW   0X28                    ;
        MOVWF   DELAYGPR2               ;
DELAY_50mS_0                            ;
        DECFSZ  DELAYGPR1, F            ;
        GOTO    $+2                     ;
        DECFSZ  DELAYGPR2, F            ;
        GOTO    DELAY_50MS_0            ; 49993 cycles
        GOTO    $+1                     ;
        NOP                             ; 3 cycles
        RETURN                          ; 4 cycles (including call)

5 Millisecond Delay (1/200 Second)

DELAY_5mS ; Actual delay = 0.005 seconds = 5000 cycles                  1/200 Second
        MOVLW   0XE6                    ;
        MOVWF   DELAYGPR1               ;
        MOVLW   0X04                    ;
        MOVWF   DELAYGPR2               ;
DELAY_5mS_0                             ;
        DECFSZ  DELAYGPR1, F            ;
        GOTO    $+2                     ;
        DECFSZ  DELAYGPR2, F            ;
        GOTO    DELAY_5mS_0             ; 4993 cycles
        GOTO    $+1                     ;
        NOP                             ; 3 cycles
        RETURN                          ; 4 cycles (including call)

1 Millisecond Delay (1/1000 Second)

DELAY_1mS  ; Actual delay = 0.001 seconds = 1,000 cycles               1/1000 Second 
        MOVLW   0XC6                    ;
        MOVWF   DELAYGPR1               ;
        MOVLW   0X01                    ;
        MOVWF   DELAYGPR2               ;
DELAY_1mS_0
        DECFSZ  DELAYGPR1, F            ;
        GOTO    $+2                     ;
        DECFSZ  DELAYGPR2, F            ;
        GOTO    DELAY_1MS_0             ; 993 cycles
        GOTO    $+1                     ;
        NOP                             ; 3 cycles
        RETURN                          ; 4 cycles (including call)

Note: All delay routines were generated using the Delay Code Generator from piclist.com

line