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.

pic delay

Status
Not open for further replies.

josi

New Member
hi! i'd lik to use a 1s delay! can anybody help me? this is what i'v done so far


delay
movlw D'333333'
movwf count1
loop
decfsz count1,1
goto loop
return
end
i got the value 333333 using this formula: XX= (TD-5us)\3

xx is the value to decrement
TD is the required time delay
but this is not working! the led is constantly on. i know that the max value i can use is 255(11111111), but i need 1s delay! with 6MHZ XT.
can anybody help with a formula or method wich i can use for any delay?
thanx
 
Here's a somewhat universal delay sub-system I use which allows you to specify exact delays in cycles, microseconds, or milliseconds with almost any clock (4, 8, 12, 16, or 20 MHz). It's relatively low overhead with a 12 word sub-routine and uses 4 words for each DelayCy() macro call (minimum, depending on the clock and delay). You must set the clock equate to your clock frequency (in MHz).

Soapbox

Honestly I don't know why you guys keep recommending those barbaric loop routines from PICLIST. They're fine if you only need a single delay. Wouldn't you rather have a single general purpose routine that works with any clock? I think I'd fall off my chair if one of you were ever to recommend the method I've been promiting this last year (lol).

Mike

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
 
Last edited:
Here's a somewhat universal delay sub-system I use which allows you to specify exact delays in cycles, microseconds, or milliseconds with almost any clock (4, 8, 12, 16, or 20 MHz). It's relatively low overhead with a 12 word sub-routine and uses 4 words for each DelayCy() macro call (minimum, depending on the clock and delay). You must set the clock equate to your clock frequency (in MHz).

Soapbox

Honestly I don't know why you guys keep recommending those barbaric loop routines from PICLIST. They're fine if you only need a single delay. Wouldn't you rather have a single general purpose routine that works with any clock? I think I'd fall off my chair if one of you were ever to recommend the method I've been promiting this last year (lol).

Mike

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

Thanx mike! but this routine is too advanced 4 me! i'm still trying to find my way with the PIC! i do understand the instructions but i'm confused by this routine!
 
PICList is working but i'd lik to know another method coz i'd lik to do it manualy. i can't use the net to generate my dealys all the time! i might b required to rwite down by hand a peace of code with a specific delay during a class test.
 
Nigel's right. Learn how to count instruction cycles and learn how to use the MPLAB simulator and MPLAB "stop watch" and start experimenting.
 
PICList is working but i'd lik to know another method coz i'd lik to do it manualy. i can't use the net to generate my dealys all the time! i might b required to rwite down by hand a peace of code with a specific delay during a class test.


hi,
What is the clock [crystal] frequency of the PIC you are using.?
 
i'm using a 6Mhz XT for now but i'll change it later to 4Mhz
hi josi,

The PIC will internally divide the 4Mhz down to 1MHz [the 6MHz will be 1.5MHz]

The datasheet tells you that most of the PIC instructions [except just a few]
take 1 internal clock cycle to execute, thats 1µSec/instruction for the 1MHz.

So count the instructions in your delay loop and the number of times you good around the loop,
multiply those numbers and you will be quite close to the actual delay period.

It shows in the data sheet which of the few instructions that take 2 cycles.

Do you follow OK.?
 
Last edited:
thanx eric! thanx evry body i got what u said and i'll do my best to get it right. i hav to liv now! but u can still post your replies! i'll b back 2morow. thanx again
 
Status
Not open for further replies.

Latest threads

Back
Top