Subroutines

Justin

Member
Join Date
Feb 2004
Location
Florida
Posts
1
Hi everybody,

I'm working on a project where I've got to activate a subroutine and then inside the sub is a timer and once the timer is done it goes back to my main program and runs some more code. I'm totally new to the world of PLCs and have no clue how to activate the sub. Any ideas?

Justin
 
What are you trying to control? The program you've outlined is not the way a PLC typically runs. The PLC continuously executes its program. It doesn't sit in an idle state waiting for something to happen.

You could use a JUMP or MCR instruction to do what you want.


_____
----] [-----| TMR |
| |
|_____|

TMR.DN
---]/[------|Jump to Label|


.. }
.. }
.. }Other Logic
.. }
.. }

---|Label|



You don't necessarily need a subroutine to do it.


I suggest you rethink your program design, however. You appear to be trying to make the PLC's operating system conform with the way a VB program runs.
 
Justin

The following assumes you are using RSLogix. If you aren't, disregard.

The help files in RSLogix will get you the needed info but here is a nutshell version. When necessary logic is true, end your rung with a JSR instruction. This will cause the subroutine to execute. The first instruction of the first rung of the subroutine should be an SBR. At the end of the last rung of the subroutine, an RET instruction will end the subroutine.

Read the help files inside the RSLogix software for a lot of useful information. Especially as someone new to PLCs, these files could save you some time and aggravation.

Welcome to the dance.
 
This could get messy

Following on to previous posts, the JSR, or whatever it's called in your case, is an output instruction. Like any other output, it is 'energized' by preceding rung conditions being true. As long as true input conditions prevail, the subroutine will be called every scan. When it is not called, the logic in the the subroutine becomes inert, it's not executed.

The implication for you is that if the JSR rung is not sealed in somehow, the timer will freeze. Think about it - the processor cannot update any instruction unless it executes that instruction.

Now, the most likely way to seal in the JSR for the duration of the timer is to use a branch (around the momentary condition which starts the timer) in the JSR rung to monitor the state of one or more of the timer bits and kill the subroutine calls when the timer is done. Messy. Besides, in my mind anyway, it's against the concept of a subroutine

You'll be a lot better off to put the timer in the main line code, condition it accordingly, and end up with cleaner logic.

.02
 
maybe a picture will help ...

Justin,

it looks like you’ve already been given all of the information you need ... but since I already had most of this typed up anyway, I’ll post it for what it’s worth ...

first of all, the scan is NOT going to STAY in the subroutine until the timer is done and THEN go back to the main program ... basically it’s going to have to go back to the main program on each and every scan ... if it doesn’t, then the processor will fault ...

anyway ...

here’s the best way to learn about subroutines ... start out by programming this in your main program (ladder file number 2) ... when the software asks you for “input parameter” and “return parameter” just press the Enter key ... you don’t need these entries to start learning ... in fact, most programmers NEVER use them ...

[attachment]

next set up three new subroutines by creating three new ladder files ... for example: ladder 3, ladder 4, and ladder 5 ... (hint if you need it: right click on the Program Files icon and then select New) ... in each one of the new subroutines put another rung just like the first rung shown in the picture ... EXCEPT make sure that you use a DIFFERENT address for each timer ... for example: use T4:3 and T4:3/DN in file number 3 ... use T4:4 and T4:4/DN in file number 4 ... use T4:5 and T4:5/DN in file number 5 ... NOTE: in real life your timer number does NOT have to correspond with your subroutine number ... but in this experiment, it just helps to keep things straight ...

these little timers will act like “scan testers” ... the numbers in their accumulators will constantly be changing while their files (subroutines) are active (being scanned) ... the numbers in their accumulators will freeze when their subroutines are inactive (NOT being scanned) ...

now put the processor in run mode ... next toggle the test bits on and off ... this will selectively activate and deactivate your subroutine files ... watch which timers run ... and which timers freeze ...

this little experiment should give you a good idea of just how the subroutines behave ... trust me, you’ll learn more from this than from reading and rereading the book ... been there ... done that ...

something specific to watch for: the “power rails” at the left and right side of the subroutine ladder files will turn GREEN while the processor is in run mode ... even when the subroutine is NOT being scanned ... some beginners think that the green means that the subroutine is currently “active” ... it doesn’t ...

want to learn more along the same lines? ... research and experiment with these topics ... MCP (Main Control Program) and STI (Selectable Timed Interrupt) ...

finally, did you do a search on this forum for “subroutine” and “JSR” threads? ... this subject comes up quite often ...

good luck ...

PS ... here I'm assuming (gosh I hate that word) that you're using RSLogix5 ... from now on, how about telling us what system you're working with ... that will help us all out ...

subroutines.jpg
 
Last edited:
Re: maybe a picture will help ...

Ron Beaufort said:

first of all, the scan is NOT going to STAY in the subroutine until the timer is done and THEN go back to the main program ... basically it’s going to have to go back to the main program on each and every scan ... if it doesn’t, then the processor will fault ...

I KNEW I forgot something!
 

Similar Topics

I have been asked to extensively modify a piece of machinery that is very mission critical to the operation, thus not allowed much downtime or...
Replies
4
Views
2,264
We have an application where we need to indicate a piece of machinery needs maintenance. I have created a routine which resets a value of "days...
Replies
12
Views
3,690
Hi all. Please see attached. Tried to google the error with absolutely no results. I did the same at payed version and real PLC so I'm almost...
Replies
7
Views
2,325
hello all is there a rule of thumb as to using or not using subroutines when writing a program ? i am using an Allen Bradley slc 500 thank you...
Replies
5
Views
1,709
All, Using RSLogix 5000. I was wondering if there is a way to quickly check to see if all subroutines are being called by the Main Routine (in...
Replies
3
Views
1,581
Back
Top Bottom