Bitwise OR for Output Override Patterns in S7-1500 TIA V17
When porting control logic between PLC platforms, the bitwise OR pattern used for output forcing is one of the most commonly mis-translated idioms. Mitsubishi MELSEC programs frequently aggregate an 8-bit sequence value and an 8-bit HMI force value into a physical output byte with a single word-level OR instruction, expecting the compiler to fold the operation into eight parallel bit operations. Engineers porting that code to a Siemens SIMATIC S7-1500 in TIA Portal V17 often default to writing eight explicit rung pairs - one per bit - which inflates scan time, increases maintenance burden, and obscures the original intent. This reference documents the single-instruction equivalents in STL, SCL, LAD, and FBD, and shows how to migrate from the legacy M-area pattern into a structured, data-block based override model that aligns with S7-1500 best practices as documented on the Siemens Industry Online Support and the SIMATIC S7-1500 product page on siemens.com.
1. Problem Definition
The original Mitsubishi logic appears in the field as two flag banks - typically M100 through M107 for sequence outputs and M150 through M157 for HMI-driven force outputs - that converge on the physical output byte Y0. The Boolean equation is bitwise:
Y0.0 := M100.0 OR M150.0
Y0.1 := M101.0 OR M151.0
Y0.2 := M102.0 OR M152.0
Y0.3 := M103.0 OR M153.0
Y0.4 := M104.0 OR M154.0
Y0.5 := M105.0 OR M155.0
Y0.6 := M106.0 OR M156.0
Y0.7 := M107.0 OR M157.0
The MELSEC OR instruction collapses these eight equations into a single 1-byte operation. The engineer's question to the field is: how do I express the same collapse on an S7-1500 in TIA Portal V17 without writing eight parallel rungs?
2. Memory Area Comparison: MELSEC M/Y vs. SIMATIC M/Q
| Element | Mitsubishi MELSEC | Siemens S7-1500 (TIA V17) | Notes |
|---|---|---|---|
| Sequence flag bank (8 bits) | M100 - M107 | %M100.0 - %M100.7 (in %MB100) | M area supported on all S7-1500 CPUs; size is CPU-dependent (consult the S7-1500 datasheet on the Siemens product page) |
| Force flag bank (8 bits) | M150 - M157 | %M150.0 - %M150.7 (in %MB150) | HMI writes here |
| Physical output (8 bits) | Y0 - Y7 (Y0 device) | %Q0.0 - %Q0.7 (in %QB0) | Output process image, byte 0 |
| Bitwise OR aggregator | Single OR instruction (K8M100 OR K8M150 -> K8Y0) | OW (STL), OR operator (SCL), WOR_W box (LAD/FBD) | Single-instruction form available in all four languages |
| Recommended modernization | n/a | Move to optimized DB (e.g., DB_IO_Override) | Siemens best practice: minimize M area in S7-1500 designs |
3. S7-1500 Bitwise OR Instruction Set
The CPU 1500 family implements bitwise OR for byte, word, and double-word operands in all four programming languages. TIA Portal V17 maps the operation as follows:
| Language | Byte (8 bits) | Word (16 bits) | DWord (32 bits) |
|---|---|---|---|
| STL (Statement List) | OB | OW | OD |
| SCL | OR operator on BYTE | OR operator on WORD | OR operator on DWORD |
| LAD / FBD | OR_B (Bit Logic catalog) | WOR_W (Word Logic catalog) | WOR_DW (Word Logic catalog) |
For the 8-output scenario in the original Mitsubishi code, the byte-level instruction is the natural fit. Word- and double-word forms are required when the force/sequence banks are wider (e.g., 16 or 32 outputs grouped on a single output module).
4. Step-by-Step Implementation in TIA Portal V17
4.1 Prerequisites
- S7-1500 CPU (any 1500-series such as CPU 1511-1 PN, CPU 1515-2 PN, CPU 1518-4 PN/DP) configured in a TIA Portal V17 project. Verify the CPU firmware is on the support list for V17 (consult the TIA Portal V17 readme that ships with the installation media).
- TIA Portal V17 (Update 4 or later recommended) installed on a 64-bit Windows engineering station.
- A program block (OB1, a custom FC, or FB) to host the OR logic.
- Default tag table or optimized data block for source operands. The M-area form requires no extra declaration; M is a global area in the CPU.
- PLCSIM V17 for offline simulation (optional, recommended for pre-commissioning testing).
4.2 STL (Statement List) Implementation
STL is the lowest-overhead path: a load, a load, a byte OR, and a transfer.
// S7-1500 STL, OB1 segment 1
L %MB100 // Load sequence byte
L %MB150 // Load force byte
OB // Bitwise OR of ACCU1, ACCU2 (8-bit)
T %QB0 // Transfer result to output byte 0
For wider banks the same pattern uses OW (word) or OD (double word). Example: 16 outputs driven by %MW200 and %MW250 collapsed into %QW2:
// S7-1500 STL, 16-output version
L %MW200
L %MW250
OW
T %QW2
4.3 SCL (Structured Control Language) Implementation
SCL makes the operation declarative and is the preferred language in TIA V17 for block-based logic. The OR operator on integer-typed tags (BYTE, WORD, DWORD) is bitwise:
// SCL, symbolic DB form
"DB_IO_Override".ResultByte :=
"DB_IO_Override".SequenceByte
OR
"DB_IO_Override".ForceByte;
"Outputs".QB0 := "DB_IO_Override".ResultByte;
If the operands are kept in the M area (per the original Mitsubishi convention), the same line reads:
// SCL, M-area form
%QB0 := %MB100 OR %MB150;
Compile and download to the CPU. SCL emits the OB/OW/OD instruction directly; no ladder is generated for the OR on integer operands.
4.4 LAD (Ladder) Implementation
From the LAD toolbox, drop a Word Logic box (e.g., WOR_W) on the rung. Wire IN1 to %MB100, IN2 to %MB150, and OUT to %QB0. For the byte variant, use the OR_B box (Bit Logic catalog). The TIA V17 LAD catalog is searchable: type 'OR' in the instruction search field at the top of the editor and pick the size that matches the operand width.
[ WOR_W ]
IN1: %MB100
IN2: %MB150
OUT: %QB0
EN : BOOL ENO: BOOL
4.5 FBD (Function Block Diagram) Implementation
FBD exposes the same WOR_W box as LAD but with explicit signal-flow style. Connect %MB100 to the first input, %MB150 to the second, and route the output pin to %QB0. The compiled instruction is the OB/OW/OD regardless of the language the editor uses to draw it.
5. Migrating from M Area to an Optimized Data Block
The S7-1500 Programming and Operating Manual recommends minimizing the use of global M memory in new programs in favor of data blocks with symbolic access. Optimized blocks give you:
- Symbolic-only access - no absolute address dependency, easier HMI tag import.
- Per-tag retentivity setting (Inspector > Retain).
- Single download in the case of changes (only modified tags reload on incremental download).
- Better online/offline comparison results.
5.1 Declare the Data Block
Add a new data block named, for example, DB_IO_Override, with optimized access enabled. Declare the following structure:
| Name | Data Type | Offset | Retain | Comment |
|---|---|---|---|---|
| SequenceByte | BYTE | 0.0 | Non-retain | Sequence outputs from control logic |
| ForceByte | BYTE | 1.0 | Retain | Operator-driven force bits from HMI |
| ResultByte | BYTE | 2.0 | Non-retain | Output of the OR aggregation |
5.2 Migrate the Logic
// SCL, DB-based form
"DB_IO_Override".ResultByte :=
"DB_IO_Override".SequenceByte
OR
"DB_IO_Override".ForceByte;
"Outputs".QB0 := "DB_IO_Override".ResultByte;
Update the sequence logic to write to DB_IO_Override.SequenceByte and the HMI tags to write to DB_IO_Override.ForceByte.%X0 through %X7 (slice access). With optimized access enabled, slice access on a BYTE is supported in TIA V17.
5.3 AT View for Mixed Bit/Byte Access
If legacy code writes individual bits (M100, M101, ...) but a downstream block needs the byte, declare an AT view in a non-optimized DB or FB static section:
// FB static, non-optimized block
bSequenceByte : BYTE; // Offset 0.0
bForceByte : BYTE; // Offset 1.0
bResultByte : BYTE; // Offset 2.0
AT bSequenceByte : ARRAY[0..7] OF BOOL;
AT bForceByte : ARRAY[0..7] OF BOOL;
AT bResultByte : ARRAY[0..7] OF BOOL;
The AT construct overlays the bit array on the same memory, so writes through the bit symbols appear in the byte. This pattern enables migration without restructuring all writers.
6. Bit-by-Bit Alternative (When Aggregation Is Not Possible)
If a single word-level OR is not possible - for example, a third-party block expects individual bit variables - the explicit per-bit form is:
// SCL, per-bit form (not recommended when word-OR is possible)
"DB_IO_Override".ResultBits[0] :=
"DB_IO_Override".SequenceBits[0]
OR "DB_IO_Override".ForceBits[0];
"DB_IO_Override".ResultBits[1] :=
"DB_IO_Override".SequenceBits[1]
OR "DB_IO_Override".ForceBits[1];
// ... repeat for indices 2..7
This form inflates the compiled code to eight rungs, costs more scan time, and obscures the relationship between the two banks. Use only when the consumers require bit-granular tags.
7. HMI Tag Integration
The HMI (WinCC Comfort/Advanced or Unified in TIA V17) writes the force bits. Recommended practice:
- Connect the HMI tag to the symbolic bit slice, e.g., DB_IO_Override.ForceByte.%X0 for bit 0. With optimized blocks, the slice access syntax is supported in the HMI tag editor.
- Set the HMI tag acquisition mode to 'Cyclic continuous' with a 1 s cycle for operator override bits. Faster updates are unnecessary and increase HMI-CPU bus load.
- Configure the HMI button as a momentary toggle. Bit 0 of the force byte stays latched at the CPU because the DB tag is declared Retain and the HMI only writes on operator action.
- Display the effective output state on the HMI by reading DB_IO_Override.ResultByte.%X0 (the OR-aggregated bit). Never have the HMI do its own OR - that creates a visual/safety disagreement between the HMI indication and the actual output.
8. Verification, Commissioning, and Watch Tables
Verify the logic in three layers before production release:
- Offline simulation: TIA V17 PLCSIM V17 supports STL, SCL, LAD, and FBD code on simulated S7-1500 CPUs. Force the sequence and force bytes to known patterns and observe the result byte.
- Online watch table: create a watch table with the operands in the order they are used. Use force values for testing only; do not leave forced values in production.
// Watch table rows %MB100 BYTE 'Sequence byte' %MB150 BYTE 'Force byte' %QB0 BYTE 'Output byte' %M100.0 BOOL 'Seq bit 0' %M150.0 BOOL 'Force bit 0' %Q0.0 BOOL 'Output bit 0' - HMI simulation: use the TIA V17 HMI runtime simulator (WinCC RT) to drive the force bits and confirm the output bits follow the OR truth table.
8.1 Truth-Table Spot Check
| Sequence bit (M100.x) | Force bit (M150.x) | Output bit (Q0.x) | Pass/Fail |
|---|---|---|---|
| 0 | 0 | 0 | Expected 0 |
| 0 | 1 | 1 | Expected 1 |
| 1 | 0 | 1 | Expected 1 |
| 1 | 1 | 1 | Expected 1 |
All four cases for each of the eight bits should be confirmed before the system is released to production.
9. Troubleshooting Matrix
| Symptom | Likely Cause | Diagnostic Step | Resolution |
|---|---|---|---|
| Output byte stays 0 even when force bit is set | HMI writes to the wrong tag (e.g., M100 instead of M150) | Watch the force byte from the HMI; verify with the HMI tag list cross-reference (Project tree > Cross-references) | Re-bind the HMI tag to the force bank |
| Output byte reflects sequence only; force has no effect | OB/OW instruction is not being executed (wrong OB, conditional skip, or scan order) | Online > Monitor/Modify; confirm the OR instruction's accumulator values; place a breakpoint if debug-allowed | Place the OR logic in OB1 (or the main cyclic OB) and remove the gating condition |
| Output bits are inverted from expected | Source code wrote Y0 := NOT (M100 OR M150) in error, or HMI uses inverted polarity | Inspect the source code line by line; cross-check with the operator manual | Remove unintended inversion; correct the HMI button event |
| OR result is zero on the first scan after download | Optimized DB tags with retain configuration not loaded | Check the online / offline comparison; run 'Download to device' with full project option (not 'Firmware update only') | Confirm retain attribute; perform full download |
| Compiler error: data type mismatch | Mixing BYTE and WORD operand types in the OR | Right-click > Go to Definition on the operand and confirm the data type | Use a type cast (BYTE_TO_WORD) or align operand sizes |
| Compiler error: slice access on optimized block not allowed | Older firmware CPU with TIA V17 project may reject slice access on some tags | Check CPU firmware under Online > Accessible Devices; consult the TIA V17 readme | Update CPU firmware, or use a non-optimized DB with AT view |
| OR result is correct in PLCSIM but wrong on real CPU | Differences in start-up behavior (OB100 vs OB1); initial values of M area | Online > Monitor all M bytes used; check OB100 start-up values | Initialize M bytes in OB100 to the expected default |
| Compile warning: M area used in S7-1500 program | Information only; some organizations prohibit M-area use | Project tree > right-click > Properties > Compiler > 'Warnings as errors' | Migrate to a data block per Section 5 |
10. Performance and Timing Notes
On a CPU 1515-2 PN, a single OW (word-OR) and the surrounding L/T execute in well under 1 microsecond of instruction time; a 32-bit OD is comparable. The byte-level OB is the cheapest of the three. Replacing eight per-bit rungs (each carrying two contacts, one coil, and a network terminator) with a single OW typically reduces the relevant network size by 60-80% and removes seven of eight branch evaluations per scan. In high-speed applications (e.g., 1 ms cyclic task) the savings can be measurable; for typical 10-100 ms cycle times the benefit is mainly in readability and reduced instance data block overhead.
For OB execution order on the S7-1500, see the S7-1500 System Manual (Section 'Program execution'). The OR aggregation should be placed after the sequence and force writers have completed for the current cycle. This is naturally the case if all three are written in OB1 or in a single FC/FB called once from OB1.
11. Safety and Override Audit Considerations
The OR override pattern is essentially a software OR-merge of a control path and an operator path. In a safety-relevant application this is a forced bypass that the safety logic must account for. S7-1500 safety CPU variants (CPU 1515F, CPU 1516F, CPU 1518F) keep safety-related outputs in a separate address space (F-I/O) and do not use the standard OR merge; safety outputs must be sourced from a safety program (F-program) executed in the F-runtime. The standard OR merge shown above is appropriate for non-safety outputs only.
For audit purposes, the force byte should be logged. In TIA V17, enable the data block change log on the force byte (Inspector > Audit > 'Log changes') so the WinCC Audit option can record every operator write. The sequence byte is generated by the control program and typically does not need an audit log.
12. Cross-Platform Comparison
| Aspect | Mitsubishi MELSEC (GX Works 3) | Allen-Bradley CompactLogix / ControlLogix | Siemens S7-1500 (TIA V17) |
|---|---|---|---|
| 8-bit aggregation | Single OR instruction on K8 device (K8M100 OR K8M150 -> K8Y0) | No native 8-bit OR; use FAL with mask 16#00FF and OR mode, or eight explicit XIC-OTE branches per bit | %QB0 := %MB100 OR %MB150; or STL OB |
| Symbolic tags | Optional labels in GX Works 3 | Controller tags / program tags; required by convention | Symbolic access on optimized DB; recommended |
| Bit slice syntax | M100.0-style | MyTag.0 (tag must be BOOL) | %M100.0 or symbolic tag.%X0 |
| Audit / change log | Not native in CPU; external SCADA logging | FactoryTalk Logix Echo / ChangeLog in supported firmware | DB change log + WinCC Audit option |
| Safety I/O | Separate MELSEC-QS or MELSEC iQ-R safety CPU | GuardLogix; safety tags in safety task | F-CPU with F-runtime; safety outputs not writable from standard program |
13. Field Commissioning Checklist
- Confirm CPU firmware is on the TIA V17 support list (see TIA V17 readme).
- Compile the project - no errors, no remaining M-area warnings (if policy requires).
- Download the project to the CPU; perform a full download (not 'Firmware update only').
- Online > Go online > Monitor all relevant tags: %MB100, %MB150, %QB0, and their bit slices.
- From the HMI, force each of the eight force bits ON one at a time. Verify the corresponding output bit follows.
- From the HMI, force each of the eight force bits OFF. Verify the output bit returns to the sequence value.
- Cycle power to the CPU. Verify retain attributes are set correctly - the force byte should retain the last value; the sequence byte should reset to its initial value (or also retain, per design).
- Remove the HMI simulation; hand off to operations. Document the override procedure in the operator manual.
14. Common Pitfalls
- Forgetting the OR is bitwise. On DWORD operands, the OR acts on 32 bits, not 8. If the source code intended a Boolean OR of 8 separate BOOL tags, the explicit per-bit form is the correct interpretation. The Mitsubishi pattern with consecutive M bits packed into one byte is the case where word-OR is correct.
- Writing to a slice that crosses the operand boundary. %MW100.%X0 is a valid slice; %MB100.%X8 is not - it crosses into %MB101. The compiler will reject it.
- Optimized vs. non-optimized block confusion. Slice access and symbolic-only access are features of optimized blocks. With non-optimized blocks, absolute addresses work but symbolic-only is disabled, and AT views are valid for overlays.
- Output process image vs. direct I/O. Writing to %QB0 updates the output process image, which is then copied to the physical outputs at the start of the next cycle (or immediately for some I/O modules with direct output access). If the application requires immediate output update, use the %Q:P0 syntax - but the OR pattern itself is unaffected.
- M-area clearing on STOP-RUN. The M area is not cleared automatically on STOP->RUN transition. If the M area must be in a known state, initialize in OB100 (start-up OB).
15. Glossary
| Term | Definition |
|---|---|
| OB (in STL) | Bitwise OR for byte operands. 'O' = OR, 'B' = byte. |
| OW | Bitwise OR for word (16-bit) operands. |
| OD | Bitwise OR for double-word (32-bit) operands. |
| WOR_W | LAD/FBD box for word OR. 'W' suffix = word. |
| Optimized block | Data block with symbolic-only access and per-tag retentivity, default for new S7-1500 DBs. |
| AT view | Overlay of one data type onto another, used for bit/byte mixing in non-optimized blocks. |
| Slice access | Addressing a sub-element of a tag, e.g., MyWord.%X3 for bit 3. |
| F-runtime | Safety program execution context on F-CPUs; isolated from standard program writes. |
FAQ
What is the direct equivalent of a Mitsubishi K8Y0 device OR in TIA Portal V17 for the S7-1500?
Use a byte-level bitwise OR. In SCL, write %QB0 := %MB100 OR %MB150;. In STL, load both bytes, execute OB, transfer to %QB0. In LAD/FBD, drop a WOR_W box (or OR_B for the byte variant) and wire the operands. No eight rungs are required - the instruction acts on all eight bits in parallel.
Can I write to the force byte from the HMI when the data block is optimized?
Yes. TIA Portal V17 supports symbolic HMI tag access to optimized data blocks. In the HMI tag editor, browse the project, navigate to the DB, and select the bit slice (for example, DB_IO_Override.ForceByte.%X0). Confirm the connection is established in the HMI's Connections editor and that the HMI runtime has read/write access to the CPU. Avoid the legacy absolute-pointer approach - it bypasses the symbolic layer and complicates future tag moves.
Why is the compiler warning about M-area use on my S7-1500 project?
The M area is fully supported on S7-1500 CPUs, but TIA Portal V17 may emit an informational warning when the standard program reads or writes M bytes, because Siemens best practice is to consolidate global state in optimized data blocks. The warning does not prevent download. If your organization prohibits M-area use, migrate the flag banks to a DB as described in Section 5. If you intend to retain the M area for retrofit reasons, the warning can be suppressed per project in the project properties.
Does the bitwise OR pattern work on F-CPU safety outputs?
No. Fail-safe outputs on an S7-1500 F-CPU are owned by the F-runtime and cannot be written from a standard program; attempting to do so causes a compile-time access error. The OR merge shown in this article applies only to standard outputs (%Q of non-F modules). Safety outputs must be sourced from the F-program.
How do I verify the OR result without driving the real outputs?
Use PLCSIM V17 for offline verification, then move to a real CPU with a watch table. In the watch table, monitor %MB100, %MB150, %QB0, and the individual bit slices. To exercise the logic without driving the field, set the force byte to a known value (e.g., 16#A5) and confirm the output byte matches the OR of the sequence byte and 16#A5. Disable the physical output module via the CPU's 'Disable peripheral output' option in the device configuration if the application requires it during commissioning.