ORGANIZATION_BLOCK OB1
VAR_TEMP
// Temporary variable declaration
END_VAR
BEGIN
// Read input
"InputBit" := "I0.0";
// Simple logic processing
IF "InputBit" THEN
"OutputBit" := TRUE;
ELSE
"OutputBit" := FALSE;
END_IF;
// Write output
"Q0.0" := "OutputBit";
END_ORGANIZATION_BLOCK
In this example, we implemented a simple logic in OB1: when the input I0.0 is TRUE, the output Q0.0 is also TRUE; otherwise, Q0.0 is FALSE.
Note: The execution time of OB1 will affect the PLC’s scan cycle, so be careful to control the complexity of the program in OB1 to avoid extending the scan cycle.
OB100 – Startup Organization Block
OB100 is the startup organization block, which executes once when the PLC switches from STOP to RUN mode. We can perform some initialization operations here.
Here’s an example:
ORGANIZATION_BLOCK OB100
VAR_TEMP
// Temporary variable declaration
END_VAR
BEGIN
// Initialize variables
"CounterValue" := 0;
"SystemStartTime" := "Clock_Sys.LTOD";
// Set initial status
"PumpStatus" := FALSE;
"ValvePosition" := 0;
// Log system startup
#LogSystemStart();
END_ORGANIZATION_BLOCK
In this example, we initialized some variables at PLC startup, set some initial states, and called a function to log the system startup.
Tip: OB100 is suitable for setting the initial state of the system or performing operations that only need to run once at startup!
OB40 – Hardware Interrupt
OB40 is used to handle hardware interrupts. When a specified hardware event occurs (such as the rising or falling edge of an input signal), OB40 will be triggered to execute.
Let’s look at an example:
ORGANIZATION_BLOCK OB40
VAR_TEMP
// Temporary variable declaration
EventCount : INT;
END_VAR
BEGIN
// Increase event count
"TotalEventCount" := "TotalEventCount" + 1;
// Execute corresponding operation based on interrupt event
CASE "OB40_EV_CLASS" OF
1: // Rising edge trigger
"RisingEdgeDetected" := TRUE;
#HandleRisingEdge();
2: // Falling edge trigger
"FallingEdgeDetected" := TRUE;
#HandleFallingEdge();
END_CASE;
// Log interrupt occurrence time
"LastInterruptTime" := "Clock_Sys.LTOD";
END_ORGANIZATION_BLOCK
In this example, we executed different operations based on the type of interrupt event and logged some related information.
Note: The execution of OB40 will interrupt the main program, so the code in OB40 should be kept as simple as possible and return quickly after processing.
Practice Assignments
-
Try writing an OB1 to implement a simple traffic light control logic. -
Write an OB100 to initialize some system parameters at PLC startup. -
Use OB40 to implement an emergency stop button function.
Summary
Today we learned about several commonly used OB blocks in Siemens PLC:
-
OB1 – Main loop block, executed periodically -
OB100 – Startup organization block, executed at PLC startup -
OB40 – Hardware interrupt block, responds to hardware events
Mastering the use of these OB blocks can make your PLC programs more flexible and efficient. Remember, different OB blocks have different application scenarios, and it’s important to choose the right OB block to implement your control logic!
Friends, today’s learning journey on Siemens PLC ends here! Remember to get your hands on the code, and feel free to ask me any questions in the comments. I wish you a happy learning experience and continuous progress in learning Siemens PLC!