RSLogix500: JSR and RET

Tharon

Member
Join Date
Jan 2007
Location
Other
Posts
1,430
I was told that all subroutines should have a RET in them, even if the subroutine runs til the end of the ladder, then the last rung should be a RET.

I have many PLC in the shop, all use subroutines (For organization) and only about half of them use RET at the end of each new ladder (the most recent ones, after I was told about using RET).

Is there any side effect to not using RET at the end of a ladder that was entered with a JSR command?
 
Nope. No effect at all. At the end of a ladder scan, control always returns to the caller.

The only time I use RET instructions for SLC's if if I want to return early from a sub.

With the PLC-5 and Logix Processors, Returns are only needed if you wish to return early from a sub, OR you are returning parmeters to the caller.

With the 500's, the SBR and RET instructions are mostly useless. Again, with PLC-5 and Logix processors, you need to use an SBR instruction if you are passing in parameters, but only then.
 
That's an interesting question!

I have seen the same thing and I suspect that the CPU treats the END rung as a RET, but I don't know for sure.

I would expect a runtime fault of some sort if it did not.

There should be some way to test that:

If (in Ladder 2 MAIN) you JSR to Ladder 5 for example, then from within ladder 5 jump back to Ladder 3, and ladder 3 does not have a RETurn, then you would expect the logic to hang in an infinite loop, and set a watchdog timeout fault.

Maybe one of the gurus can help answer this question with certainty.
 
SBR and RET are completely optional instructions.

From RSLogix500 help
This output instruction marks the end of subroutine execution or the end of the subroutine file. It causes the processor to resume execution in the main program file at the instruction following the JSR instruction where it exited the program. If a sequence of nested subroutines is involved, the instruction causes the processor to return program execution to the previous subroutine.
The rung containing the RET instruction may be conditional if this rung precedes the end of the subroutine. In this way, the processor omits the balance of a subroutine only if its rung condition is true.
Without a RET instruction, the END statement (always present in the subroutine) automatically returns program execution to the JSR instruction in your calling ladder program.

The only time I program a RET in Logix500 is if I want to exit the subroutine early. I never bother with the SBR instruction in Logix500.

However, in the PLC5 and ControlLogix platforms, you can pass parameters into and back out of subroutines. If you are passing parameters, SBR and RET are required as they define the parameters to be passed and returned. The SLC500 cannot pass parameters to subroutines.
 
OkiePC said:
That's an interesting question!

I have seen the same thing and I suspect that the CPU treats the END rung as a RET, but I don't know for sure.

I would expect a runtime fault of some sort if it did not.

There should be some way to test that:

If (in Ladder 2 MAIN) you JSR to Ladder 5 for example, then from within ladder 5 jump back to Ladder 3, and ladder 3 does not have a RETurn, then you would expect the logic to hang in an infinite loop, and set a watchdog timeout fault.

Maybe one of the gurus can help answer this question with certainty.

Actually, when you JSR from 2 to 5, you wouldn't return to ladder 3, you would return to 2, then proceed on to 3. If you JSR from 5 to 3, 3 would then return to 5, which would then return to 2. There wouldn't be any problems.
 
Actually, when you JSR from 2 to 5, you wouldn't return to ladder 3, you would return to 2, then proceed on to 3. If you JSR from 5 to 3, 3 would then return to 5, which would then return to 2. There wouldn't be any problems.

Does this not contradict this?

Nope. No effect at all. At the end of a ladder scan, control always returns to the caller.
 
Hey Guys,


Here is my $10 question I just recieved a new lap top of course no serial port. My old one did not have one either, but I used a Quantech PCMCIA card for two ports. Well the new lap top has an express slot. What would you do? The machine also has blue tooth.
Thank

jim cav
 
OZEE said:
Actually, when you JSR from 2 to 5, you wouldn't return to ladder 3, you would return to 2, then proceed on to 3. If you JSR from 5 to 3, 3 would then return to 5, which would then return to 2. There wouldn't be any problems.

If the CPU did not treat the END as a RET there would be a problem because it would follow through from ladder 3 to 4 (if it existed) then 5, then hit the jump back to 3 again.

Since Alaric clarified the fact that the SLC (and PLC5?) does treat the END as a RET, there is no problem omitting the RET instruction.
 
scottmurphy said:
Does this not contradict this?

The JSR/RET stack always unwinds, like a LIFO buffer.

Execute - LAD-2 -- JSR 5
-- interrupt LAD2 Processing --
Execute - LAD-5 -- JSR 3
-- interrupt LAD5 Processing --
Execute - LAD-3 -- JSR 19
-- interrupt LAD3 Processing --
Execute - LAD19 -- RET/END
-- Leave LAD19 for LAD3
Execute - LAD3 - Instruction just after JSR
LAD3 - RET/END
Execute - LAD5 - Instruction just after JSR
LAD5 - RET/END
Execute - LAD2 - Instruction just after JSR
LAD2 - RET/END
Execute - LAD2 - from start again (cyclic scan)

So, the call stack is :
LAD2->LAD5->LAD3->LAD19->LAD3->LAD5->LAD2
 
OkiePC said:
If the CPU did not treat the END as a RET there would be a problem because it would follow through from ladder 3 to 4 (if it existed) then 5, then hit the jump back to 3 again.

Since Alaric clarified the fact that the SLC (and PLC5?) does treat the END as a RET, there is no problem omitting the RET instruction.

It would be a problem only if the program went automatically from 2 to 3 to 4 to 5 to ... However, it doesn't do that. It never goes outside of 2 unless it is jumped out. The only way to get into 3 or 4 or 5 or ... is for it to be jumped to. If you have nothing to jump it out of 2, the code in any other program file will never be executed. If the ladder 4 is never called, it doesn't matter how much code is there, it won't be executed.

If there isn't an explicit END/RET at the end of a program file, the CPU will assume an implicit one.

I think rdrast did a great job of explaining the jump stack.
 

Similar Topics

So here's my situation, I have been tasked with modifying the logic to mimic a button press in the PLC. I have two identical machines however one...
Replies
6
Views
538
Hello, I'm new to programming. I'm using RSLogix500 to modify an existing program for a SLC500. My plan was to use one of the existing inputs...
Replies
26
Views
1,983
So i have an allen bradley rack (2080-LC50-24QWB), i have it connected through ethernet. I dont have an offline program so im trying to upload...
Replies
6
Views
676
Hi Everyone, I am supporting a system which runs on a SLC 500 (1747-L532). A fault has developed whereby the output state of some digital outputs...
Replies
10
Views
997
Hello, I am currently taking a course on plc programming and in the course rslogix 500 is being used. I am however having a problem since it keeps...
Replies
4
Views
1,695
Back
Top Bottom