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 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.
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.
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.
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.
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.
| Item | DECO (FNC 41) | ENCO (FNC 42) |
|---|---|---|
| Direction | Bit number to bit position | Bit position to bit number |
| Source operand | Integer from 0 to 2 to the power n minus 1 | 2 to the power n consecutive points, normally one ON |
| Destination operand | 2 to the power n consecutive bit points | Word device holding the bit number |
| Usual bit count n | 1 to 8 | 1 to 8 |
| Result | Exactly one point in the block is ON | The block is written with the bit number |
| More than one source bit ON | Not applicable, the source is a value | Only the highest-numbered point is taken |
| Source out of range | Operation-error flag set, output not as expected | Handled as defined by the model, check the manual |
| Points consumed | 2 to the power n points, taken as one block | One word |
| Typical use | Station selection, sequence outputs, digit display | Alarm numbering, priority encoding |
| With index registers | Target head can follow the axis number | Source head can be moved |
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.
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.
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.