ALL THE PLC Questions!

CallRET

Member
This is kinda common on forums.  Someone makes a thread with a list of questions and starts a Question Number.  If you have an answer, you make a post quoting the question number and the question, and post an answer.
 
For example
 
Q1 What is a label?
Q1 What is a label?
From page 22 in the PLC manual
 
Both GOTO and CALLSUB use label to identify where to execute next instruction.
Label can be any string less than 10 characters. Label cannot be identical to any
instruction keyword. If sub routines are used then they are coded after the END of
the main routine body. Sub routines start at their label and must end with the
instruction RET e.g.
TEST_IO_SUB:
#instructions here
RET
Subroutines can be called from the main program and from within other subroutines.
Note that WebControl PLC has a return program address stack depth of 8 (or call
stack 8).
 
And that would be the end of my post with the answer to the question.
 
So i'll continue with a few questions of my own.  Feel free to quote this in your answer, or just reference the question number if quoting is too complicated.
 
Q2 Can I call END inside a label?
 
Q3 Can I figure out how deep I am in the call stack?
 
Q4 Is there a best way to do the year sanity check for a power fail?
 
Q5 Can we get all the PLC questions in a sticky so we can have one place to look at all the already asked and answered questions?
 
Q2 Can I call END inside a label the program body (not at the end)
You should call GOTO START because it makes more sense.
 
Q3 Can I figure out how deep I am in the call stack?
Count the number of times you CALLSUB, CNZ, CZ and subtract the number of times you RET
 
Q4 Is there a best way to do the year sanity check for a power fail?
No just test it for a valid value
 
CallRET said:
Is there a short cut to empty the call stack and exit and restart the program?
 
No. (Although there was recently discussion about introducing a "reset" command)
 
Any properly constructed program will manage the stack automatically.
DON'T jump to a subroutine that exits with a RET.
DON'T jump out of a subroutine that was called with a callsub (or conditional version thereof)
It's quite straightforward. I would STRONGLY discourage introducing a "reset" command purely for the purposes of unmessing an otherwise borked stack!
 
rossw said:
DON'T jump to a subroutine that exits with a RET.
How should a subroutine exit if not with a RET?  Are the examples in the manual wrong?
 
Re: reset
While unborking a stack is a valid reason, i simply was looking for a quick exit to save time and lines of code.
 
CallRET said:
How should a subroutine exit if not with a RET?  Are the examples in the manual wrong?
 
Of course a subroutine should exit with a RET.
But you don't get INTO a subroutine with a JUMP. You CALL it (CALL is the same as a jump, except it pushes the return address onto the stack)
 
Just because you want to organize code into a subroutine you don't have to CALL it you can GOTO (aka jump or branch) to it and GOTO out of it

GOTO in GOTO out
CALL in RET out
 
Back
Top