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 Calculation Without a Software

Status
Not open for further replies.

Suraj143

Active Member
Hi! I have a simple delay routine listed below. Now I want to calculate its exact value in microseconds (uS).
I have many DELAY CODE GENERATORS & PICLOOPS software’s I DON'T want to use them I want to calculate in real life.

20h, 21h are GP registers loaded as 00h

Code:
Delay	decfsz 	20h,F
	goto	Delay
	decfsz	21h,F
	goto	Delay
	return

Here is my calculation Tell me am I right

Code:
 	[{(1+2) * 255} + 2]  * [{(1+2) * 255}] + 2 + 2

	[765 + 2] * [765] + 4

	586759 cycles = uS

	0.586 Seconds
 
Can't you just count up all the instructions and multiplty it by 4 (for 4 clock cycles per instructions on a PIC) and then multiplty it by the oscillator period?

It's dependent on the clock frequency being used...which you did not specify. I only count 4-6 instructions which is nowhere near equal to 586759 cycles. And if the registers have 00h in them, it's only several cycles that the code runs. (Actually Im not sure what decfsz will do since it will try to decrement 0 by one giving you a negative 1. THen it will check to see if the negative 1 is zero or not in order to decide to skip.)
 
Last edited:
Hi I forgot to tell I'm using 4MHz crystal. And the registers are not equal to 00H
sorry for that.I just declared in the beginning.So it has its full 8 bits.

Can somebody tell me the exact value how to calculate.
Thanks again
 
This is how I would calculate it,
Code:
Delay	decfsz 	20h,F	;1	
	goto	Delay	;2	=3*256-1
	decfsz	21h,F	;	+1
	goto	Delay	;	+2	=((3*256-1)+1+2)*256-1
	return 		;		+2	total = 197121
The minus 1s are due to the fact that on the last pass the decfsz will skip and so the loop is 1 cycle shorter.

Mike.
 
Hi mike thanks a lot now I understood very well.

But a small question the SKIP moment (last past) in DECFSZ is it takes 1 cycle or two cycles?

Thanks a lot.
 
The decfsz takes 1 cycle if it doesn't skip - 2 if it skips. So on the final loop the skip will take 2 but the goto doesn't take any.

Mike.
 
I've found it easier to create DelayUS() routines based on counting small loop times. Perhaps one of the following routines for a 4 MHz clock may be suitable for your needs;

Code:
        radix   dec
;******************************************************************
;                                                                 *
;  DelayUS(8..1031), 4 MHz clock      Mike McLaren, K8LH, Jun'07  *
;                                                                 *
;  requires the use of constant operands known at assembly time!  *
;                                                                 *
;  7 words, 0 ram variables, 14 bit core                          *
;                            ^^^^^^^^^^^                          *
;  calling macro generates 2 instructions;                        *
;                                                                 *
DelayUS macro   delay           ; parameter 8..1031
        movlw   delay/4-1
        call    Delay4Tcy-(delay%4)
        endm
;                                                                 *
;  code for simulation testing;                                   *
;                                                                 *
SimTest DelayUS(1000)           ; delay 'n' usecs
        nop                     ; put simulator break point here
;                                                                 *
;******************************************************************
        nop                     ; entry point for (delay%4) == 3
        nop                     ; entry point for (delay%4) == 2
        nop                     ; entry point for (delay%4) == 1
Delay4Tcy
        addlw   -1              ; entry point for (delay%4) == 0
        skpz                    ; 
        goto    Delay4Tcy       ; 
        return                  ; 
;******************************************************************
Code:
;******************************************************************
;                                                                 *
;  DelayUS(16..262159), 4 MHz clock   Mike McLaren, K8LH, Jun'07  *
;                                                                 *
;  requires the use of constant operands known at assembly time!  *
;                                                                 *
;  12 words, 1 RAM variable, 14-bit core                          *
;                            ^^^^^^^^^^^                          *
;  the macro produces 4 instructions;                             *
;                                                                 *
DelayUS macro   delay           ; parameter range 16..262159
        movlw   high((delay-16)/4)+1
        movwf   TMRH
        movlw   low ((delay-16)/4)
        call    DelayLo-(delay%4)
        endm
;                                                                 *
;  example code for simulation testing;                           *
;                                                                 *
SimTest DelayUS(262159)         ; delay 'n' usecs
        nop                     ; put simulator break point here
;                                                                 *
;******************************************************************
;                                                                 *
Delay.16F
        nop                     ; entry point for delay%4 == 3
        nop                     ; entry point for delay%4 == 2
        nop                     ; entry point for delay%4 == 1
DelayLo addlw   -1              ; subtract 4 cycle loop time
        skpnc                   ; borrow?  yes, skip, else
        goto    DelayLo         ; do another loop
        nop                     ;
DelayHi addlw   -1              ; subtract 4 cycle loop time
        decfsz  TMRH,F          ; done?  yes, skip, else
        goto    DelayLo         ; do another loop
        goto    $+1             ;
        return                  ;
;******************************************************************
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top