MOULDLKnowledge BaseElectrical Knowledge Base3D Model DownloadsMembership
中文版Model Store »
Knowledge Base › Electrical Knowledge Base › How do you use a data word as a group of flags?
HomeModels
Table of contents

On this page

PLC & Programming (100 articles)

Quick Reference

  • PLC & Programming
  • Electrical & Electronic Basics (monthly)(每月更新中)
  • Electrical Control & Drawings (monthly)(每月更新中)
  • Drives & Actuators (monthly)(每月更新中)
  • Sensors & Detection (monthly)(每月更新中)
  • Industrial Communication & Networks (monthly)(每月更新中)
  • HMI & Host Systems (monthly)(每月更新中)
  • Industrial Robots & Motion Control (monthly)(每月更新中)
  • Mechatronic System Integration (monthly)(每月更新中)
  • Fault Diagnosis & Standards (monthly)(每月更新中)

How do you use a data word as a group of flags?

When intermediate flags multiply, M devices run short, or you want block snapshots, using the 16 bits of one data register D as 16 flags is standard practice: individual bits read and write directly (D100.3), and the whole word copies in one instruction. Done well it saves resources and is elegant; done casually it becomes a mine where "a bit vanished and nobody can find where". The sections below cover the syntax, read/write, snapshot and the rules.

1. Basic syntax: word.bit addressing

OperationSyntax (ladder/ST)Meaning
Read a bitLD D100.3Conducts when bit 3 of D100 is ON
Write a bitSET D100.3 / RST D100.3Set or reset bit 3 alone
Whole-word testLD= D100 K0All 16 flags OFF test
Whole-word snapshotMOV D100 D20016 flags backed up in one instruction

Platform note: Mitsubishi FX3U/FX5U support bit designation on word devices (D100.3); older series or some brands do not — there, use WAND/WOR/WXOR bit arithmetic for indirect access (below). Confirm which style your platform supports before coding.

2. Platforms without direct bit addressing: masked access

Read bit n: D100 AND K(2^n) — non-zero means ON. Set: D100 = D100 OR K(2^n). Reset: D100 = D100 AND NOT K(2^n) (masked AND with the inverted mask). Wrap "set / reset / read" into three small function blocks taking the D address (or a fixed block plus bit number), so the whole project calls them uniformly instead of hand-writing masks everywhere.

Set bit n: D100 ← D100 OR (1 << n); reset: D100 ← D100 AND NOT (1 << n)

3. Two killer applications: step sub-states and snapshots

① A step's sub-states (waiting / running / done / fault) live in one word's four bits, and whole-state tests become word comparisons (D100 = K8 means "done") — cleaner than four separate Ms. ② Alarm/permission flag groups snapshot wholesale: MOV to the backup area before a stop, restore after — 16 flags saved and restored in one instruction each. With BMOV, a row of Ds (D100~D103) becomes a 64-flag group whose backup is still a few instructions.

4. The rules: no rules means hidden mines

RuleReason
Bit-purpose table in the commentsWhat D100.0~.15 each mean, written down — otherwise in three months nobody knows what .7 does
One word, one ownerTwo logics writing different bits of one word; one bad mask and they trample each other
No whole-word assignment on shared wordsMOV K0 D100 clears the bits other functions own
Comms mapping words kept separateHost-side word reads should not mix with flag groups

The biggest mine is the third: "clearing the flag word with a quick MOV K0" — other functions' bits die with it. Shared words clear only through bit operations on your own bits, or through one agreed clearing point.

5. Common errors

SymptomCauseFix
A flag "disappears by itself"A whole-word assignment elsewhere overwrote itFind the MOVs, switch shared words to bit operations
Masked set corrupts other bitsMask miscalculated / OR written as additionUse the packaged set/reset blocks
Host reads garbage flagsByte order / nibble alignment differsAgree alignment with the host and test once live
Restored snapshot partly wrongSnapshot and restore areas mismatchPair them on the same parameters
Nobody knows a bit's meaningNo bit-purpose tableAdd the table; make it a mandatory comment item

6. Notes

"A word as a flag group" organises bit resources so they can be operated as a whole: bit-level access for flexibility, word-level for efficiency. The preconditions are a clear bit table and single ownership. When resources are tight or block snapshots are needed it is a sharp tool; used without a table it is an unfindable mine — the difference is entirely discipline.

7. References

Word-device bit designation and bit-instruction syntax follow the PLC's programming manual (Mitsubishi FX3U/FX5U applied instructions; WAND/WOR/WXOR and bit designation); host byte-order conventions follow the communication protocol documents; flag groups feeding safety interlocks still pass the unified safety stage per GB 5226.1 / IEC 60204-1.

8. Cautions

⚠️ Three traps to avoid:
① A whole-word MOV K0 clearing bits owned by other functions — low-probability recurring faults;
② No bit-purpose table — maintenance becomes per-bit cross-reference archaeology;
③ Comms mapping and flag groups sharing one word — the host reads nonsense.

The day a flag-group word is created, its bit table goes into the program-header comments — that table is the word's household register; no register, no flag word.

Related reading

Related 3D models: Ready to build it? Download free 3D models & CAD drawings at MOULDL »
MOULDL · Electrical Knowledge Base · free self-study reference for electrical & automation engineers, 3D CAD models at MOULDL