Programmers can accomplish a task in many ways. They have many options available, and it is almost to the point that you can use whatever coding style your preference is for a task at hand.
PLC code does not need to be fancy, but it does help to be efficient, especially if you are embarking on a large project.
In this article, I want to show some differences in programming styles.
After a few years, I would learn to program with sub-routines to accomplish the same things, with less effort.
Today, with the creation of Add-on instructions, you can go a step further and create your own instructions that can be custom defined for your individual needs.
What I will do in this article is show the differences between these three ways of coding the exact same scenario. I will show programming an analog input variable with a few alarms for low, low-low, high and high-high.
In this example, each of the tags in this ladder are created individually. They are “REAL” data types or “BOOL” data types. The loop number is used in each tag and they can be created with the same scheme, but they are not bound together in any way.
This routine would be called from the main routine to run the code. If you wish to re-use this code, you must create another routine with all the same rungs and change the tags in each rung to match the new loop number.
The tags shown in the database are all individual tags.
Calling the routine for the hard-coded logic is a standard JSR instruction with no parameters.
The second way I learned to program was to be able to re-use the same code over and over by creating a user-defined data type with all needed parameters. This data type can be used throughout the subroutine. Loop numbers can be passed to the subroutine and then returned with the result.
In the below screenshot, I have created a user-defined data type called “AI_SUB”
In this screenshot, when I create the subroutine, I use a tag with the same UDT data type throughout the routine.
The beauty of the tag is that the parameters are bound together as part of the tag itself. You only have one tag with all the needed parameters.
Calling the subroutine is a little different than calling the hard-coded routine. The tag that is used in place of the template tag must be passed and returned from the subroutine.
And here is a tip for you, this must be correct or you will fault your processor. Ask me how I know.
Add-on instructions (AOIs):
ControlLogix programming now offers the option to create your own instructions, custom fit for your needs. These instructions allow you to take what would have been done in a subroutine and place it in a single instance or instruction. The instruction contains parameters, local tags and logic. Here I created an Add-on instruction called “Analog_Input” using the same parameters we have used in the previous coding examples.
Next, the logic is written inside the Add-on instruction as shown here.
The AOI is now listed in the instruction pallet in Studio 5000 software and can be inserted into a rung just as any other instruction would be.
For a quick recap, there is absolutely nothing wrong with any of these options for writing a program, but take into consideration the scope of a project, writing code once and reusing it dozens or even hundreds of times can save large amounts of time and money.
Think outside the box too. What I have shown you here is a very small example of a few lines of code. Complex instructions can be created for countless uses only usually limited by our imaginations.
Written by Brandon Cooper
Senior Controls Engineer and Freelance Writer
- PanelView 800 – Datalogging and Trending - August 10, 2021
- PanelView 800 – Using the Recipe Feature - August 3, 2021
- SIMATIC HMI Basic Panel – Using the Recipe Feature - July 27, 2021