PLC programming tips, benefits for engineers

Get help for programmable logic controller (PLC) programming from Control Engineering editorial advisory board member Frank Lamb, covering ladder logic and scanning, binary-coded decimals, and reusable codes.

07/17/2017


Ladder logic and programmable logic controller (PLC) scanning, binary coded decimals, and reusable codes are among PLC programming tips offered by Control Engineering editorial advisory board member Frank Lamb, the founder of Automation Consulting Services Inc., a Control Engineering content partner. Featured below are tips on PLC programming from three different viewpoints. 

Ladder logic, PLC scanning

Almost all programmable logic controllers (PLCs) worldwide handle scanning in the same way. First, the CPU reads the physical inputs into a memory table, usually called the "input table." This table is then used as the program is evaluated. There are different types of registers that are used in different platforms; these registers are updated as the logic is processed left to right on each rung and top to bottom within each routine. This includes updating an output table, which will later be used to drive the physical devices connected to the PLC.

The program might call different subroutines for different purposes, and it can be important in what order routines are called. Depending on where memory registers and output tables are updated, the physical outputs could be delayed by up to two scans.

In either case, the program meanders through the different routines as they are called before returning to wherever they were called from and eventually ends up at the end of the original cyclic routine. Most programs use an initial cyclic routine that is used to call all of the other routines. Some programs, however, run on a periodic basis instead of a continuous program. This is uncommon, though. Most programs use a continuous program configuration that runs as fast as it can. After executing all of the code, evaluating the logic, and updating all of the tables (except for the input tables, which were written at the beginning of the scan), the resulting output table or register contents are written to the physical outputs.

How long does this take? That depends on the platform (speed of the processor), how much code there is in the program, and the types of instructions used. Sometimes programmers will use loops in the program or make repetitive calls to the same routines. All of this has an effect on the total scan time. There is usually documentation available indicating the execution time for different instructions, but there's no point in trying to add up all of the code to make an estimate on length. It simply is available for reference.

The scan time can take as long as 80 ms. If the scan time is longer than about 50 ms (for a machine control project) then the user should be looking for a more powerful processor or ways to make the code more efficient. Beyond 50 ms, the effect on output reaction for a machine control project starts to be noticeable; for a process control project this may not matter. 

Binary-coded decimal (BCD) is a class of binary encodings of decimal numbers where each decimal is represented by a fixed number of bits, usually four or eight, which goes against the way humans compute data. This disconnect can cause problems for programBinary-coded decimals and PLCs

Binary-coded decimals (BCDs) are a class of binary encodings, which are usually represented by four or eight bits. It's only humans who need to add up all those individual bits in our registers to convert them to base 10 because that's the way we're programmed to think.

Back in the days before touchscreens, seven-segment displays and thumbwheel switches were used as a numerical interface between humans and PLCs. Even before the PLC, these devices were the only graphical way to interface with circuits numerically. Users could move plugs around like in the days of ENIAC (the first electronic general purpose computer), but it was a lot easier to view and adjust decimal numbers with these devices. The problem was, they were very input/output (I/O) intensive. Each thumbwheel segment required four inputs (+ power), while each 7-segment display required four outputs (+2 power connections). Still, it was a lot easier to interface directly with signed or unsigned integers in decimals than use pushbuttons and pilot lights.

A common complaint is the math; every data type has to be explicitly declared on the platform, and converted if data types are not equivalent. Not only that, but the standard timer and counter data types incorporate BCD into their data structures. This is because the structures go all the way back to when people had to deal with things like these thumbwheels and seven-segment displays. In fact, the timer setpoints are still entered as "S5T#3S" for a 3-second setpoint. The timer uses three BCD digits (12-bits) and two extra bits for the time base. This is also true for the counters, meaning that they only count from -999 to +999.

Each four-digit section can only carry bit values from 0000 to 1001; for the next value, rather than indexing to 1010, ("10″ in signed or unsigned decimal or "A" in hexadecimal), the next bit gets bumped to the next section of bits. This means that the last six combinations of bits (A-F) are effectively wasted-not possible in the BCD structure.

The BCD structure or base also is still usable in many of the newer touchscreens, but most programmers tend to choose an integer base to express decimal numbers. BCD is sort of like DOS; engineering schools still touch on it, but people really don't know where it comes from. Hopefully referring back to those old thumbwheels and seven-segment displays will help clear up some of the mystery and "why" of BCD. 

Reusable code for programmable logic controllers (PLCs) provide users with flexibility and allow structures to be built that can be exported from one application to another. Courtesy: Frank Lamb, Automation PrimerAdvantages of reusable code for PLCs

Many programmers base their knowledge and abilities on their experience with Allen-Bradley products from Rockwell Automation since much of the installed base in manufacturing consists of A-B PLCs. Rockwell Automation's ControlLogix family was a huge jump in PLC capabilities with enhancements such as being tag-based, allowing the use of user-defined data types (UDTs), and add-on instructions (AOIs). Tags could also be made local to each program, allowing them to be duplicated for re-use.

All of those capabilities, except for being tag-based, already existed years before that on other platforms. IEC 61131 for programming languages has existed since 1993 or so, and other PLCs leveraged that early on. One of the most important differences between the older, register-only based systems and the more modern ones is the ability to build re-usable code blocks. Platforms that support this must have these three features:

1. Local vs. global variables. Re-usable code must have variables that apply to each instance of the code; ideally only formatting the data once for the original code. What this means is that a list of tags or symbols should not have to be re-named for each instance or call. For instance, duplicating a subroutine several times and iterating the addresses inside of it, though it saves time, isn't really re-usable code. 

2. User-defined data types (UDTs). Creating UDTs allows structures to be built that can be exported from one application to another. They allow components to be described using generic terms such as "Speed," "Start," and "Reject." UDTs don't require tag-based systems, but they do require an advanced use of symbols. 

3. Protectable self-contained blocks. It is important that the code be contained in a block that allows variables to be passed in and out, and also protected so that users can't change a specific instance of it. This requires a password or software key.

These are just some of the requirements. Other features like being able to write code in other IEC-compliant PLC languages such as structured text also help make platforms much more powerful and "rapid code development" friendly.

Regardless of whether the platform uses subroutines with local variables or customizable instructions, reusable code is a critical part of creating powerful programs quickly. Many platforms allow reusable code, and each has its own unique methods.

Frank Lamb is the founder of Automation Consulting Services Inc. This article originally appeared on the Automation Primer blog. Automation Primer is a CFE Media content partner. Edited by Chris Vavra, production editor, Control Engineering, CFE Media, cvavra@cfemedia.com.

MORE ADVICE

Key Concepts

There are different types of registers for PLC scanning that are updated to an output table, which will be used to drive physical devices connected to the PLC.

Binary-coded decimals (BCDs) are a class of binary encodings represented by four or eight bits.

Many platforms allow reusable code to create powerful programs quickly, but each has its own method.

Consider this

What other programming methods can be used for PLCs and what benefits do they provide for engineers?



Engineers' Choice Awards
The Engineers' Choice Awards highlight some of the best new control, instrumentation and automation products as chosen by Control Engineering subscribers.
System Integrator Giants
The System Integrator Giants program lists the top 100 system integrators among companies listed in CFE Media's Global System Integrator Database.
System Integrator of the Year
Each year, a panel of Control Engineering and Plant Engineering editors and industry expert judges select the System Integrator of the Year Award winners in three categories.
How to Maximize Factory Automation Efficiency with Low Cost Machine Vision
This eGuide illustrates solutions, applications and benefits of machine vision systems.
Wireless Reliability in Harsh Environments
Learn how to increase device reliability in harsh environments and decrease unplanned system downtime.
Human Factors and the Impact on Plant Safety
This eGuide contains a series of articles and videos that considers theoretical and practical; immediate needs and a look into the future.
August 2018
Augmented reality and virtual reality education, autotuning PID control, cybersecurity advice, educating engineers
July 2018
Ladder logic best practices and object-oriented programming, safety instrumented systems, enclosure design issues and challenges, process control advice
June 2018
Discrete and process sensor fundamentals, autotuning controls, system integrator roundtable
Edge Computing
This article collection contains several articles on how today's technologies heap benefits onto an edge-computing architecture such as faster computing, better networking, more memory, smarter analytics, cloud-based intelligence, and lower costs.
Data Center Design
Data centers, data closets, edge and cloud computing, co-location facilities, and similar topics are among the fastest-changing in the industry.
PLCs
Programmable logic controllers (PLCs) represent the logic (decision) part of the control loop of sense, decide, and actuate. Featured articles in this digital report compare PLCs and programmable automation controllers (PACs), industrial PCs, and robotic controllers.
SIDB

Find and connect with the most suitable service provider for your unique application. Start searching the Global System Integrator Database Now!

August 2018
SCADA standardization, capital expenditures, data-driven drilling and execution
June 2018
Machine learning, produced water benefits, progressive cavity pumps
April 2018
ROVs, rigs, and the real time; wellsite valve manifolds; AI on a chip; analytics use for pipelines
John O. Ayuk, PE, CFSE, PMP, CAP
Automation Engineer; Wood Group
Doug Baker
System Integrator; Cross Integrated Systems Group
Jose S. Vasquez, Jr.
Jose S. Vasquez, Jr.
Fire & Life Safety Engineer; Technip USA Inc.
Data Centers: Impacts of Climate and Cooling Technology
This course focuses on climate analysis, appropriateness of cooling system selection, and combining cooling systems.
Safety First: Arc Flash 101
This course will help identify and reveal electrical hazards and identify the solutions to implementing and maintaining a safe work environment.
Critical Power: Hospital Electrical Systems
This course explains how maintaining power and communication systems through emergency power-generation systems is critical.
Engineers' Choice Awards
The Engineers' Choice Awards highlight some of the best new control, instrumentation and automation products as chosen by Control Engineering subscribers.
System Integrator Giants
The System Integrator Giants program lists the top 100 system integrators among companies listed in CFE Media's Global System Integrator Database.
System Integrator of the Year
Each year, a panel of Control Engineering and Plant Engineering editors and industry expert judges select the System Integrator of the Year Award winners in three categories.
How to Maximize Factory Automation Efficiency with Low Cost Machine Vision
This eGuide illustrates solutions, applications and benefits of machine vision systems.
Wireless Reliability in Harsh Environments
Learn how to increase device reliability in harsh environments and decrease unplanned system downtime.
Human Factors and the Impact on Plant Safety
This eGuide contains a series of articles and videos that considers theoretical and practical; immediate needs and a look into the future.
August 2018
Augmented reality and virtual reality education, autotuning PID control, cybersecurity advice, educating engineers
July 2018
Ladder logic best practices and object-oriented programming, safety instrumented systems, enclosure design issues and challenges, process control advice
June 2018
Discrete and process sensor fundamentals, autotuning controls, system integrator roundtable
Edge Computing
This article collection contains several articles on how today's technologies heap benefits onto an edge-computing architecture such as faster computing, better networking, more memory, smarter analytics, cloud-based intelligence, and lower costs.
Data Center Design
Data centers, data closets, edge and cloud computing, co-location facilities, and similar topics are among the fastest-changing in the industry.
PLCs
Programmable logic controllers (PLCs) represent the logic (decision) part of the control loop of sense, decide, and actuate. Featured articles in this digital report compare PLCs and programmable automation controllers (PACs), industrial PCs, and robotic controllers.
SIDB

Find and connect with the most suitable service provider for your unique application. Start searching the Global System Integrator Database Now!

August 2018
SCADA standardization, capital expenditures, data-driven drilling and execution
June 2018
Machine learning, produced water benefits, progressive cavity pumps
April 2018
ROVs, rigs, and the real time; wellsite valve manifolds; AI on a chip; analytics use for pipelines
John O. Ayuk, PE, CFSE, PMP, CAP
Automation Engineer; Wood Group
Doug Baker
System Integrator; Cross Integrated Systems Group
Jose S. Vasquez, Jr.
Jose S. Vasquez, Jr.
Fire & Life Safety Engineer; Technip USA Inc.
Data Centers: Impacts of Climate and Cooling Technology
This course focuses on climate analysis, appropriateness of cooling system selection, and combining cooling systems.
Safety First: Arc Flash 101
This course will help identify and reveal electrical hazards and identify the solutions to implementing and maintaining a safe work environment.
Critical Power: Hospital Electrical Systems
This course explains how maintaining power and communication systems through emergency power-generation systems is critical.
Engineers' Choice Awards
The Engineers' Choice Awards highlight some of the best new control, instrumentation and automation products as chosen by Control Engineering subscribers.
System Integrator Giants
The System Integrator Giants program lists the top 100 system integrators among companies listed in CFE Media's Global System Integrator Database.
System Integrator of the Year
Each year, a panel of Control Engineering and Plant Engineering editors and industry expert judges select the System Integrator of the Year Award winners in three categories.
How to Maximize Factory Automation Efficiency with Low Cost Machine Vision
This eGuide illustrates solutions, applications and benefits of machine vision systems.
Wireless Reliability in Harsh Environments
Learn how to increase device reliability in harsh environments and decrease unplanned system downtime.
Human Factors and the Impact on Plant Safety
This eGuide contains a series of articles and videos that considers theoretical and practical; immediate needs and a look into the future.
August 2018
Augmented reality and virtual reality education, autotuning PID control, cybersecurity advice, educating engineers
July 2018
Ladder logic best practices and object-oriented programming, safety instrumented systems, enclosure design issues and challenges, process control advice
June 2018
Discrete and process sensor fundamentals, autotuning controls, system integrator roundtable
Edge Computing
This article collection contains several articles on how today's technologies heap benefits onto an edge-computing architecture such as faster computing, better networking, more memory, smarter analytics, cloud-based intelligence, and lower costs.
Data Center Design
Data centers, data closets, edge and cloud computing, co-location facilities, and similar topics are among the fastest-changing in the industry.
PLCs
Programmable logic controllers (PLCs) represent the logic (decision) part of the control loop of sense, decide, and actuate. Featured articles in this digital report compare PLCs and programmable automation controllers (PACs), industrial PCs, and robotic controllers.
SIDB

Find and connect with the most suitable service provider for your unique application. Start searching the Global System Integrator Database Now!

August 2018
SCADA standardization, capital expenditures, data-driven drilling and execution
June 2018
Machine learning, produced water benefits, progressive cavity pumps
April 2018
ROVs, rigs, and the real time; wellsite valve manifolds; AI on a chip; analytics use for pipelines
John O. Ayuk, PE, CFSE, PMP, CAP
Automation Engineer; Wood Group
Doug Baker
System Integrator; Cross Integrated Systems Group
Jose S. Vasquez, Jr.
Jose S. Vasquez, Jr.
Fire & Life Safety Engineer; Technip USA Inc.
Data Centers: Impacts of Climate and Cooling Technology
This course focuses on climate analysis, appropriateness of cooling system selection, and combining cooling systems.
Safety First: Arc Flash 101
This course will help identify and reveal electrical hazards and identify the solutions to implementing and maintaining a safe work environment.
Critical Power: Hospital Electrical Systems
This course explains how maintaining power and communication systems through emergency power-generation systems is critical.
click me