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 Loops?

Status
Not open for further replies.
Hi,

I have pretty much difficulty studying how does the delay loop work. From the training kit sheets from the PIC Kit 2, it said something about the cycles,counts and clocks. But the explaination is really confusing and I kinda got stuck here. Sometimes they overlap each other. :confused:

Also, I noticed some goto $ + 1. What do they mean?
 
Hi,

I have pretty much difficulty studying how does the delay loop work. From the training kit sheets from the PIC Kit 2, it said something about the cycles,counts and clocks. But the explaination is really confusing and I kinda got stuck here. Sometimes they overlap each other. :confused:

Also, I noticed some goto $ + 1. What do they mean?

hi,

Look here:


The expression goto $ + 1, refers to Program Counter Value ie: $

Goto Program Cntr + 1
 
it said something about the cycles,counts and clocks. But the explaination is really confusing and I kinda got stuck here.


Its not that bad -

First of all there is your Xtal or internal osc. frequency, let say 4meg.

The Pic chips system clock uses that frequency but after dividing it by 4, so the system clock is F/4 or 4meg / 4 = 1meg.
( you can work out what that is as a faction of a second )

Given these figures you can work out how many times it has to loop around to cause a delay of say 1ms.
For long delays, say 250ms, you often see the 'loop' made up of two or more loops within it.

However thats a bit much for most folk, so thats why you have those handy little delay calculators are mentioned earlier.

hth
 
I don't want to hi-jack this topic, but I am currently confused with the $+2 or $+1 command also.

Eric's link describes it as a small 2 cycle delay.

I am looking at it in Nigel's led blinking tutorial

Code:
Delay_0
 decfsz counta, f
 goto $+2
 decfsz countb, f
 goto Delay_0
 decfsz count1 ,f
 goto d1
 retlw 0x00
 end

whats the point of wasting 2 clock cycles here? Does it skip over somthing. I am confused.
 
It instructions to jump $+2 is the same as skipping over by 1.

$ = Current Location
+2 (PIC18) = 1 for this location and 1 for the next so it skips current and next. So it jumps over the next instruction
 
Last edited:
Ok, rereading back the tutorials supplied and with the help of the Microchip's tutorial webpage given just from a few posts back, I could calculate the number of cycles provided.

Code:
LOOP    decfsz count1   ...(1) }  Total => (3 * 256) = 768  }
        goto LOOP        ...(2) }                          } Total =>
                                                            } (3+768)*256
        decfsz count2   ...(1)                             }  = 197,376
        goto LOOP        ...(2)                             }
^ code above generates approx 200msec delay. I got that part easily.

But not this one below, even if I applied the calculation methods given from the first one: (below one is 250msec delay)

Code:
movlw   0x4F
movwf   count1
movlw   0xC4
movwf   count2

LOOP    decfsz count1   ...(1) }  Total => (3 * 79) = 237   }
        goto LOOP        ...(2) }                          } Total =>
                                                           } (3+237)*196
        decfsz count2   ...(1)                             }  = 47,040 (?)
        goto LOOP        ...(2)                             }

goto     $+1                 ...(2)
But the total is 47,040 and not around 250,000, what have I could be missing here? :)
 
But the total is 47,040 and not around 250,000, what have I could be missing here? :)

Simply because your delay code is wrong!!! Look carefully at the target of the first GOTO statement of the correct version.

Code:
; Delay = 250000 instruction cycles
; Clock frequency = 4 MHz

; Actual delay = 0.25 seconds = 250000 cycles
; Error = 0 %

			;249998 cycles
	movlw	0x4F
	movwf	count1
	movlw	0xC4
	movwf	count2
LOOP
	decfsz	count1, f
	goto	[color=red][b]$+2[/b][/color]         ;<<<<<<<< !!!!!!!!!!!
	decfsz	count2, f
	goto	LOOP        ;[color=red][b]here is $+2 for the above GOTO[/b][/color]

			;2 cycles
	goto	$+1
 
Ok, so the "goto $ + 2" is the correct answer.

Right now it's easy for me to make a simple delay from nested loops which involves just going back to the loop twice, or using the "goto $ - 1" technique, which could be simpler.

However, I can't figure out how they get 249998 cycles from the "goto $ + 2" method.. :confused:
 
Hi,

Hope this will clear up your delay calculation problem -

You have worked count1 loop out on your initial microchip example of xx times 3 instruction cycles -

but in the 250ms delay, your code loop is:-

decfsz count1 = 1 inst
goto $+2 = 2 inst
goto Loop = 2 inst

so you have a loop delay of 5 inst. ie 4F x 5

However, on the second time round this loop, count1 is now set at 00, so it counts down by 256 time on all subsequent passes .
This means you have 1 pass at 4F x 5 and all others at FF x5.
This calculates to 1281, then x count2 of C4, which equals 250,000

As you can now see, count1 for the first pass is just used as an adjusting method

If you can, run your code in MPLAB-Debugger -Sim and open the StopWatch in the Debugger dropdown -this will let you see each step down to a single instruction.
 
You've got to be careful when using
Goto $+1

In a 12 or 14bit core PIC it will branch +1
in a 16bit core PIC you must double the number to branch +1 (Goto $+2)
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top