MOULDLKnowledge BaseElectrical Knowledge Base3D Model DownloadsMembership
中文版Model Store »
Knowledge Base › Electrical Knowledge Base › How do the DECO and ENCO instructions work on a Mitsubishi PLC?
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 the DECO and ENCO instructions work on a Mitsubishi PLC?

DECO is a decode instruction: it takes a bit number and turns on the matching point inside a target block. ENCO is the reverse: it looks at a block of bits in which only one point is ON and returns that point's number as a value. The two are inverses of each other, and they turn up in station selection, fault-code compression and sequence outputs. What really needs settling before you write them is the bit count n, the legal source range, and what the instruction does once the source goes out of range.

Two problems, two opposite directions

Two needs come up again and again on the shop floor. In the first, you hold a station number from 1 to 8 and want to energise the matching solenoid valve or indicator lamp. In the second, one of eight position sensors closes and you want to know which one it was. DECO (FNC 41) covers the first case and ENCO (FNC 42) the second. Neither changes the source data itself; they only convert between a number and the position of a bit.

Fixing the three operands of DECO

The form is DECO S D n: S supplies the bit number, D is the head of a block of 2 to the power n points, and n sets the bit count. Take DECO K3 Y0 K4: n is 4, so the target block runs from Y0 to Y17, sixteen points; the source value 3 turns on Y3, the fourth point from the head, and resets the other fifteen. The mistake most often made is reading n as "number of target points". The target count is 2 to the power n, so n = 4 consumes sixteen points in one block, and you have to leave that much room in the output area before wiring.

How ENCO picks a value, and what that means for priority

ENCO S D n runs the other way: S is a block of 2 to the power n consecutive points and D receives the bit number. The rule is to find the highest-numbered ON point in that block and send its number to D; if two points are ON at the same time, only the higher one is taken and the two are never added. That makes it a priority encoder: when several faults appear together, the higher-numbered one is reported first, which suits a "most severe first" fault code. When the source block is all zeros, the value written to D is defined by the model, so check the manual rather than assuming zero.

Bit count and source value have to line up

The bit count fixes the legal source range: the source may take integer values only from 0 to 2 to the power n minus 1. With n = 3 the largest valid value is 7; writing 8 is out of range, the instruction will not set the expected point, and the operation-error flag is raised. In the same way, the 2 to the power n target points must fall inside the valid device numbers of that model and must be consecutive, with no reserved area in between. Check bit count, source value and device range together.

Typical uses: station selection and fault numbering

For station selection, put the station number in a data register and let DECO drive the outputs. Changing stations then means changing one number instead of rearranging a row of contacts, and with an index register the target head can follow the axis number so a single instruction serves several stations. Fault numbering works the other way round: wire eight detectors to consecutive inputs and use ENCO to compress them into one code for the HMI, so the screen shows a single number instead of testing bits one by one. Where more than eight positions are needed, combine two results into a two-word value.

DECO and ENCO compared

ItemDECO (FNC 41)ENCO (FNC 42)
DirectionBit number to bit positionBit position to bit number
Source operandInteger from 0 to 2 to the power n minus 12 to the power n consecutive points, normally one ON
Destination operand2 to the power n consecutive bit pointsWord device holding the bit number
Usual bit count n1 to 81 to 8
ResultExactly one point in the block is ONThe block is written with the bit number
More than one source bit ONNot applicable, the source is a valueOnly the highest-numbered point is taken
Source out of rangeOperation-error flag set, output not as expectedHandled as defined by the model, check the manual
Points consumed2 to the power n points, taken as one blockOne word
Typical useStation selection, sequence outputs, digit displayAlarm numbering, priority encoding
With index registersTarget head can follow the axis numberSource head can be moved

Notes

Both instructions execute in the scan, so the result appears in the next scan after the source changes, and neither is suitable for capturing microsecond events. If faster response is needed, put the logic in an interrupt routine or drive the bits directly. Large bit counts are better handled another way, because 2 to the power n grows quickly — n = 8 already occupies 256 points, which rarely pays for itself on a run of outputs.

References

The definition of the instructions, their operand types and the error flags follow GB/T 15969.3 and the equivalent international standard IEC 61131-3 for programming languages, with the exact mnemonics and ranges as given in the Mitsubishi programming manual. Electrical connection, protection and earthing of the PLC and its circuits follow GB 5226.1 and IEC 60204-1; graphical symbols and terminal designations in the ladder diagram follow GB/T 4728; general requirements for the equipment are given in GB/T 15969.2.

Points to watch

⚠️ Three common traps:
(1) Reading n as the number of target points, so n = 4 gets a four-point block and the instruction overwrites the next twelve points, taking unrelated outputs with it;
(2) Not range-checking the source before it reaches the instruction, then chasing wiring and loads when the output simply never comes on;
(3) Forgetting that ENCO takes only the highest bit number, so two alarms at the same time report as one and the other is missed.

Placing the DECO target block at the end of the output area with room to spare keeps it from overwriting other outputs; programs that accept an ENCO result should test for an empty source before displaying it and keep the individual bit readings for tracing simultaneous alarms. Switch off, isolate and prove dead before changing logic of this kind, and confirm that a spurious output cannot injure anyone or damage the machine.

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