A-B Function Routines
A-B Function Routines
(OP)
Many people don't realize that it is possible to write a function in the PLC5 and SLC500 processors that you can pass values to and receive an answer back. This is very useful when you have a great deal of code to place in multiple places. Example of adding 100 to a number below:
1) Create a new file for you function routine
2) The first instruction will be an SBR N7: An integer address for holding the number you want to add (N7:10 example) but make it unique or create a new data file with local scope. Then place an output instruction, OTE B/100 for example.
3) Next rung start do an ADD 100 N7:10 N7:11 to add 100 to your number.
4) The final rung is RET N 7:11 N7:11 is the result of the ADD instruction and this will be sent back to the calling JSR below.
5) In your LAD 2 File, add a JSR with your function program file number and add an input parameter of N7:0 at the end of the logic and keep hitting the enter in the graphic until you see Return Parameter and enter N7:1.
6) Put a value in N7:0 and run the program, N7:1 will always show as 100 more than N7:0.
1) Create a new file for you function routine
2) The first instruction will be an SBR N7: An integer address for holding the number you want to add (N7:10 example) but make it unique or create a new data file with local scope. Then place an output instruction, OTE B/100 for example.
3) Next rung start do an ADD 100 N7:10 N7:11 to add 100 to your number.
4) The final rung is RET N 7:11 N7:11 is the result of the ADD instruction and this will be sent back to the calling JSR below.
5) In your LAD 2 File, add a JSR with your function program file number and add an input parameter of N7:0 at the end of the logic and keep hitting the enter in the graphic until you see Return Parameter and enter N7:1.
6) Put a value in N7:0 and run the program, N7:1 will always show as 100 more than N7:0.





RE: A-B Function Routines
Another example, which I have written for the 90-30 GE/Fanuc plc, is a subroutine I named "UP_SCL", which takes a value from 0 - 100 (for instance a motor speed, stopped to wide open) and turns it into an equivalent 0 - 32,000 integer, which the GE plc uses for it's Analog Output registers. I do a few MOV_INT instructions to put my input values into the correct registers, then "CALL" the subroutine, and then immediately MOV_INT the output into the desired register.
The subroutine is a 14-rung program that takes a little thought to write, but is easy to call again and again. I put my "zero checking" in there as well - any values below zero I convert to zero. This has saved me a lot of time.
RE: A-B Function Routines
RE: A-B Function Routines
http://www.ktech-usa.com
RE: A-B Function Routines
RE: A-B Function Routines
Thanks
http://www.ktech-usa.com
RE: A-B Function Routines
RE: A-B Function Routines
I will agree that the A-B is easier to use and I believe the brand PLC is a redundant argument for 90% or more of the applications. I have not tried to change anyone's mind about a processor brand, though I have helped OEM clients convince their customer due to the OEM's preference (and every time so far it has been to A-B). No worry to me about any pissing match since it isn't all that important to me what brand I use for most applications.
http://www.ktech-usa.com
RE: A-B Function Routines
RE: A-B Function Routines
RE: A-B Function Routines
http://www.ktech-usa.com
RE: A-B Function Routines
I agree with techshoot that subroutine functions are easier in PLC, and ControlLogix...in fact in CLX I use create a User Defined Data type for ALL my subroutinesand simply pass one variable with all the routine data.
BUT the same technique also works for the SLC and Micro1500...just a little more work. Write the device or logical subroutine and point ALL the data in it to one N file dedicated to it. Typically I will use the first word or two as flags, and the rest as true integer values. If Reals are needed define a separate F file.
Give everything in this file a symbol such as _Run (N10:0/0), or _TimeMax (N10:1). Then using the database export tool get it into Excel.
Using Excel cut and paste the variables you have just made and create new N files (say N11,N12), but now dedicated to the device or logical objects you are working with. Then use Search and Replace to create for example, Pump1, Pump2 would become:
P1_Run(N11:0/0), P1_MaxTime (N11:1), or P2_Run (N12:0/0), P2_MaxTime (N12:1).
In this way you can quickly create lots of very well organised datatable, complete with full symbols and comments if you wish.
And finally, in the ladder simply use the COP instruction to copy the file say beginning with P1_Run onto _Run. (COP N11:0 N10:0 LEN 2)
Call the SBR and then immediately after do another COP of _Run back onto P1_Run. (COP N10:0 N11:0 LEN 2) and Voila!@! the subroutine has passed its data back to the device data table.
If any of you are interested I am happy to email an SLC .RSS example project file that uses this technique.
Then