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 Question

Status
Not open for further replies.

uaefame

New Member
Hello everyone 'Good Morning' Its Morning here :)

Code:
Delay2sec
			;1999996 cycles
	movlw	0x11
	movwf	d1
	movlw	0x5D
	movwf	d2
	movlw	0x05
	movwf	d3
Delay2sec_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay2sec_0

			;4 cycles (including call)
	return

These are the codes generated from a delay generator website.

I am interested in knowing $? and $+2? What does it mean?

If this is simple question don't flare at me point me to good website so I can read more about PIC.

Learning PIC is more fun than writing reports :) Just a side comment :)
 
Last edited:
$ is assembly for the current program counter value. It points to the next instruction to be executed so if the next instruction takes 2 bytes "goto $+2" skips the next instruction
 
Thanks for replying

$ is assembly for the current program counter value. It points to the next instruction to be executed so if the next instruction takes 2 bytes "goto $+2" skips the next instruction

Can You explain more?

I understand that d1 is loaded with a value of 0x11 which is 17 decimal.

decrease 1 from it, it will be 16 right. Then goto $+2? This line confuses me?
 
You'll find the older 16F have a few quirks that are gone in the 18F
Wait till you hit the RMW bug. The good news is the new 14bit core fixes the quirks of the older 14bit core. Also seems the older 33 instruction core is at the end of development in favor of the updated 44 instruction core.
 
Hi,

goto $

is go to the current program location, which is also the same as:

Label
goto Label

while goto $+2, it can be said go to the next two program location. So:

Code:
    decfsz    d1, f
    goto    $+2
    decfsz    d2, f
    goto    $+2
    decfsz    d3, f
    goto    Delay2sec_0
is also the same as:

Code:
Label1
    decfsz    d1, f
    goto    Label2
    decfsz    d2, f
Label2
    goto    Label3
    decfsz    d3, f
Label3
    goto    Delay2sec_0
 
Do you think, it should look like this

Code:
Label1
    decfsz    d1, f
    goto    Label2  
Label2
    goto    Label3
    decfsz    d2, f  
Label3
     decfsz    d3, f
     goto    Delay2sec_0

Or Am I wrong?

I understand goto $ very well explained when you explained goto $+2 I was lost.

BTW, I am interested in activating stopwatch in simulation any idea what to do or press?

Sorry for my Newbie questions!!

Thanks in advance
 
Last edited:
Do you think, it should look like this

Code:
Label1
    decfsz    d1, f
    goto    Label2  
Label2
    goto    Label3
    decfsz    d2, f  
Label3
     decfsz    d3, f
     goto    Delay2sec_0

Or Am I wrong?
No, it should not look like that. You are wrong! :D

In this code:
Code:
Delay2sec_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
goto $+2 means move the program counter from the current program memory location to current memory location + 2. So PC + 1 = decfsz d2,f. And PC + 2 = the next goto $+2. That means it skips right to the bottom every time until that variable finishes counting down and becomes zero.

Have a look at Figure 13-1 on page 103
bleh.jpg
Each instruction takes 14 bits. Each time the program counter increments, it moves 14-bits higher in memory (to the next instruction).

Goto $+1 means move the PC (Program Counter) from the current position to the current position + 1 (increment it). In other words it just goto's to the next instruction. Pointless of course, since it was going there anyway. :D

Goto $+2 means move the PC two instructions ahead, skipping the next instruction. You can do the same thing with a label, but for little short skips like that, why bother? It's obvious (not to you yet, but you'll get it. :p) what you're doing.

Clear as mud, right? :D
 
Last edited:
The OP was lost because he sees "GOTO $+2" meaning GOTO$ first, then adds 2 to the value of whatever variable.

Of course it should be GOTO(current address+2).
 
what is f? how is it 1?

Code:
decfsz d1, f

The instruction is: decrement the value in 'd1' register and place the result back into 'd1' .
The 'f' is the part of the instruction which tells the program where put the result of the decrement.
If the Instruction was decfsz d1, W it would decrement 'd1' and put the result in 'W', the working register.

OK.:)
 
Code:
decfsz d1, f

The instruction is: decrement the value in 'd1' register and place the result back into 'd1' .
The 'f' is the part of the instruction which tells the program where put the result of the decrement.
If the Instruction was decfsz d1, W it would decrement 'd1' and put the result in 'W', the working register.

OK.:)



I thought this instruction says ‘Decrement the register (in this case d1) by the number that follows the comma.
 
Last edited:
Code:
decfsz d1, f

The instruction is: decrement the value in 'd1' register and place the result back into 'd1' .
The 'f' is the part of the instruction which tells the program where put the result of the decrement.
If the Instruction was decfsz d1, W it would decrement 'd1' and put the result in 'W', the working register.

OK.:)



I thought this instruction says ‘Decrement the register (in this case d1) by the number that follows the comma.
 
I thought this instruction says ‘Decrement the register (in this case d1) by the number that follows the comma.

No, the decfsz d1,f and other 'dec's always change the contents of d1 reg by '1'.
Likewise a incf d1,f would always increment the contents of d1 reg by '1'.

If you wanted to change the contents of d1 reg by more than '1' you could use
add or sub intructions.

EDIT:
All of these instructions are on the PIC datasheet
 

Attachments

  • esp01 Nov. 13.gif
    esp01 Nov. 13.gif
    31.3 KB · Views: 113
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top