Today, I'm sharing a practical article to help you better understand what a perfect PLC program looks like, as well as PLC programming standards and suggestions for practical work.
The design requirements that a perfect PLC program must meet
A complete PLC program is not simply about making the system run; it also requires complete comments, a sophisticated architecture, good scalability, a comprehensive alarm and protection system, and a simulation system before operation.
1. Simplicity
Keep PLC programs as simple as possible. Simplicity means using standardized program frameworks and simple instructions whenever possible.
To simplify a program, broadly speaking, we need to optimize the program structure and use flow control instructions to simplify the program. On a smaller scale, we also need to replace single-function instructions with more powerful ones, and pay attention to the order in which the instructions are arranged.
2. Readability
The designed program should be highly readable. This not only helps the programmer better understand the program and facilitates debugging, but also makes it easy for others to understand and for users to maintain. It should also facilitate program dissemination when necessary.
To ensure good readability, the program design should be as clear as possible. Pay attention to hierarchy, implement modularity, and even employ object-oriented design methods. Utilize more standard design principles.
In special circumstances where programming languages are used, ladder diagrams should be used in most cases for ease of reading.
Furthermore, I/O allocation should be systematic to facilitate memorization and understanding. Comments should be added when necessary. The use of internal components should also follow a systematic approach; they shouldn't be used haphazardly.
Readability should be considered from the very beginning of program design. This is not easy to achieve completely. During program debugging, the addition or removal of instructions, and changes in the use of internal components, can make an originally clear program somewhat messy. Therefore, it is essential to leave some leeway for debugging additions and removals during the design phase, and then tidy it up after debugging is complete. This will result in a higher quality program.
Program comments should at least include the following aspects:
A. System Comments: Copyright holder of the entire program and its purpose.
B. Block Comments: This section includes the main purpose and author of the block.
C. Section Comment: The purpose of this code segment
D. Variable comments: Their importance is self-evident; they include I/O comments and intermediate variable comments.
As for confidentiality considerations, I think they should be addressed through the encryption algorithm of the program or the encryption of blocks, rather than through clever tricks like reducing comments.
3. Correctness
The PLC program must be correct and verified through actual operation to prove its correct function. This is the most fundamental requirement for a PLC program; if this is not achieved, no matter how good the other programs are, they are useless.
For a program to function correctly, instructions and internal components must be used accurately. Accurate instruction usage is linked to accurate instruction understanding; therefore, the meaning and conditions of use of each instruction must be thoroughly understood. If necessary, small programs can be written to test any unclear instructions.
For the same instruction, some details may differ due to different PLC batches or series models. You should carefully consult the programming manual.
The correct use of internal components is also important. For example, some PLCs have power-down protection, while others do not. It is essential to ensure that components with power-down protection are used where required, and vice versa.
In short, the most fundamental requirement for PLC programs is to use instructions accurately and internal devices correctly so that the programmed program can execute correctly.
For a simple example, Siemens requires variables with storage functionality as intermediate variables for rising and falling edges, such as M-points or DB-points. Using FC's temp variable would cause problems.
4. Reliability
The program must not only be correct, but also reliable. Reliability reflects the stability of the PLC program, which is a basic requirement for PLC programs.
Some PLC programs work correctly under normal operating conditions or during legitimate operations, but fail to function properly under abnormal operating conditions (such as a temporary power outage followed by rapid power restoration) or during illegal operations (such as pressing buttons out of sequence or pressing several buttons simultaneously). Such programs are unreliable, unstable, or simply bad programs.
A good PLC program can identify abnormal operating conditions and seamlessly integrate them with normal conditions, making the program adaptable to various situations. A good PLC program can reject illegal operations without leaving any trace, accepting only legal operations.
Interlocking is a common method to prevent illegal operations. It is frequently used in relay circuits and can also be adopted by PLCs.
5. Ease of modification
The program should be easy to modify, meaning it should be easy to modify.
One of the characteristics of PLCs is their convenience and flexibility in adapting to various situations. This is achieved by modifying or redesigning the program.
When redesigning a PLC program to adapt to changes in its application requirements, not only must the program be rewritten, but I/O allocation must also be done. In most cases, rewriting the program is unnecessary; minor modifications are sufficient. This necessitates a program that is easy to modify.
Easy to modify, or flexible, means that only minor changes are needed to achieve the purpose of changing parameters or modifying actions.
6. Scalability
Many programs may have been programmed before being deployed to the site, but additional programs may need to be added once the site is reached. In order to avoid disrupting the structure of the entire system, a certain amount of space needs to be reserved in each functional area as a backup.
Leave enough margin in the hardware, and when writing the software, consider manual, automatic, and semi-automatic modes and reserve space for them.
7. Complete alarm system
PLC systems are often used in industrial environments, where every accident can cause losses, large or small. In order to pre-handle accidents or minimize losses during an accident, it is essential to pay attention to the alarm and protection functions of the PLC. Therefore, we have extracted this as an important component of the system.
8. Program Simulation
To ensure on-site debugging progress or for customer demonstrations, it's often necessary to perform a fully automated simulation of the program before going to the site. This requires adding a simulation component to the program, which disconnects after normal on-site operation. To enable simulation functionality in the program, the following steps are required:
(1) Convert the actual PLC I/O points into PLC intermediate variables or data block variables;
(2) Write simulation programs for each piece of equipment according to the process requirements.
A good PLC program is one that meets the above requirements.
PLC Programming Specifications
1. Select the appropriate PLC model and number of I/O points. If there are special functional requirements, select special function modules.
2. Be familiar with the selected PLC programming instructions and compilation software.
3. Perform software component planning, including internal relays, holding relays, data registers, timers, counters, etc.
4. Program planning is generally carried out in the following order: fault extraction, fault handling, manual handling, automatic handling, and output processing. Larger projects or equipment are divided into functional units and blocks for processing. For example, an automated production line may contain elevators, transfer devices, lifting and rotating devices, etc., and should be programmed in the above-mentioned unit segments and blocks.
5. A brief segment comment should be added before each segmented or block-based program to explain its function. If necessary, the corresponding process flow can be noted. The order of the segmented or block-based programs in the overall program should basically follow the process flow sequence to facilitate program readability.
6. Before designing the program, the equipment should be abstracted, and common factors such as stop, emergency stop, overload, over-limit, timeout, safety light curtain, collision stop, and door switch should be extracted and placed in the start circuit or start main control and interlock circuit as the premise of the entire program structure. On this basis, the program is then divided into two major functional areas: automatic and manual.
7. Extract common factors in the manual function area of the program structure, such as manual control and factors endangering equipment and personal safety, and place them in the manual main control and interlocking loop to protect, shield, and alarm the manual control.
8. Extract common factors from the automatic function areas of the program structure, such as automatic, over-limit, and timeout factors, and place them in the automatic main control and interlocking loops to protect, shield, and alarm equipment under automatic control. A general principle is to strictly restrict equipment entry while loosely restricting equipment exit, ensuring safety.
9. The program should include a master reset function to facilitate quick and easy restoration of normal operation in case of equipment failure. The master reset function should fully consider the safety of both equipment and personnel during the reset process.
10. When switching from automatic mode to manual mode, the program should clear the output and intermediate states in automatic mode. In particular, when using the SET instruction in automatic mode, it must be cleared using the RESET instruction in manual mode.
11. Dual outputs are strictly prohibited in programming, meaning the same output statement or the same output coil must appear two or more times in the program. For outputs to the same point under different mode conditions, use intermediate relays for transfer, and finally combine them and list them to the output point.
12. When using a touch screen, the control area and status area shared by the touch screen and PLC shall not be used for programming other functions.
13. Before using special modules of a PLC, it should be determined whether their control area and status area occupy working words. If they do, these working words should not be programmed in any other way.
14. PLC inputs, outputs, intermediate relays, timers, counters, data registers, etc., must all have Chinese annotations. Inputs and outputs must also include the component name and tag number. Corresponding input points are generally assumed to be NO contacts connected to external switches; for those requiring NC contacts, this must be specified in the annotation. All annotations should be clear and unambiguous, avoiding misunderstandings, and minimizing the use of generic terms.
15. After the project is debugged, the final software program must be retained. The file name should include the project number, author, date information, version number, etc.
16. Regarding program encryption: The password for the encrypted program must be stored in a dedicated file, clearly indicating the corresponding username, password, and permissions. The file must be distributed to at least two people to learn the password, in order to prevent the program from becoming inaccessible due to password loss.
Programming suggestions
1. When a PLC and a host computer (or touchscreen) form a monitoring system , the screen often needs to display control modes such as "manual" and "automatic" (usually multiple modes can only be used at a time). The "MOV" instruction can be used in the program. For example , when "manual" is selected, the constant 1 is MOVed into register VB10 ; when "automatic" is selected, 2 is MOVed into the same register VB10. By checking the data in the register, the system's control mode can be determined. The advantage of this approach is its ease of understanding and avoids the need for complex interlocking procedures.
2. When the program involves analog signal control, if the read analog signal has virtually no error, time filtering can be used to delay the data for a certain period. If the read data has a large error, other filtering methods are needed, such as calculating the average value. Please consult relevant materials for more information.
3. During program debugging (especially when modifying equipment and adding your program to the existing equipment's program), if a condition is met but the output coil is not connected, check if this part of your program is within such statements , such as JUMP goto. Another possibility is that after the program is interrupted, the condition is met but there is no output, usually because this part of the program is not scanned.
4. In sequential control programs, where one action is completed before moving to the next, or similar sequential control, the +10+10 control mode is used, which I find very convenient. The idea is : a register is preset to 0 during initialization . After system startup, it is incremented by 10, bringing the register to 10. With the register at 10, the first action can be performed. After the first action, the register is incremented by 10 again, bringing the register to 20, allowing the second action to begin. After the second action, it is incremented again, bringing the register to 30. This way, by checking the value in the register, the desired action can be determined. When a jump action is needed, the increment can be changed to +20, +30 , etc. , depending on the specific requirements.
Why add 10 instead of 1? Because after adding 10, if you want to insert a segment, you can simply choose any one of these 10 empty spaces.
5. When designing a program, if a process failure (not controlled by the control system) occurs, it's best to preserve the fault symptoms and provide visual and audible alarms. This should continue until the operator resets the system, informing them of the malfunction. Otherwise, if the machine stops, others might assume it's a program error. These points are generally important to consider when designing a new system.
6. For frequently called subroutines, they can be made into submodules for frequent calls.
7. Since each step in the work cycle of a production machine requires a certain amount of time to execute, and these times have certain limits, a timer can be started simultaneously with the start of the step to be monitored. The timer's time setting is 20% to 30% longer than the normal duration of the action. The timer's output signal can be used for alarms or automatic shutdown devices. When the time of a certain step exceeds the specified time, reaching the corresponding timer preset time, and before the next step has begun, the timer issues a fault signal. This signal stops the normal working cycle and initiates the alarm or shutdown procedure. This is what we commonly refer to as over-cycle protection.
8. Some safety detection switches (such as emergency stop buttons, safety light curtains, limit switches, etc.) use normally closed (NC) inputs.
9. For safety and energy saving considerations, the output should be designed to only operate when needed and stop once the desired action is achieved, rather than being designed to output continuously and only disconnect when necessary.
10. The operating principle of actuators should be: it is better to keep them still than to move them haphazardly!
11. Single device control: Each device must have the function of switching between manual and automatic operation and the ability to start/stop when operating manually. When switching from automatic to manual operation, the device cannot stop. When switching from manual operation to automatic, the start/stop of the device depends on the automatic program.
12. Each piece of equipment (pump, fan and other large equipment) must be rotated after 24 hours of operation, and the operating time must be accumulated, unless the start/stop sequence is set by the host computer, in which case the operator shall set it himself.