Searching \ for 'DELAY routine' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/timers.htm?key=delay
Search entire site for: 'DELAY routine'.

Truncated match.
PICList Thread
'delay routine'
1997\10\21@135713 by jack

flavicon
face
Hi all,

Is it possible to make a delay routine with the TIMER of the PIC16C84?
If so, please send me the routine. I'm looking for a delay routine of 20 msec.

With greetings ... Jack Raats


*************************************************************
*       Fidonet:        Jack Raats 2:285/751                *
*       Internet:       spam_OUTjackTakeThisOuTspamjarasoft.xs4all.nl             *
*************************************************************


'DELAY routine'
1998\12\12@104714 by Goovaerts
flavicon
face
Can anyone give me the source code for a DELAY of 1 second !!

Here is the code for a 3byte counter wich results in a delay of 1 second.
This code is written for a 4MHz crystal

I'm need the code for a 20 MHz crystal

DELAY
   MOVLW      005H
   MOVWF      COUNTER               ;MSB VAN DE DRIE BYTE TELLER
   MOVLW      016H
DELAY0
   MOVWF     COUNTER1
   CLRF          COUNTER2              ;LSB VAN DE DRIE BYTE TELLER
DELAY1
   DECFSZ     COUNTER2,F          ;DEZE INSTRUCTIE DUURT 1uS !
   GOTO          DELAY1                    ;DEZE INSTRUCTIE DUURT 2uS !
   DECFSZ     COUNTER1,F
   GOTO          DELAY1
   DECFSZ     COUNTER,F
   GOTO          DELAY1
   RETURN

Let me know something,

Glenn

1998\12\12@105542 by Mike Sauve

flavicon
face
At 04:42 PM 12/12/98 +0100, Goovaerts wrote...
>Can anyone give me the source code for a DELAY of 1 second !!
>
>Here is the code for a 3byte counter wich results in a delay of 1 second.
>This code is written for a 4MHz crystal
>
>I'm need the code for a 20 MHz crystal
>
>DELAY
>    MOVLW      005H

Change the above to:
    MOVLW      d'25'         ;005H times 5 (20Mhz/4Mhz =5) = 25 decimal

How hard was that?

{Quote hidden}

1998\12\12@105957 by Michael Hagberg

flavicon
face
since the 20MHz crystal is 5 times faster than the 4MHz crystal, try this

DELAY
   MOVLW      005H * 5   ; Do 5 times the number of loops

michael

You may leave the list at any time by writing "SIGNOFF PICLIST" in the
   body of a message to .....LISTSERVKILLspamspam@spam@MITVMA.MIT.EDU.


{Original Message removed}

1998\12\15@093653 by Hanafi Tanudjaja

flavicon
face
I tried your routine ,using MPSIM and I found out that the number
instruction pass from the begining to the end of routine 536609
instructions.
if you use 4Mhz crystal then the T crystal = 0.25 usec .T instruction = 1
usec.It means your routine isn't for delaying 1 sec because 536609 X 1 u sec
=
536609 u sec .You have to trim counter content to get 1 sec delay.
Correct me if I am wrong.

Hanafi T

{Quote hidden}

1998\12\15@151517 by Goovaerts

flavicon
face
I think you are wrong ! I have been told (and it's really quite obvious !)
that when I use a 20 MHz crystal, I simply have to multiply the constant
that indicates how many times it does the outher loop with 5 --> 20 Mhz / 4
MHz = 5

DELAY
{Quote hidden}

This way I become a delay of one second, I HOPE. Tomorrow I'm going to test
everything for the first time. So I'm a bit nervous, but everything will
work out !!

Thanks for your assistance.

Glenn

1998\12\16@100809 by lilel

flavicon
face
Let's see if we can do the math right on this one.  I have to work
this problem all the time, and I'd like to see if I'm approaching it
correctly.

At 20 Mhz, each clock tick takes 50 nanoseconds, each cycle takes 4
ticks, or 200 nanoseconds.



> DELAY
> >>    MOVLW      005H            -----------------> This one *5 = 25
> >>    MOVWF      COUNTER               ;MSB VAN DE DRIE BYTE TELLER
> >>    MOVLW      016H
> >> DELAY0
> >>    MOVWF     COUNTER1
> >>    CLRF          COUNTER2              ;LSB VAN DE DRIE BYTE TELLER

First loop:
> >> DELAY1
> >>    DECFSZ     COUNTER2,F          ;DEZE INSTRUCTIE DUURT 1uS !
> >>    GOTO          DELAY1                    ;DEZE INSTRUCTIE DUURT 2uS !

this loop takes 3 cycles and Counter2 will count 256 times before
skipping the GOTO statement.    200 nanoseconds * 256*3 = 153.6
microseconds.

> >>    DECFSZ     COUNTER1,F
> >>    GOTO          DELAY1

Now Counter1 is 16H the first time, but 0 the next time (watch this,
folks! It's confusing!)

First time through:  16H=22D.  22 * 153.6 microseconds = 3.38
milliseconds  (plus 600 nanoseconds to do the DECFZ and
GOTO which I'll ignore)

COUNTER is initialized to 5, so the next 4 times through, COUNTER1
will take 256*153.6 microseconds, or 39.3  milliseconds.


> >>    DECFSZ     COUNTER,F
> >>    GOTO          DELAY1
> >>    RETURN

Total time delay according to this is 39.3 millisec * 4 + 3.38
millisecs.  Hmmm, I don't come up with nearly 1 second.  More like
160 millisecs


>
> This way I become a delay of one second, I HOPE. Tomorrow I'm going
> to test everything for the first time.

Well, unless I'm missing something really basic, it won't.  Lets try
this:  1 second divided by 200 nanoseconds = 5 * 10e6.  We've got to
count this high to get the delay we are looking for.

Now it's likely that most systems will have a watchdog timeout in
this amount of time, so our inner loop could look like this:

DELAY1
       CLRWDT
       DECFSZ COUNTER2, F
       GOTO DELAY1

This loop cycles through in 4 cycles, or 800 nanoseconds.  There are
1.25*10e6 of these in a second.

1.25*10e6 / 256= 4.88*10e3      this accounts for one loop
(COUNTER1)

4.88*10e3 / 256 = 19.07              This accounts for the outer loop
(COUNTER)

So if we have  three loops, the first two initialized at zero and the
outer one itilialized at 19, we'll get a delay of about one second.

OK, fire away ye flamers, and tell me if this is wrong.  Here's the
whole bananna:


DELAY
        CLRF COUNTER2
        CLRF  COUNTER1
       MOVLW .19
       MOVWF COUNTER
DELAY1
      CLRWDT
       DECFSZ COUNTER2, F
       GOTO DELAY1                  ; 204 MICROSECONDS
      DECFSZ COUNTER1, F
     GOTO DELAY1                    ;52.4 MILLISECONDS
     DECFSZ   COUNTER, F
     GOTO DELAY1                    ; 996.14 MILLISECONDS
     RETURN

If you add in the overhead for doing the second and third loops I
come up with an additional 2.9 milliseconds for a total of 999
milliseconds.


-- Lawrence Lile

"Nyquist was an optimist."

=> Median Filter Source Code
=> AutoCad blocks for electrical drafting

at:  http://home1.gte.net/llile/index.htm

1998\12\16@163744 by lilel

flavicon
face
DAZLOGAN wrote, with endless wit:


> Blimey, Im glad Im not stuck in that assembly nonsense.
> Why dont you use proper structured languages ???
>
> Look at this example PIC C code to delay for 500 uS:
>
> DELAY_US(500)         ' Thats it !!! - easy or what ?

Yeah yeah. I know.  It's depressing.   But how many program memory
locations and variables does it use up?  I'm sticking this code into
a 12C508 and fighting for every byte.









-- Lawrence Lile

"Nyquist was an optimist."

=> Median Filter Source Code
=> AutoCad blocks for electrical drafting

at:  http://home1.gte.net/llile/index.htm

1998\12\16@194755 by Regulus Berdin

picon face
Hi all,

I make my delay routines somewhat isochronous.  This makes my life
easier in computing the number of cycles spent on the routine.

Example, the routine below will have a delay of 256*256*cnt1*7.  To have
approx. 1 second at 20MHz, cnt1 is set to 11.

;
;Delay 1sec@20MHz:
;
; cnt1 = Cycles/256/256/7 = (20000000/4)/256/256/7 = 10.899 = 11
;
;

delay:
       movlw   .11
       movwf   cnt1
       clrf    cnt2
       clrf    cnt3

dloop   decfsz  cnt3,f
        goto   $+1
       decfsz  cnt2,f
        goto   $+1
       decfsz  cnt1,f
        goto   dloop


regards,
Reggie

1998\12\17@094513 by lilel

flavicon
face
> Hi all,
>
> I make my delay routines somewhat isochronous.  This makes my life
> easier in computing the number of cycles spent on the routine.

This is a really neat structure for this routine.  What does
isochronous mean?  (obviously "Same Time" in literal translation)

.....  hmmm - does that mean that the loop takes the same time to
execute each time through?



{Quote hidden}

-- Lawrence Lile

"Nyquist was an optimist."

=> Median Filter Source Code
=> AutoCad blocks for electrical drafting

at:  http://home1.gte.net/llile/index.htm

1998\12\17@132735 by Andy Kunz

flavicon
face
>This is a really neat structure for this routine.  What does
>isochronous mean?  (obviously "Same Time" in literal translation)

It means that every path through a portion of a program takes the same
amount of time.  That is, the time to process and "if" == the time to
process and "else" condition.

Andy

==================================================================
Andy Kunz - Statistical Research, Inc. - Westfield, New Jersey USA
==================================================================

1998\12\17@193405 by Regulus Berdin

picon face
Lawrence Lile wrote:
> This is a really neat structure for this routine.  What does
> isochronous mean?  (obviously "Same Time" in literal translation)
>
> .....  hmmm - does that mean that the loop takes the same time to
> execute each time through?

Yes, it takes 7 cycles for all inner and outer loops.  Can also be extended to
delay more, if needed, by adding another decfsz and goto $+1 on top of the loop.

{Quote hidden}

regards,
Reggie

1998\12\17@201028 by Scott Dattalo

face
flavicon
face
On Thu, 17 Dec 1998, Regulus Berdin wrote:

{Quote hidden}

Shouldn't the goto $+1 instructions be goto $+2 ?

If they're supposed to be goto $+1, then I calculate that the delay time
is:

9 cycles per pass through the loop AND the cnt2 and cnt3 variables are
unnecessary.

However, if you have goto $ + 2, then the second and third decfsz
instructions are skipped, unless of course cnt3 goes to zero (or cnt3 and
cnt2 go to zero). This gives the 7-cycle isosynchronocity (we're really
starting to abuse this word!) to which you allude.

Scott

1998\12\17@203443 by Regulus Berdin

picon face
Scott Dattalo wrote:
> Shouldn't the goto $+1 instructions be goto $+2 ?
Yes, it should be goto $+2.  I calculated it at 7 cycles but I mistyped.
Sorry for the mistake.

regards,
Reggie

1998\12\19@050754 by Hanafi Tanudjaja

flavicon
face
>Example, the routine below will have a delay of 256*256*cnt1*7.  .
Why  7 ?
any explaination ?
If the number of counter four ,will the formula become 256*256*256*cnt1*7  ?


TIA
Hanafi T

1998\12\19@072110 by Michael J. Ghormley

flavicon
face
Hanafi Tanudjaja wrote:

> Example, the routine below will have a delay of 256*256*cnt1*7.  .
> Why  7 ?  any explaination ?

I'll have a go at it.  Please forgive me if I explain elementary things as this
is aimed
at the PICLIST in general and some newbies might need the extra info.

Regulus Berdin's delay routine below was supposed to give the original poster an
approximate one second delay on a 20MHz part.  He calculated that if he had isoc
hronous
loops of 7 cycles, then he had to perform 256 (the rollover of an 8-bit counter)
times
256 (same thing) times some number for the outermost loop.  Thus, he calculated:

cnt1 = Cycles/256/256/7 = (20000000/4)/256/256/7 = 10.899 ~= 11

So let's look at the code FROM THE PERSPECTIVE OF THE NUMBER OF CYCLES FROM dloo
p:

delay:
       ; initialization of the loop variables
       movlw   .11     ; this is the 11 from the calulation above
                       ; each increment of this variable would give you
                       ; another 458,752 cycles or approximately 0.09 seconds
                       ; on a 20MHz part
       movwf   cnt1
       clrf    cnt2    ; starts at 0 so that it takes 256 decrements to set the
Z flag
       clrf    cnt3    ; ditto
;
; NOTE: all notes are counting cycles from dloop -- not the overall cycles after
many
; iterations.  This is covered in the text below.
dloop
       decfsz  cnt3,f  ; if a zero is produced then this takes 2 cycles
                       ; as the next instruction is replaced by a NOP
;
       goto    $+2     ; here = the above decrement didn't produce a zero
                       ; so we have used 1 cycle from dloop to get here
                       ; now we are going to jump over the next decrement
                       ; this will take 2 more cycles
;
       decfsz  cnt2,f  ; here = we have a zero result in the decrement above
                       ; so it took 2 cycles from dloop to get here
                       ; if a zero is produced here then this takes 2 more cycl
es
                       ; as the next instruction is replaced by a NOP
;
       goto    $+2     ; we could have gotten here two different ways:
                       ; 1) we jumped here from the goto $+2 above which means
that
                       ;    it has taken 3 cycles from dloop to get here
                       ; or
                       ; 2) a non-zero was produced in the decrement of cnt2
                       ;    this means that it has taken 3 cycles from dloop to
get here
                       ;
                       ;    this goto will take 2 more cycles to get to the bot
tom of
                       ;    the routine
;
       decfsz  cnt1,f  ; to get here both decrements must have produced zero re
sults
                       ; thus, it has taken 4 cycles from dloop to get here
                       ; this insturction takes 1 cycle to get to the next line
       goto    dloop   ; we could have gotten here two ways:
                       ; 1) fallen through from the decrement of cnt1
                       ; or
                       ; 2) jumped here from the goto $+2 above
                       ;
                       ; either way it has taken 5 cyces from dloop to reach th
is spot
                       ; the goto will take 2 more cycles
                       ; thus total cycles is 7 per loop (inner, middle, or out
er)
                       ; no matter how we get here

As we can see, a non-zero result in the decrement of cnt3, performs jumps over t
he
decrement of cnt2 and cnt1, thus it will take 256 loops before it falls through.
 Each
loop takes 7 cycles so we get 256 * 7 = 1792 cycles before we decrement cnt2.

We can also see that a non-zero result in the decrement of cnt2 will jump over t
he
decrement of cnt1.  Thus it will take 256 decrements of cnt2 to decrement cnt1.
Each
loop of cnt2 will take 256 * 256 * 7 = 458,752 cycles before we decrement cnt1.

We decrement cnt1 eleven times before we fall through.  So we get:
256 * 256 * 7 * 11 ~= 5,046,272  (I actually count 5,046,275 cycles with initial
ization
and the fact that the last decrement of cnt1 will result in a 1-cycle NOP rather
than a
2-cycle GOTO, but who's counting?)

This is my best effort at explaining it.  If I have made a mistake, I'm sure I'l
l hear
about it!  d8^)

Michael

*************************************************************************
When the way of the Tao is forgotten, kindness and ethics must be taught.
Men must learn to pretend to be wise and good.  --  Lao Tzu
*************************************************************************

1998\12\20@092823 by Hanafi Tanudjaja

flavicon
face
Thank Michael,
for your explaination,  for me it also explain isochronous at the same time

1998\12\20@134504 by Scott Dattalo

face
flavicon
face
On Sun, 20 Dec 1998, Hanafi Tanudjaja wrote:

> Thank Michael,
> for your explaination,  for me it also explain isochronous at the same time

Pun intended?

I agree Michael, you did a superb job with that detailed explanation.

Scott


'Delay routine'
1999\05\03@124549 by Lalakis Parafyadas
picon face
part 0 16 bytes
</x-html>

1999\05\03@130421 by Sean Breheny

face picon face
Hi Argiris,

I did some work on precise delays a while back and posted it on my page.
It should get you an exact 1 sec delay. Note that the type of delay I
have posted there is the kind where the PIC can't bne doing anything else
(including interrupts!) at the same time. If you want interrupt-based
timing, let me know, I can help you there,too.

Have a look at:

http://www.people.cornell.edu/pages/shb7/lop.html

Good luck,

Sean


On Mon, 3 May 1999, Lalakis Parafyadas wrote:

> Hi there. I'm interested in a routine in ASM that causes a delay of one =
> second (exactly) before something happens. I need it to be as accurate =
> as possible. Thanks in advance, Argiris
>

1999\05\03@132131 by Windows-1252?Q?Sebasti=E1n_Dols?=

flavicon
face
-----


Hi Argiris,

I did some work on precise delays a while back and posted it on my page.
It should get you an exact 1 sec delay. Note that the type of delay I
have posted there is the kind where the PIC can't bne doing anything else
(including interrupts!) at the same time. If you want interrupt-based
timing, let me know, I can help you there,too.

Have a look at:

http://www.people.cornell.edu/pages/shb7/lop.html

Good luck,

Sean
----------------


And if you really wants something easy (for the next time ;) and pretty
exact (Time Delay = 1,000002 s with Osc = 4 MHz including call&return), take
a look at a program called PICLoops. I don't remember the URL, but probably
other PICsters will. The generated code from this timecruncher has been:

;Time Delay = 1,000002 s with Osc = 4 MHz
       movlw   D'6'
       movwf   CounterC
       movlw   D'24'
       movwf   CounterB
       movlw   D'168'
       movwf   CounterA
loop    decfsz  CounterA,1
       goto    loop
       decfsz  CounterB,1
       goto    loop
       decfsz  CounterC,1
       goto    loop
       retlw


I only have the mail address of the picloops coder, so here is the credit:

PicLoops written by: William J. Boucher, Dec.1998, <boucherspamKILLspamkent.net>

PS: Ahm.. and speaks parallax dialect, too (I *love* this kind of proggies
;).

1999\05\03@144400 by engelec

picon face
Argiris,

this is what I did before.
I tested with tech. scope using picmaster emulator. let experts
calculate see how accurate is it.
the reason I divided in two part because of I needed to
have blinking led at the same time so if you needed delete
"nop" put   bsf    port whatever then on next nop put bcf port
whatever.



1secdelay
            movlw     .10          ; load 10
            movwf     second       ; save it
            nop                    ;
            nop                    ;
            clrf     TMR0          ; reset timer 0
            movf     TMR0,W        ; load timer value in w to compare
            XORLW    .195          ; compare
            btfss    STATUS,Z      ; is it =
            goto     $ - 3         ; no try again
            decfsz   second,F      ; skip if second  = 0
            goto     $ - 6         ; no
            movlw    .10           ;
            movwf    second        ;
            nop                    ;
            nop                    ;
            clrf     TMR0          ;
            movf     TMR0,W        ;
            xorlw    .195          ;
            btfss    STATUS,Z      ;
            goto     $ - 3         ;
            decfsz   second,F      ;
            goto     $ - 6         ;
            return                 ;

More... (looser matching)
- Last day of these posts
- In 1999 , 2000 only
- Today
- New search...