All good questions! The main way that I use subroutines is very simple: break my logic up into different routines for ease of navigation and troubleshooting. Then, in the main routine, call each and every subroutine in order. The main routine does nothing else but call all of the other routines.
That's not to say its the
only way of using JSR and subroutines - and in fact I have used them in several other ways as the application demands - but this is probably the simplest way to do things and if you're still "cutting your teeth", probably the best approach to make your own life easy while you work things out.
defcon.klaxon said:
First off, I'm not passing any parameters when I'm calling subroutines; should I be? I mean I know it's optional, what I'm actually asking is there some sort of error checking or keep-from-locking-up-in-an-infinite-loop sort of reason to add a parameter and pass it from main program to subroutine?
You don't need to pass parameters - if you're using the JSR's as I described above, all the instruction has to do is say "run that routine", "now run that routine", etc etc. An example of where you WOULD pass a parameter might be: you have 100 bins which all have one of 20 different products in it. Your program needs to find product 7. So, you write a subroutine that, given a product number, returns the first bin that (a) has product 7 in it, and (b) is not empty. Then, at the point your PLC needs to know where that product is, you use a JSR to call that subroutine, and pass the value "7" to the input. The subroutine finds product 7 in bin 56, so it returns a value of 56, and your code carries and perhaps sends a robot down to get bin 56.
defcon.klaxon said:
Secondly, if I'm not passing parameters then it doesn't seem like I need any SBR or RET instructions; is that true?
Correct. You only need to use those if you are handling parameters; otherwise the subroutine will get to the end and return automatically.
defcon.klaxon said:
Third, how does Logix5000 actually respond to the JSR instruction? What I mean is, in my MainProgram I jump to a subroutine and it seems to come back to the MainProgram automatically; does it run the subroutine logic once and then come back to the MainProgram where it left off?
Yes. If the PLC encounters a JSR instruction, it goes and executes that subroutine all the way to the end, then returns to the JSR instruction that called it, and carries on with the next instruction in the rung/next rung in the routine.
defcon.klaxon said:
Do I need an END instruction at the end of a subroutine?
Not explicitly. You'll note that Logix automatically puts in "end" rung at the end of each subroutine, and you can't delete that. You don't need a separate end instruction. There are occasions where you might want to stop executing a subroutine early and return back to the JSR, but they are reasonably rare and again, I'd recommend not delving too far into that sort of thing upfront as it can make things confusing, and usually for no good reason
defcon.klaxon said:
Is there any reason why I'd want to be able to jump to a subroutine and stay there, or is that just asking for system hangups? Should I be controlling how often I jump to subroutines to keep the system from being overtaxed?
This is perhaps not quite the right question to be asking. The PLC doesn't just "stay" in a subroutine, unless you code in an infinite loop, in which case you'll get a major fault on the task watchdog and your PLC will stop. I think your best approach is as I mentioned first up - just call all subroutines sequentially and let the PLC run through everything continuously. You really shouldn't have issues of "overtaxing" your PLC by calling routines that aren't necessarily "needed" right now, not in this day and age with modern processors - and if you did, you'd have deeper issues than just the way you were calling your routines.
Hope that gives you some sort of useful information!