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 time calculation

Status
Not open for further replies.

Ken Ho

New Member
For a normal cascaded 2 loops delay

Test Decfsz a,f
goto Test
Decfsz b,f
goto Test

the delay time is 3 X a X b cycles

but I need longer time so I have cascaded 3 loops

Test Decfsz a,f
goto Test
Decfsz b,f
goto Test
Decfsz c,f
goto Test

I plug in this formula 3 X 3 X a X b X c cycles

but the actual timing is much longer than this. Have anyone cascaded 3 loops for delay before? How do I get the exact delay time from this loops? Thanks a lot for reading
 
Setup a simple project with just your delay code. Turn on the Simulator and the Stopwatch. If you're not familiar with these facilities it's time to start reading the MPLAB Help files.

There's also a Delay Code Generator on PICLIST.

Regards, Mike
 

Attachments

  • Delay Stopwatch.JPG
    Delay Stopwatch.JPG
    28.8 KB · Views: 430
Pommie said:
The delay is,

Delay = a*3 + b*770 + c*197122 - 197883

I see how 3 cycles is obtained from 1 instruction cycle from decfsz
and 2 instruction cycles from goto. But how do you come out with
770 and 197122?
 
Every time b is decremented a will be zero which will be decremented 256 times before it is zero again. Hence,

770 = 256*3+2
197122 = 256*770+2

the -197833 is to compensate for the fact that writing all 1's to the registers will cause it to drop straight through without any looping. 197833 = 3+770+197122-12 the 12 is the number of cycles to drop straight through.

Mike.
 
Last edited:
Not good practice

You asked for an exact delay time
Ken Ho said:
.... How do I get the exact delay time from this loops? ...

I don't think loops can provide the EXACT part of your question. When an interrupt is serviced during your loops, they will be longer...
You don't have interrupts? Ok but it's still not good practice.

What if the user press a button while your in the delay loop? You will never see it.
You don't have buttons? Ok but still not good practice.

Use a timer and a counter :)
 
Sorry I am quite new to this. Where can I get the Delay Code Generator in the PICList that you mentioned?
 
How do I calculate the below loop?

Code:
;VARIABLE
CBLOCK 0X20
B
COUNTER0
COUNTER1
ENDC

;CONSTANT
A   EQU   .65

             MOVLW   A
             MOVWF   B

REPEAT:  MOVF      B,W
             MOVWF   COUNTER0

ON:        BSF         GPIO,1
             DECFSZ   COUNTER0,F
             GOTO      ON

            MOVWF   COUNTER

OFF:     BCF         GPIO,1
            DECFSZ   COUNTER0,F
            GOTO      OFF

           DECFSZ    COUNTER1,F
           GOTO       REPEAT

Thanks
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top