IMDTouchKnowledge BaseShop main store

How-to

Initializing a packed GRAFCET integer with one MOV instruction

PLCs and controllers Published 7 min read

Short answer

Write 0x1 to the whole integer variable in one MOV block: the write replaces every bit, so all step bits clear and bit zero — the initial step — sets. 0x00000001 and 0x1 are the same stored value; the editor only masks leading zeros. Evaluate transitions against a previous-scan copy so the sequence advances one step per scan.

On this page
  1. How do I enter a hexadecimal constant in a MOV block?
  2. Why does 0x00000001 display as 0x1?
  3. Does one MOV clear every step bit and set step zero?
  4. What do the .X[0], .B[0] and .W[0] suffixes write?
  5. How do I stop the sequence skipping steps inside one scan?
  6. Packed integer, step number or Boolean array: which should I use?
  7. How do I reach drive parameters that are missing from the PDO mapping?
  8. Questions asked

Write hexadecimal 0x1 to the whole integer variable in one MOV block. The instruction replaces the variable's entire content, so every step bit clears and bit zero — the initial step — sets. Leading zeros are display-only: the editor shows 0x00000001 as 0x1 and the stored value is identical. This is the ladder equivalent of an operator-block assignment such as 16#0001 in the PLC engineering tool you came from, on a platform whose editor offers no SFC language, so the GRAFCET lives in ladder and its state lives in an integer.

How do I enter a hexadecimal constant in a MOV block? #

Type the source constant as 0x1: the 0x prefix marks hexadecimal in the logic editor, the same role the 16# prefix plays in an operator block on a conventional PLC engineering tool, and the MOV block accepts the notation directly.

Set the block up once, in this order:

  1. Declare the state variable as an integer. The example uses a 32-bit integer named G7_securite for the safety-sequence GRAFCET; the same project carries G7_conduite and one G7_TacheX per task.
  2. Place a MOV block and set the destination to the bare variable name — G7_securite, with no .X, .B or .W suffix.
  3. Enter 0x1 as the source constant.
  4. Trigger it from the initialization demand on your GEMMA, so it executes once per demand rather than every scan.
  5. Verify online: the variable reads 1, G7_securite.X[0] reads 1, and every other X[n] reads 0.

If this logic is one leg of a move between engineering tools, check every variable and address after the transfer, not just the new rungs — validating HMI variables during a software migration covers that pass.

Why does 0x00000001 display as 0x1? #

Because the leading zeros carry no value: 0x00000001 and 0x1 both equal decimal 1, and the editor masks non-significant zeros on the left, the way most numeric fields do; the stored bit pattern is identical either way.

The masking is formatting, not a conversion. A 32-bit integer holds eight hexadecimal digits and a 16-bit integer four, but the value 1 occupies only the last digit in either case. Zero padding helps in binary, where a fixed width shows which bit is which; in hexadecimal it changes nothing. Type the short form and trust the write.

Does one MOV clear every step bit and set step zero? #

Yes — MOV replaces the destination rather than OR-ing into it, so a source of 0x1 leaves bit 0 set and every other bit 0, whatever state the sequence was in before the write.

In this worked example the sequence sits with steps 2 and 5 active when the initialization demand arrives:

  • Before: G7_securite = 0x0024. X[2] carries weight 2^2 = 4 and X[5] carries 2^5 = 32; 4 + 32 = 36, and 36 decimal converts to 0x24 because 2 x 16 + 4 = 36.
  • Instruction: MOV with destination G7_securite, source 0x1.
  • After: G7_securite = 1 = 2^0, so X[0] = 1 and X[2] = X[5] = 0.
Field Before After
G7_securite.X[0] — step 0, weight 1 0 1
G7_securite.X[2] — step 2, weight 4 1 0
G7_securite.X[5] — step 5, weight 32 1 0
Every other X[n] 0 0
Whole variable 0x0024 = 36 0x1 = 1

Clearing the same state bit by bit takes one instruction per bit and leaves any bit you forget still active; the single MOV cannot forget one. The arithmetic assumes the declared width covers every bit you index — variables of this kind appear in projects declared both as 32-bit integers and as 16-bit INT, and the declaration, not the name, sets which X[n] exist.

If this variable sequences the machine's response to a stop demand, keep the stopping function itself in the hardwired chain: this logic orders the sequence, it does not provide the safety function.

What do the .X[0], .B[0] and .W[0] suffixes write? #

A field of the declared variable, not the whole variable: VariableName.X[n] addresses one bit, VariableName.B[n] one byte and VariableName.W[n] one word, so initialization uses the bare variable name and transitions use .X[n].

The editor's help documents the three views exactly that way: VariableName.X[0] for a bit, VariableName.B[0] for a byte, VariableName.W[0] for a word. Test steps in transitions through .X[n]; initialize through the bare name.

Do not substitute a field write for initialization. A write scoped to B[0] or W[0] touches that field only, and field writes have been observed leaving the integer at 256 or 65536 — numbers that say the value landed one byte or one word away from the position assumed. Treat them as diagnostics and settle the question against the declaration and the online value before you rely on field access. The caution extends across address types: the bit at address 0000 and the word at address 0000 in the memory-link area are not documented as one shared location, so prove any alias with an online write before depending on it.

How do I stop the sequence skipping steps inside one scan? #

Evaluate every transition against a copy of the state taken at scan start — G7_securite_M here — and write the new state back once, so the sequence advances at most one step per scan, which is what a GRAFCET transition demands.

The failure mode: transition rungs that read and write the live variable let a step activated early in the scan satisfy another transition later in the same scan, and the sequence passes through an intermediate step too fast for diagnostics to catch. Run the scan this way:

  1. At the top of the logic, copy G7_securite into G7_securite_M, the N-1 image.
  2. Evaluate every transition from G7_securite_M, never from the live variable.
  3. Assemble the next state from those evaluations alone.
  4. Commit it to G7_securite once, at the end of the logic.
  5. On an initialization demand, run MOV 0x1 at that commit point and inhibit transition evaluation for the scan, or a later rung overwrites the fresh state in the same pass.

An SFC editor enforces this one-step-per-scan behavior for you; in ladder you supply it yourself.

Packed integer, step number or Boolean array: which should I use? #

Keep the packed integer if you are carrying an existing bit-per-step design across: it initializes in one MOV, gives every step a testable X[n], and puts the complete state on the screen as a single number.

Approach Initialization Step access Diagnostics Limit Best fit
Packed integer bit field MOV 0x1 to the whole variable X[n] per step Whole state as one number; parallel branches show as multiple set bits Stray bit writes leave extra steps active unless transitions evaluate the snapshot Parallel branches; existing bit-per-step designs
Integer step number MOV the initial step number Equality comparison One directly readable number Holds one active step; no simultaneous branches Single-sequence machines with a short HMI trace
Boolean array BLMV of a prepared array with the first element set Direct element per step One tag per step More tags, more display configuration Programs already built on step flags

For the job in question — clear the state and activate step zero without rewriting transition logic — the packed integer with MOV 0x1 is the choice; the step-number form means re-authoring every transition as a comparison, and the Boolean array pays off only where the program is already built on step flags. For tracking on the panel, a numeric display of the packed integer plus one lamp per X[n] shows where the sequence sits, and parallel branches show as two set bits at once. When a number on screen must read in engineering units rather than as a raw state word, see how to scale a PLC integer into an HMI display value.

How do I reach drive parameters that are missing from the PDO mapping? #

Read and write them with SDO transfers — the SDOR and SDOW blocks — addressing each parameter by the object index and sub-index from the drive's object dictionary for the selected operating mode.

In the reported setup, STATUSWORD and CONTROLWORD are mapped on an LXM32M servo drive while acceleration, deceleration and the speed setpoint are not: the configured process-data exchange carries only what the mapping lists, so mapping two objects proves nothing about the rest of the dictionary.

Criterion Process data (PDO) SDO with SDOR and SDOW
Transfer model Cyclic or event-driven, no request per value Explicit request and response
Program use Values consumed every scan Setup, commissioning, recipe changes
Addressing Configured mapping Object index plus sub-index
Completion handling Watch bus state and process-data validity Watch the busy flag and result of the instruction

The decision: a speed command or target position the logic writes during the cycle belongs in process data, mapped for the drive mode in use — a cyclic command should not ride a chain of serviced requests. Acceleration and deceleration written at setup or per recipe go by SDOR and SDOW. Take each object's index, sub-index, access direction, width, signedness and unit scaling from the object dictionary for the selected operating mode; a screen label and an object index are not interchangeable, and neither is guessed from a name. Before debugging any object, confirm the bus itself — wiring, termination, node configuration and state — because a network in error state is not repaired from the ladder.

Questions asked

My variable is declared 16-bit, not 32-bit - does the initialization change?

No. 0x1 produces the same low-bit pattern in a 16-bit integer as in a 32-bit one, so the MOV block, the constant and the activation of step zero are identical. What changes is the valid index range, X[0] through X[15] instead of X[0] through X[31], and the signed range, -32768 to 32767 against -2147483648 to 2147483647. Keep the highest numbered step inside the declared width.

Why did a write through B[0] leave my integer at 256?

Decode it as position, not damage. 256 is 2^8, the value 1 shifted one byte over; 65536 is 2^16, one word over. The write reached a field other than the one assumed, which is why field writes are the wrong tool for initialization. Write 0x1 to the bare variable name and confirm the online value reads 1 before using field access again.

How do I show the active step on the HMI screen?

Put a numeric display on the packed integer and one lamp object per step, each lamp addressed to VariableName.X[n]. The number gives the whole state at a glance, including two set bits when parallel branches are both active, and the lamps name the step without decoding hexadecimal on the floor. Leave the state word unscaled; scaling belongs to process values.

Can one MOV initialize all of my GRAFCET variables?

No. One MOV block writes one destination, so give each state variable its own block triggered by the same initialization demand: G7_securite, G7_conduite and every G7_TacheX each take MOV 0x1 in the same scan, ahead of transition evaluation. If your steps were stored as a Boolean array instead, one block move of a prepared array with the first element set does the same job.

Does initializing the GRAFCET variable touch the CANopen network?

No. The MOV writes an internal logic variable and generates no CANopen frame; the drive exchange is a separate path. Before the first cycle, take the drive through its own enable sequence through CONTROLWORD, and confirm wiring, termination and node configuration first - a bus in error state cannot be repaired by changing ladder instructions or object access.

Keep reading

  • How-to

    How to Display Text Labels From a Sensor Range in CODESYS

    Classify the reading in the controller with an IF/ELSIF chain that assigns a STRING label to each sensor range, then point the textbox's text variable at that string instead of the numeric tag. The number becomes a category in the PLC, not on the screen.

    PLCs and controllers

  • Troubleshooting

    Why an HMI counter display jumps from 9 to 16

    The counter stores each count in BCD and the HMI tags are typed as integers, so the word for 10 reads as 16 and a preset of 25 arms the counter at 19. Set both the preset-entry tag and the accumulator-display tag to BCD int 16, keep the addresses, and use BIN only when ladder logic needs binary.

    PLCs and controllers

  • Explainer

    Can an HMI Script Count PLC Bit Changes Reliably

    Yes, but only as an approximate, non-critical count. A runtime script sees the tag at discrete reads, so a bit that turns on and off between reads is missed; reported field experience treats signals held 2 s per state as reliable and anything below 500 ms as unusable. Any count that matters belongs in the PLC, displayed on the HMI.

    HMI and touchscreens