1. Basic Concept of OB
Definition: An Organization Block (OB) is a program block called by the CPU operating system to perform specific functions. Users write programs in the corresponding OB, and the CPU automatically executes it when the corresponding event occurs.
Priority: Each OB has a different priority. A high-priority OB can interrupt a low-priority OB. For example, a hardware interrupt (OB40) can interrupt the main loop program (OB1).
Trigger Event: Each OB has a reason for being called, known as the trigger event. For instance, the trigger event for a cyclic interrupt OB is a fixed time interval.
Classification: Based on functionality, OBs can be divided into the following major categories.
2. Detailed Explanation of Main OB Types
1. Cyclic Program OB
This is the core and most fundamental OB.
OB1: Main Loop Organization Block
Function: After the CPU starts, it continuously calls OB1 in a loop. It is the core carrier of the user’s main program.
Priority: Lowest priority (usually 1), can be interrupted by almost all other OBs.
Note: Any program written in OB1 should ensure that the execution time is short enough to avoid watchdog timeouts (which can lead to PLC shutdown). For long operations, other OBs (such as cyclic interrupts or time delay OBs) should be used.
2. Startup OB
Used for initialization operations when the PLC switches from STOP to RUN mode.
OB100: Warm Start (for S7-300/400) / Startup (for S7-1500)
Function: Executed once when the CPU starts. The process image and non-retentive memory (M), timers (T), and counters (C) are reset. The program then starts executing from the first instruction of OB1.
Changes in S7-1500: The distinction between warm start and cold start has been removed in S7-1500; OB100 is the standard startup OB.
OB101: Hot Start (only S7-400)
Function: Continues execution from the program breakpoint when power is restored after a power failure. All data before the power failure is fully retained. S7-300 and S7-1500 do not support hot start.
OB102: Cold Start (only S7-300/400)
Function: Executed once when the CPU starts. All data (including process image, M, T, C, whether retentive or non-retentive) is initialized to initial values. The program then starts executing from the first instruction of OB1.
3. Delay Interrupt OB
Used to execute a program once after a specified delay time.
OB20: After the delay time specified by the system function (SFC32 SRT_DINT) (e.g., 100ms), the CPU will call OB20 once.
Application: Suitable for scenarios requiring precise delayed execution without wanting to wait with a timer in the cyclic program.
4. Cyclic Interrupt OB
Interrupts the main loop program (OB1) at fixed time intervals to execute the interrupt program.
OB30 ~ OB38: These OBs are set with a fixed time interval (e.g., 100ms) during configuration, and the CPU will strictly call them periodically according to this interval.
Application: Used for periodic tasks requiring high time precision, such as PID closed-loop control, fast data acquisition, etc. OB35 is the most commonly used one.
5. Hardware Interrupt OB
Immediately interrupts the current program and executes the hardware interrupt OB when a specific hardware signal (such as the rising/falling edge of a digital input point, or analog over-limit) occurs.
OB40 ~ OB47: When a module with hardware interrupts (such as DI modules, FM modules) triggers a signal, the CPU will call the corresponding hardware interrupt OB.
Application: Used to handle urgent events requiring quick responses, such as emergency stop buttons, encoder zero pulses, etc.
6. Time Error Interrupt OB
Called when the CPU detects a time error during program execution.
OB80:
Trigger Event:
Loop timeout (OB1 execution time exceeds the set loop monitoring time).
Forward modification of system time.
The execution of the interrupt OB is frequently interrupted by another higher-priority OB.
OB startup is canceled due to CIR (Configuration Insertion Ready) errors.
Processing: If OB80 is written, the CPU will call it and report the error, but the PLC will remain in RUN mode. If OB80 is not downloaded, when a loop timeout error occurs, the CPU will directly jump to STOP mode.
7. Diagnostic Error Interrupt OB
Called when a module with diagnostic functions (such as analog modules, DP slaves) detects an error and reports it to the CPU.
OB82:
Trigger Event: Diagnostic information from the module (such as disconnection, short circuit, loss of 24V power) arrives or departs.
Application: Diagnostic and alarm programs can be written in this OB to obtain specific error information.
8. Rack Failure OB / DP Slave Failure OB
Called when there is a failure in the connection of distributed I/O (PROFIBUS-DP or PROFINET-IO).
OB86:
Trigger Event:
Arrival: The DP master station loses communication with one or more slaves.
Departure: Communication is restored.
Application: Used to handle the loss and recovery of distributed I/O stations, recording which station has failed.
9. I/O Access Error OB
Called when the program accesses a non-existent or faulty I/O module (e.g., directly reading PIW256, but that address has no module).
OB122:
Trigger Event: An I/O module that the CPU cannot address is accessed in the program (e.g., module does not exist, module failure, address error).
Processing: If OB122 is downloaded, the CPU will call it, and the PLC will remain in RUN mode. If OB122 is not downloaded, when an I/O access error occurs, the CPU will directly jump to STOP mode.
3. Summary and Usage Recommendations
OB Block Number Name Main Function Key Points
OB1 Main Loop Main program executed in a loop Must exist, lowest priority
OB100 Startup Initialization when PLC starts Standard startup OB for S7-1500
OB35 Cyclic Interrupt Fixed interval execution (e.g., PID control) High time precision
OB40 Hardware Interrupt Quick response to external signals Fast response speed
OB80 Time Error Handle loop timeout and other time errors Prevent CPU from shutting down due to timeout
OB82 Diagnostic Error Handle module diagnostic information Used for hardware diagnostics and alarms
OB86 Rack/Station Failure Handle DP/PN slave loss Used for distributed I/O diagnostics
OB122 I/O Access Error Handle erroneous I/O address access Prevent CPU from shutting down due to access errors