Searching \ for '[PIC]: How to calculate delay?' 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/microchip/time.htm?key=delay
Search entire site for: 'How to calculate delay?'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: How to calculate delay?'
2000\11\03@042517 by William

flavicon
face
Hi!

I am using PIC16F84, works at 4Mhz. I like to know how to
calculate a delay routine timing, like example below:-

----------------
wait
   movlw .200 ; load count with decimal 200
   movwf count
loop
   decfsz count, 1        ; decrement and skip next line if zero
   nop
   goto loop
       return
-----------------

Thanks.


--
Regards,

William Tan

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
"[PIC]:","[SX]:","[AVR]:" =uP ONLY! "[EE]:","[OT]:" =Other "[BUY]:","[AD]:" =Ads




2000\11\03@043143 by Roberto Casas

picon face
If you are working with MPLAB you can use the stopwatch window
I think that your routine must be:
> ----------------
> wait
>     movlw .200 ; load count with decimal 200
>     movwf count
> loop
>     decfsz count, 1        ; decrement and skip next line if zero
>     goto loop
>         return
> -----------------

{Original Message removed}

2000\11\03@085727 by Scott Dattalo

face
flavicon
face
On Fri, 3 Nov 2000, William wrote:

> Hi!
>
> I am using PIC16F84, works at 4Mhz. I like to know how to
> calculate a delay routine timing, like example below:-
>
> ----------------
> wait
>     movlw .200 ; load count with decimal 200
>     movwf count
> loop
>     decfsz count, 1        ; decrement and skip next line if zero
>     nop
>     goto loop
>         return
> -----------------

The answer is: you add up the instruction cycles. Fortunately, this is easy to
do with a PIC.

1 instruction = 4 cpu clock cycles
(e.g. If you're running the PIC at 4Mhz, 1 clock cycle is 250 ns and 1
instruction cycle is 1 uS.)

Look at the instruction set data sheet and find the number of cycles for each
instruction.


wait
   movlw .200      ;1; load count with decimal 200
   movwf count     ;1
loop
   decfsz count, 1 ;1     ; decrement and skip next line if zero
   nop             ;1
   goto loop       ;2
       return

So there are 2 cycles for initialization and 4 cycles for each
loop. Unfortunately, there's a bug and the loop will NEVER terminate! So this
code will delay indefinitely!

But that's easy to fix:

wait
   movlw .200      ;1; load count with decimal 200
   movwf count     ;1
loop
   decfsz count, 1 ;1     ; decrement and skip next line if zero
    goto loop      ;2 if executed, 1 if skipped
   return          ;2

again, 2 initialization cycles.
Most of the time you'll be looping. Each iteration through the loop is 3
cycles. On the last case, when count is decremented to zero, the goto is skipped
and the return is executed.

Examples. If count were set to 1 instead of 200, then the number cycles it'd
take for this to execute are:

2 (for init) + 1 (dec) + 1 (skipped goto) + 2 (return) = 6

If count = 2, then the total is:

2 (init) + 1 (dec) + 2 (goto) + 1 (dec) + 1(skip goto) + 2 (ret) = 9

If count = 3

2(init) + 2*(1 (dec) + 2 (goto)) + 1(dec) + 1(skip) + 2 ret = 12

And you probably see the pattern. The total cycles is (N+1)*3. So the answer to
the question is that this would take (201)*3 = 603 cycles or 603 uS if the
processor is running off a 4Mhz crystal. (Note if count is set equal to zero,
the delay is not 3 cycles, it's 257*3 cycles).

Also, don't forget that there are two extra cycles if this were a function that
was called as opposed to one that is inlined.

Clear?

Scott

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
"[PIC]:","[SX]:","[AVR]:" =uP ONLY! "[EE]:","[OT]:" =Other "[BUY]:","[AD]:" =Ads




2000\11\03@091823 by Andy Stubbins

flavicon
picon face
You could try this link

www.piclist.com/techref/default.asp?from=/techref/piclist/codegen/&ur
l=delay.htm

Most useful !


{Original Message removed}

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