Step7 program execution principle and programming method - Database & Sql Blog Articles

Programmable Package SG-8018CA(SG7050C) 0.67M~170M
Photocoupler

How does a PLC execute its loop program? This is a crucial topic for understanding the inner workings of a programmable logic controller. Let's take a closer look at the process and explore how the program runs step by step.


1. Execution of the Loop Program


1.1 After the CPU is powered on, the startup block (OB100/101/102) is executed once. Which specific OB block runs depends on the system’s startup configuration settings.

1.2 Once the startup block completes, the system begins monitoring the cycle time. This time can be set in the hardware configuration within STEP7. If the cycle time is exceeded, the PLC will stop to prevent errors.

1.3 Following this, the CPU reads all input states from the input modules and other process image devices. This ensures that the current state of the system is captured accurately before any processing occurs.

1.4 The main loop program then begins execution. The program runs in a sequential manner, as shown in the diagram. For example, when OB1 executes a "Call FB" instruction, it moves into the called FB block and processes each statement in order.

1.4.1 If another function or function block is called within the FB, the program continues to the next level, executing statements in a hierarchical way.

1.4.2 Once the lower-level FC or FB finishes execution, the program returns to the original call point and continues with the remaining instructions.

1.4.3 When the FB execution is complete, the program returns to the calling location in OB1 and proceeds with the rest of the code.

1.4.4 This process continues until all instructions in OB1 are completed, marking the end of the main loop.

1.4.5 During the execution of OB1, the system may also handle interrupts such as cyclic interrupts (e.g., OB35) or fault interrupts (e.g., OB86). These interrupts are processed and then the program resumes from where it was interrupted.

1.5 After the main loop completes, the CPU writes the updated output values from the process image back to the output modules.

1.6 Finally, the cycle ends, and the system restarts from step 1.2, resetting the cycle timer for the next iteration.


2. Types of Blocks in STEP7


Understanding the execution flow of the PLC program leads us to explore the different types of blocks used in STEP7 programming. Each block serves a specific purpose and plays a role in organizing the program efficiently.

2.1 User Blocks

User blocks contain both program code and user-defined data. Some blocks are executed cyclically, while others are called only when needed, depending on the application requirements.


2.1.1 Organization Block (OB)

The OB block acts as the interface between the S7CPU and the user program. OB1 is typically used for continuous cyclic execution, but other OB blocks can be triggered based on events like time-based interrupts, hardware triggers, or diagnostic conditions.

2.1.2 Function Block (FB)

A function block is a reusable unit that contains logical operations and maintains its own memory space through an instance data block (DB). Parameters are passed via this DB, and local variables are stored in the stack. Data in the instance DB is preserved even after the block exits.

2.1.3 Function (FC)

A function is similar to a function block but does not have its own memory area. It uses the local stack for temporary variables, which are lost once the function completes. FCs are ideal for simple tasks that don’t require persistent data storage.

2.1.4 Data Block (DB)

Data blocks are pre-allocated memory areas that store data for use by functions and function blocks. They are read/write accessible and are part of the user program, allowing for flexible data handling.


2.2 System Blocks

System blocks are predefined functions and blocks built into the CPU’s operating system. They do not occupy user program space and can be called directly. Examples include SFCs, SFBs, and SDBs, which provide essential system functionality.

2.2.1 System Function (SFC)

SFCs are pre-programmed functions available for direct use. They handle tasks like module configuration, communication, and data transfer without requiring additional data blocks.

2.2.2 System Function Block (SFB)

SFBs are integrated system functions that must be assigned an instance DB. They are part of the operating system and are downloaded along with the user program for execution.

2.2.3 System Data Block (SDB)

SDBs are memory areas created by STEP7 tools and contain critical system data such as configuration parameters, communication settings, and more.


3. Programming Methods


Now that we understand the execution flow and block types, let's explore the different programming methods available in STEP7.

3.1 Linear Programming

This method involves writing all the program in a single, continuous block. It mimics the behavior of traditional relay circuits and is suitable for small, straightforward applications.

3.2 Modular Programming

In this approach, the program is divided into separate modules, each responsible for a specific task or device. The main OB determines which module to call, making it easier to manage complex systems.

3.3 Structured Programming

Structured programming allows users to create custom blocks with parameters that can be reused across the program. This method promotes modularity, reusability, and easier maintenance.

Choosing the right programming method depends on the complexity of the application and the programmer's preference. By using these techniques, you can design efficient, organized, and maintainable PLC programs.

HP Laptop 250 255 G10

HP Laptop 250 255 G10,HP Laptop 250 255 G10,HP N41352-001,HP 255 G10 top cover

S-yuan Electronic Technology Limited , https://www.syuanelectronic.com