Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

delay routine

Status
Not open for further replies.

josi

New Member
hi guys can anyone help , i am trying to implement a delay of 1/2 of a second .can anyone tell me if the following routine will produce 1/2 of a second.please respond !!!


DELAY ; delay subroutine



MOVLW d'250' ; delay 250 ms ( 4 MHZ clock )
MOVWF COUNT0
D1 MOVLW 0xC7
MOVWF COUNT1
MOVLW 0x01
MOVWF COUNT2

DELAY_PROCESS

DECFSZ COUNT1,f
GOTO $+2
DECFSZ COUNT2,f
GOTO DELAY_PROCESS
DEFSZ COUNT0,f
GOTO D1
RETLW 0x00
 
hi guys can anyone help , i am trying to implement a delay of 1/2 of a second .can anyone tell me if the following routine will produce 1/2 of a second.please respond !!!


DELAY ; delay subroutine



MOVLW d'250' ; delay 250 ms ( 4 MHZ clock )
MOVWF COUNT0
D1 MOVLW 0xC7
MOVWF COUNT1
MOVLW 0x01
MOVWF COUNT2

DELAY_PROCESS

DECFSZ COUNT1,f
GOTO $+2
DECFSZ COUNT2,f
GOTO DELAY_PROCESS
DEFSZ COUNT0,f
GOTO D1
RETLW 0x00
hi,
Look here:
 
Here's something which may be a little more versatile. It allows you to specify exact delays in cycles, microseconds, or milliseconds with almost any clock (4, 8, 12, 16, or 20 MHz).

The sub-system uses a relatively low overhead 12 word sub-routine and uses 4 words for each DelayCy() macro call (minimum, depending on the clock speed and the delay parameter).

You must set the clock equate to your clock frequency (in MHz).

Code:
        radix   dec
clock   equ     8               ; user clock frequency in MHz
usecs   equ     clock/4         ; cycles/microsecond multiplier
msecs   equ     usecs*1000      ; cycles/millisecond multiplier

DelayCy macro   pDelay          ; cycles (Tcy), minimum 16
        local   cycles
        cycles = pDelay
     while cycles > 262032
        movlw   high((262016-16)/4)+1
        movwf   TMRH
        movlw   low((262016-16)/4)
        call    DelayLo-(262016%4)
        cycles -= 262016
     endw
        movlw   high((cycles-16)/4)+1
        movwf   TMRH
        movlw   low ((cycles-16)/4)
        call    DelayLo-(cycles%4)
        endm[COLOR=Red]
;                                                                 *
;  example code for simulation testing;                           *
;                                                                 *
SimTest DelayCy(1000*msecs)     ; 1 second delay
        nop                     ; put simulator break point here[/COLOR]
;                                                                 *
;  Delay(16..262159 Tcy) subroutine   Mike McLaren, K8LH, Jun'07  *
;                                                                 *
;  12 words, 1 RAM variable, 14-bit core                          *
;                                                                 *
Delay.16
        nop                     ; entry point for delay%4 == 3    |B0
        nop                     ; entry point for delay%4 == 2    |B0
        nop                     ; entry point for delay%4 == 1    |B0
DelayLo addlw   -1              ; subtract 4 cycle loop time      |B0
        skpnc                   ; borrow?  yes, skip, else        |B0
        goto    DelayLo         ; do another loop                 |B0
        nop                     ;                                 |B0
DelayHi addlw   -1              ; subtract 4 cycle loop time      |B0
        decfsz  TMRH,F          ; done?  yes, skip, else          |B0
        goto    DelayLo         ; do another loop                 |B0
        goto    $+1             ; burn off 2 cycles               |B0
        return                  ;                                 |B0
;
Here's an example of how you might flash an LED at half second intervals (on for 1/2 second, off for 1/2 second, in an endless loop). Please note that this delay sub-system allows you to compensate for instruction cycle overhead in a loop.

Kind regards, Mike

Code:
loop
        bcf     PORTC,0         ; turn on LED on RC0
        DelayCy(500*msecs-1)    ; delay 500 msecs minus 1 cycle
        bsf     PORTC,0         ; turn off LED on RC0
        DelayCy(500*msecs-3)    ; delay 500 msecs minus 3 cycles
        goto    loop            ; loop forever
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top