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.

When I first started programming twenty years ago, I would have used the first style that I will discuss here. I will call this style hard coding.

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.

Hard coding:

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.

Image by Brandon Cooper

The tags shown in the database are all individual tags.

Image by Brandon Cooper

Calling the routine for the hard-coded logic is a standard JSR instruction with no parameters.

Image by Brandon Cooper.png

Subroutines:

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”

Image by Brandon Cooper

In this screenshot, when I create the subroutine, I use a tag with the same UDT data type throughout the routine.

Image by Brandon Cooper

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.

Image by Brandon Cooper

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.

Image by Brandon Cooper

 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.

Image by Brandon Cooper

Next, the logic is written inside the Add-on instruction as shown here.

Image by Brandon Cooper

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.

Image by Brandon Cooper

Conclusion

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

Have a question? Join our community of pros to take part in the discussion! You'll also find all of our automation courses at TheAutomationSchool.com.

Sponsor and Advertise: Get your product or service in front of our 75K followers while also supporting independent automation journalism by sponsoring or advertising with us! Learn more in our Media Guide here, or contact us using this form.

Brandon Cooper
 

LEAVE A REPLY

Please enter your comment!
Please enter your name here