Introduction to S7-1200 Siemens User Program Structure

Sep 14, 2023

Introduction to User Program Structure

The user program structure of S7-1200 and S7-300/400 is basically the same.

Modular programming

Modular programming divides complex automation tasks into smaller subtasks corresponding to the technical functions of the production process, each of which corresponds to a subroutine called a "block" that can be used to organize the program through mutual calls between blocks.


This program is easy to modify, troubleshoot, and debug. The block structure significantly increases the organizational transparency, comprehensibility, and maintainability of PLC programs. OB, FB, and FC all contain programs, collectively referred to as code blocks.

There is no limit to the number of code blocks, but they are limited by memory capacity.

The called code block can also call other code blocks, which is called nested calls.

Starting from program loop OB or startup OB, the nesting depth of S7-1200 is 16; Starting from interrupt OB, the nesting depth of S7-1200 is 6.

In block call, the caller can be various code blocks, and the called block is a code block other than OB. When calling a function block, you need to specify a background data block for it.

Organizational block

Organization block (OB) is the interface between the operating system and the production root sequence, called by the operating system, used for reporting the execution of scanning ring and interrupt program words, PLC startup, and error handling.

Each organizational block must have a unique OB number, some numbers before 123 are reserved, and other OB numbers should be greater than or equal to 123.

Specific events in the CPU trigger the execution of organizational blocks, and OBs cannot be called to each other, nor can they be called by FC and FB. Only startup events (such as diagnostic interrupt events or periodic interrupt events) can initiate the execution of OB.

1. Program loop organization block. OB1 is the main program in the user program, where the CPU loops through the operating system program. In each loop, the operating system program calls OB1 once.

Therefore, the program in OB1 is also executed in a loop. Multiple program cycles OB are allowed, with the default being OB1. The number of other program cycles OB should be greater than or equal to 123.

2. Start the organizational block. When the operating mode of the CPU switches from STOP to RUN, a STARTUP organizational block is executed to initialize certain variables in the program loop OB.

After executing the start OB, start executing the program loop OB. There can be multiple startup OBs, with the default being OB100. The number of other startup OBs should be greater than or equal to 123.

3. Interrupt organizational blocks. Interrupt handling is used to achieve rapid response to special internal or external events. If no interrupt event occurs, the CPU loops through OB1 and the blocks it calls.


If interrupt events occur, such as diagnostic interrupts and time delay interrupts, because OB1 has the lowest interrupt priority, the operating system immediately responds to the interrupt after executing the current instruction of the current program (i.e. at the breakpoint).

The CPU pauses the executing program block and automatically calls an organizational block (i.e. interrupt program) assigned to the event to handle the interrupt event.

After executing the interrupt organization block, return to the breakpoint of the interrupted program to continue executing the original program.

This means that some user programs do not need to be processed in each loop, but are only processed in a timely manner when needed. The program that handles interrupt events is placed in the OB driven by that event.


A function is a subroutine written by a user, abbreviated as FC, which is referred to as a function in STEP7V5.5. It contains code and parameters to complete specific tasks.

FC and FB (function blocks) have input and output parameters shared with the block that calls them. After executing FC and FB, return the code block that called it.

A function is a rapidly executing block of code that can be used to perform standard and reusable operations, such as arithmetic operations. Or complete technical functions, such as control using bit logic operations.

The same FC and FB can be called multiple times in different positions of the program, which can simplify the programming of repetitive tasks. The function does not have a fixed storage area, and after the function is executed, the data in its temporary variables is lost.

Function Block

Function blocks are user written subroutines, abbreviated as FB, and are referred to as function blocks in STEP7V5.5.

When calling a function block, you need to specify the background data block, which is a dedicated storage area for the function block. The CPU executes the program code in FB, saving the input, output parameters, and local static variables of the block in the background data block for access in subsequent scan cycles.

The typical application of FB is to perform operations that cannot be completed in one scan cycle. When calling FB, the corresponding background data block is automatically opened, and the variables of the latter can be used by other code blocks.

Using different background data blocks when calling the same function block can control different objects.

Some instructions of S7-1200 (such as timer and counter instructions that comply with IEC standards) are actually function blocks, and when calling them, it is necessary to specify the corresponding background data block.

Need Help? Chat with us

leave a message
For any request of information or technical support, fill in the form. All fields marked with an asterisk* are required.
Looking for FAQs?
Contact us #
+86 -15259245292

Our hours

Mon 11/21 - Wed 11/23: 9 AM - 8 PM
Thu 11/24: closed - Happy Thanksgiving!
Fri 11/25: 8 AM - 10 PM
Sat 11/26 - Sun 11/27: 10 AM - 9 PM
(all hours are Eastern Time)




Contacts Us