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.

Using picloops to calculate a timed delay

Status
Not open for further replies.

ryan_ryan

New Member

  • Code:
    Delay        
    
                    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         0x00


Hi, i was recommended to picloops to write delay subroutines for timed delays. For this particular 1 sec delay, it was calculated to have a total of 1000002 machine cycles and hence a 1.0000020sec delay.

My main problem now is that how do we actually calculate the total number of machine cycles, i have run the program on the simulator and have a rough idea but i am unable to work out the maths for the whole number of machine cycles.

Pls help.Thanks.
 
You still havent go it eh?
Alright. Give you 3 simple scenarios.

Code:
   movlw   D'1' 
   movwf   A

loop
   decfsz A
   goto exit

exit
   nop
This will take (1+1)+(2)+(1) clock cycles

Code:
   movlw   D'2' 
   movwf   A

loop
   decfsz A
   goto exit

exit
   nop
This will take (1+1)+(1+2)+(1) clock cycles.

Get it?

Code:
   movlw   D'2' 
   movwf   A

loop
   decfsz A
   goto loop

exit
   nop
Now, you get (1+1)+(1+2)+(2)+(1) clock cycles. I hope this is clear enough.

Lastly, the "call" and "retlw" instructions each take 2 cycles.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top