Message packing, encoder data merging, high/low word swaps — all come down to "shift and concatenate". Such pure calculation is stateless with same-in-same-out behaviour: exactly an FC's job. This article gives the standard patterns (building a double word, splitting halves, extracting segments, BCD conversion) as copy-ready templates.
High word Dw_H and low word Dw_L combine into a double word:
Ladder form: assign the double word whole with DMOV-type instructions, or write the halves separately (many platforms allow writing the high/low halves of a double word, e.g. Mitsubishi D0 and D1 as the two halves). One caution: treat the low word as unsigned when concatenating — if the low word is interpreted signed (negative), direct addition corrupts the result. Mask the low word with AND 0xFFFF before joining. The double's sign comes from the high word's sign bit; never sign-extend the low word.
| Goal | Method | Note |
|---|---|---|
| Take the high 16 bits | Shift the double right 16 and take the low word (or read the high half directly) | Reading the half-word is most direct |
| Take the low 16 bits | Double AND 0xFFFF | Masked extraction |
| Take any segment | AND mask → SHR into place | Mask first, then shift — value right-aligned |
| Replace a segment | AND NOT mask to clear → OR the shifted-in new value | Clear then set — both steps required |
"Mask first, then shift" — memorise the order: extracting = AND the mask, then shift right to bit 0; replacing = clear the segment in the target, then OR in the new value. Reversed, the segment lands skewed and the debugging is miserable.
Three classic mistakes: ① arithmetic right shift of a signed (negative) value fills sign bits — unsigned data must not be shifted as signed; ② left shift is not always ×2: overflowed bits vanish, so a status word (bits with meaning) destroyed by a left shift is a data-semantic error, not arithmetic; ③ rotate (ROL/ROR) and shift (SHL/SHR) serve different purposes: message rotation uses rotate, numeric scaling uses shift — mixing them is a semantic error.
| Need | Which | Why |
|---|---|---|
| Value ×2 / ÷2 | SHL / SHR | Overflow handled by numeric semantics |
| Rotating a status word | ROL / ROR | No bits lost, positions change only |
| Extract a segment | SHR + AND | Mask first, then shift |
| Sign extension | Platform instruction | Hand-rolled versions err easily |
Instruments and thumbwheel switches commonly emit BCD (one decimal digit per 4 bits). Wrap conversions in an FC: BCD → integer = extract each nibble (AND 0xF, SHR 4) and weight it (units ×1, tens ×10, hundreds ×100); integer → BCD reverses by divide-and-remainder into nibbles. Most platforms have built-in BCD instructions (BIN/BCD converts) — prefer them; the self-written FC's value is "with range checking": a nibble above 9 raises an error, keeping sick instrument data out of the program.
| Message field | Source | Packing action |
|---|---|---|
| Header | Constant 0xAA55 | MOV into the first word |
| Status bits | Various M flags | OR each into the status word |
| Measurement | Float 12.3 mm | ×10, round to 123, write (or per protocol scaling) |
| Serial double word | D100 (double) | High/low words written to the two message words |
| Checksum | Sum of all preceding bytes | FOR-loop accumulate, AND 0xFF |
The packing FC's input is "the data area to pack", its output "the message buffer" — read and write areas separated (pure-function discipline), so main and comms tasks may both call it safely. Do the scaling (×10, ×100) uniformly at packing, with the protocol's scaling factors written into the FC comments.
| Symptom | Cause | Fix |
|---|---|---|
| Assembled double word jumps around | Low word not masked for sign | AND 0xFFFF before joining |
| Extracted segment skewed | Shift before mask, order reversed | Mask first, then shift |
| Negative values shift into nonsense | Unsigned data shifted as signed | Unsigned type or dedicated instructions |
| Checksum always wrong | Byte order (endianness) mismatches the protocol | Adjust word order per protocol, verify with a capture |
| BCD conversion occasionally garbled | No nibble validation | Nibble > 9 raises an error |
The whole shift-and-concatenate craft compresses to three sentences: build = shift up (or weight) and add, masking the low word; split = mask first, then shift; replace = clear the segment, then set it. Write a pure-function FC five-pack (build / split / extract / replace / BCD) into the company library and bit-level data work never gets hand-rolled again.
Shift/rotate/word-double-word instruction syntax follows the PLC's programming manual applied-instruction chapters (SHL/SHR/ROL/ROR/DMOV etc.); byte order and scaling follow the communication protocol documents; BCD instruction behaviour follows the manuals; communication data handling does not alter safety-function independence per GB 5226.1 / IEC 60204-1.
For every new protocol, write a "scaling table" first: each field's type, byte order and scaling factor; the packing FC follows the table — table right, message right.