Retaining S7-1200 HSC Count Value Through Power Failure
Field engineers using the S7-1200 high-speed counter (HSC) for linear scales, rotary encoders, or flow meters routinely hit one recurring issue: after a power outage, STOP-to-RUN transition, or warm restart, the counter returns to zero. The HSC instance is created, the firmware initializes the input address, and the accumulated position is gone. The fix is to add a retentive global DB as a buffered copy, write the running count into it on every scan, and push that value back to the HSC during the startup OB before the first user program scan touches the counter. This article walks through the exact TIA Portal V11-V18 procedure, the wiring constraints, the CTRL_HSC and High_Speed_Counter instruction parameters, the verification checks, and the field caveats that come from deployed installations.
1. Problem Definition: Why the Counter Resets
The S7-1200 HSC is implemented in the CPU firmware and is not a software counter. The current count value is mirrored into a fixed input (I) address inside the process image. The default allocation, per the TIA Portal online help, places HSC1 at ID1000, HSC2 at ID1002, HSC3 at ID1004, and continues in 4-byte increments up to the maximum number of HSC supported by the CPU. The Siemens documentation states: "The CPU stores the current value of each HSC in an input (I) address."
That I address lives in the process-image input area, which is not backed up by the retentive memory. The HSC instance DB that TIA Portal generates automatically is retentive in some configurations, but the running counter value exposed through the I address is rebuilt from zero on every CPU restart.
Concretely, three conditions wipe the counter:
- Power-off / power-on (cold restart).
- STOP-to-RUN transition without power cycle; the HSC is reinitialised when the CPU enters RUN.
- Reinitialisation of the HSC object in the device configuration, or download of a new hardware configuration.
Marking the HSC instance DB as retentive alone does not preserve the value, because the instance DB only holds configuration metadata, not the live count. The misconception that "HSC retain = automatic backup" comes from confusing the instance DB (configuration) with the I address (live data).
2. HSC Address Map and CPU-Dependent Limits
The default addresses for the current value of each HSC are allocated as follows. They can be changed in the device configuration under "HSC properties" if the I area conflicts with another module.
| HSC Channel | Default I Address (Current Value) | Typical CPU Availability |
|---|---|---|
| HSC1 | ID1000 | CPU 1211C / 1212C / 1214C / 1215C / 1217C |
| HSC2 | ID1002 | CPU 1212C / 1214C / 1215C / 1217C |
| HSC3 | ID1004 | CPU 1211C / 1214C / 1215C / 1217C |
| HSC4 | ID1006 | CPU 1214C / 1215C / 1217C |
| HSC5 | ID1008 | CPU 1214C / 1215C / 1217C |
| HSC6 | ID1010 | CPU 1214C / 1215C / 1217C |
Maximum input frequency and supported counting modes vary by CPU and by whether the input is an onboard terminal or a signal board (SB) channel. Single-phase maximum is typically 100 kHz on most S7-1200 CPUs, up to 1 MHz on the CPU 1217C onboard inputs, with A/B quadrature rated at 80 percent of the single-phase figure. Refer to the CPU-specific data sheet for the exact rating before sizing the encoder. Counting modes supported by all HSC channels include:
- Single-phase count with internal direction control - one pulse input, direction from program.
- Single-phase count with external direction control - one pulse input, direction from a digital input terminal.
- Two-phase count - two pulse inputs (A and B) where phase relationship encodes direction.
- A/B counter (quadrature) - two pulse inputs with optional Z (zero) reference. Single, double, or quadruple evaluation.
- Frequency measurement - the HSC counts pulses over the period set on the CTRL_HSC PERIOD input.
The control instruction that reads and writes the HSC depends on TIA Portal version:
- CTRL_HSC (legacy, V11 - V18). Compact block, fixed parameter list, supports single and two-phase counting, frequency measurement, and direct write of current and reference values. Documented in the TIA Portal online help under "Legacy CTRL_HSC".
- High_Speed_Counter (V14+). Newer instruction with the same functional role, used in TIA Portal V14 and later projects. Both can coexist in the same project; CTRL_HSC remains supported for backward compatibility.
3. Prerequisites
Before implementing the retain pattern, confirm the following:
- Hardware: S7-1200 CPU with at least one HSC channel wired. Verify the encoder output type (push-pull, totem-pole, 24 V single-ended, or 5 V line driver) matches the input spec; 24 V single-ended encoders need the appropriate digital input filter setting and may require a signal board for the differential HSC inputs on some CPUs.
- Software: TIA Portal V11 SP2 or later. The instructions and configuration paths in this article are valid for V13 SP1, V14, V15, V15.1, V16, V17, and V18. Function blocks written against CTRL_HSC in V11 will compile unchanged in V18.
- Project state: The HSC is enabled in the CPU device configuration. The I address, counting mode, initial value, gate function, and reset behaviour are set. The wiring to the encoder must be in place before commissioning the retain logic; debugging the retain pattern with a floating input wastes hours.
- Retentive memory budget: Each CPU has a fixed amount of bit memory, counters, timers, and DB data that can be marked retentive. The retain budget for a CPU 1214C, for example, is 14 kB. A single DINT for the buffered count uses 4 bytes - well within budget - but multiple HSC channels, recipes, and operator-settable parameters all draw from the same pool. Open the CPU properties, navigate to "Retentive memory", and confirm the projected total is below the CPU limit.
4. Architecture: Buffered Retain Pattern
The retained design has three layers:
- Live HSC count held in the I address (IDW). Updated by firmware in real time, lost on restart.
- Retentive shadow DB ("HSC_Retain") in the load memory and work memory with the Retain attribute set. Survives power-off / power-on and STOP-to-RUN.
- Synchronisation logic in OB1 (write shadow from live) and OB100 (write live from shadow at startup).
On every OB1 cycle, the live count is copied into the shadow DB. On the very first scan after the CPU enters RUN, OB100 pushes the retained shadow value back into the HSC by setting the NEW_CV parameter of CTRL_HSC (or the equivalent input on High_Speed_Counter) and pulsing the NewCV bit. After that, the live count diverges from the shadow as new pulses arrive, and the loop continues. The pattern is sometimes called "save on cycle, load on startup" and is the same architecture used for retentive timers, position counters, and recipe parameter sets.
5. Step-by-Step Implementation
5.1 Create a Retentive Global DB
From the project tree, add a new global DB named "HSC_Retain" with the following structure:
| Name | Data Type | Initial Value | Retain |
|---|---|---|---|
| LastCount | DINT | 0 | Retain |
| LastCountValid | BOOL | FALSE | Retain |
| FirstRunDone | BOOL | FALSE | Retain |
| BootTime | DTL | DTL#1970-01-01-00:00:00 | Retain |
Set the DB's overall retain attribute. In TIA Portal V13 SP1 and later, right-click the DB node, select "Properties", and in the "Attributes" section set "Retain" to "Retain" (S7-1200) or "Memory of retain" (S7-1500, preserved across online download). Per-tag retain is also set in the declaration table's "Retain" column. The "Memory of retain" option for S7-1500 keeps the value across online changes of the program, which is convenient for commissioning but only available on S7-1500. For S7-1200, only the standard "Retain" option is available; an online download that changes the program will reset the retain area unless the user selects "Preserve retentive data" in the download dialog.
5.2 Configure the HSC in Device Configuration
Open the CPU in the device view, select the HSC channel from the navigation tree, and configure:
- Enable: ticked.
- Type: Single phase, two phase, or A/B counter, matching the encoder.
- Count direction: user program controlled, hardware controlled, or both.
- Initial value: 0 (will be overwritten in startup).
- Initial reference value: 0 unless using compare/preset.
- I address: keep the default or remap to avoid conflict with other modules.
- Input filter: minimum supported value for the filter group the HSC terminals belong to.
- Gate function: software gate (controlled by program) or hardware gate (controlled by a digital input). The gate stops counting while inactive; it does not reset the count.
5.3 Insert the HSC Control Instruction
Place a CTRL_HSC block (or High_Speed_Counter for V14+) in OB1. Wire the HSC hardware identifier. The HW_ID is found in the HSC's properties and is typically formatted as "HSC_1" or "CTRL_HSC_1".
CTRL_HSC input parameters relevant to the retain pattern:
| Input | Type | Purpose |
|---|---|---|
| HSC | HW_IO | Hardware identifier of the HSC |
| DIR | BOOL | Count direction (1 = up, 0 = down) |
| CV | DINT | New current value to load into the HSC |
| RV | DINT | New reference value to load into the HSC |
| PERIOD | INT | Period (in milliseconds) for frequency measurement mode |
| NewDIR | BOOL | Rising edge: write DIR to HSC |
| NewCV | BOOL | Rising edge: write CV to HSC |
| NewRV | BOOL | Rising edge: write RV to HSC |
| NewPeriod | BOOL | Rising edge: write PERIOD to HSC |
CTRL_HSC output parameters:
| Output | Type | Purpose |
|---|---|---|
| CV | DINT | Current value of the HSC |
| RV | DINT | Reference value of the HSC |
| STATUS | BYTE | Status byte for diagnostics |
5.4 Save the Live Count into the Shadow DB
Add the following network in OB1, after the CTRL_HSC call so the current value is fresh:
// OB1 - network: shadow DB update
"HSC_Retain".LastCount := "HSC_Count".CV; // or use the I address: %ID1000
"HSC_Retain".FirstRunDone := TRUE;
IF NOT "HSC_Retain".LastCountValid THEN
"HSC_Retain".LastCountValid := TRUE;
END_IF;
The CPU copies the live count from the I address into the retentive DINT on every cycle. After a power cycle, the DB's load memory image is restored, and the last value before the outage is what OB100 will see. The LastCountValid flag is optional but useful: it lets OB100 distinguish a freshly commissioned PLC (where LastCount is 0 because the DB was just initialised) from a PLC that was running and lost power (where LastCount holds a real position).
5.5 Restore the Count in OB100 (Startup)
OB100 executes once after the CPU transitions from STOP to RUN (warm restart). Place the load logic here so the value is in place before OB1's first scan. The HSC is also reinitialised at this point, so a write to NEW_CV is recognised:
// OB100 - network 1: load retained count into HSC
IF "HSC_Retain".LastCountValid THEN
"HSC_Count".CV := "HSC_Retain".LastCount;
"HSC_Count".NewCV := TRUE;
ELSE
"HSC_Count".CV := 0;
"HSC_Count".NewCV := TRUE;
END_IF;
// OB100 - network 2: log boot time for diagnostics
"HSC_Retain".BootTime := RD_SYS_T;
On the rising edge of NewCV in the first call of CTRL_HSC inside OB100, the CPU writes the CV value into the HSC. Subsequent calls in OB1 must not retrigger the write, because NewCV must see a 0-to-1 transition to act; ensure the network in OB1 does not continuously drive NewCV to 1, otherwise the HSC will reset to the buffered value on every cycle and incoming pulses will be lost.
6. Complete SCL Example (TIA V14+)
The following SCL block can be dropped into any S7-1200 project. Replace the hardware identifier and addresses for your configuration. The block encapsulates the retain logic and can be reused across projects.
FUNCTION_BLOCK "HSC_Retain_Handler"
VAR
HSC_CV : DINT; // live count read from %ID1000 each cycle
HSC_Shadow : DINT; // retentive shadow stored in instance DB
HSC_Loaded : BOOL; // retentive flag: load on next OB100
HSC_LoadPending : BOOL; // volatile flag: edge to NewCV
END_VAR
BEGIN
// 1. read live count from the HSC I area
#HSC_CV := "HSC_Default".%ID1000; // default address, HSC1
// 2. mirror to the retentive shadow every cycle
#HSC_Shadow := #HSC_CV;
#HSC_Loaded := TRUE;
// 3. produce a one-shot NewCV on the cycle following OB100
IF #HSC_LoadPending THEN
"HSC_Count".CV := #HSC_Shadow;
"HSC_Count".NewCV := TRUE;
#HSC_LoadPending := FALSE;
END_IF;
END_FUNCTION_BLOCK
For the startup load, drop this network in OB100:
// OB100 - executed once on warm restart
"HSC_Retain_Handler".HSC_LoadPending := "HSC_Retain_Handler".HSC_Loaded;
"HSC_Retain_DB".BootTime := RD_SYS_T;
Mark HSC_Shadow and HSC_Loaded as Retain. On every STOP-to-RUN, OB100 fires once, sets HSC_LoadPending, and OB1 pushes the retained value into the HSC on the next cycle. The NewCV pulse is exactly one OB1 cycle wide, so the HSC is loaded once and never overwritten.
7. Counting Mode Details
Each counting mode has a specific implication for retention:
| Mode | Input Wiring | Direction | Retention Caveat |
|---|---|---|---|
| Single-phase, internal direction | 1 pulse input | From program (DIR input) | DIR state at restart must be set explicitly in OB100; otherwise the HSC may count the wrong way. |
| Single-phase, external direction | 1 pulse + 1 direction input | From DI terminal | If the direction input is forced to a fixed level at restart, the count resumes correctly. Verify the direction terminal assignment in device config. |
| Two-phase | 2 pulse inputs (A, B) | From phase relationship | Direction is implicit; only the count needs retention. |
| A/B quadrature | 2 pulse inputs (A, B), optional Z | From phase relationship | Quadrature evaluation (1x, 2x, 4x) is fixed in device config and does not need to be loaded at startup. |
| Frequency measurement | 1 pulse input | n/a | Frequency values are not retained; only the period parameter and any preset count are. |
For the Z (zero) reference input on A/B counters, the HSC resets the count to 0 on each pulse of Z. Retention across power-off does not affect Z behaviour, but the post-restart Z pulse will reset the retained count, which is usually what the application wants (homing on power-up).
8. Gate and Reset Behaviour
The HSC has a software gate and, on some configurations, a hardware gate. The gate stops the HSC from counting while inactive; it does not reset the count. Typical patterns:
- Software gate only - the gate is controlled by a program tag. The HSC is enabled while the gate is TRUE. On power cycle, the gate is reinitialised to its default (TRUE unless changed in device config), so the HSC starts counting immediately after OB100 pushes the retained count.
- Hardware gate - a digital input terminal gates the HSC externally. On power cycle, the input must be re-asserted by the field wiring. If the gate is open at startup, the HSC holds the retained value but does not count new pulses. Confirm the gate wiring at commissioning.
Reset behaviour is configured per HSC: an "Initial counter value" reset occurs on a digital input configured as the reset terminal, on a software command, or on the Z pulse of an A/B encoder. The retain pattern does not interfere with the reset; the count is reset to the configured initial value, and OB100 does not push the retained count because the reset has higher priority. If the application must preserve position across an intentional reset, do not use the HSC reset; instead, manage the count in a separate DB and use the HSC purely for pulse counting.
9. Reference Value, Overflow, and Wrap
The HSC reference value (RV) is the threshold for the CV = RV event, which can trigger a hardware interrupt or set a status bit. The reference value is not retained by default; if the application needs a persistent reference, add a DINT in HSC_Retain and load it via the NewRV input on CTRL_HSC in OB100.
Counting range: DINT is signed, covering –2,147,483,648 to 2,147,483,647. A quadrature encoder at 4x evaluation counts 4 per line per revolution. At 10,000 lines per revolution, 4x evaluation, 1,000 RPM, the HSC reaches 4 * 10,000 * 1000 / 60 = 666,666 counts per second. To wrap the full DINT range, the encoder would need to run for about 3,580 seconds (nearly an hour) at that speed, which is not unusual in continuous process machinery. The retention pattern preserves the wrapped value; the application logic must handle the wrap if the range matters. A common technique is to keep a BOOL "Overflowed" in HSC_Retain, set it in OB1 if a wrap is detected, and reset it on homing.
10. TIA Portal Version Compatibility
| Portal Version | HSC Instruction | DB Retain Setting | Notes |
|---|---|---|---|
| V11 SP2 | CTRL_HSC | Retain checkbox in DB properties | Original release for S7-1200; retain pattern as described. |
| V12 | CTRL_HSC | Retain checkbox | Same behaviour, minor UI changes in the device configuration. |
| V13 / V13 SP1 | CTRL_HSC | Retain column in declaration | Per-tag retain introduced in the DB declaration table. |
| V14 | CTRL_HSC, High_Speed_Counter | Retain column, Memory of retain (S7-1500) | New High_Speed_Counter instruction introduced for new projects. |
| V15 / V15.1 | CTRL_HSC, High_Speed_Counter | Same as V14 | High_Speed_Counter extended with additional modes. |
| V16 / V17 / V18 | CTRL_HSC, High_Speed_Counter | Same as V14 | Both instructions remain supported; new S7-1200 G2 CPUs supported in V18. |
For new projects on V14+, Siemens recommends High_Speed_Counter. For migration of existing V11-V13 projects, keep CTRL_HSC and the same code will compile and run unchanged. The retain pattern described in this article is identical regardless of the instruction, because the retain logic lives in the application code, not in the instruction.
11. Verification Procedure
- Online watch: Connect to the CPU, open the HSC_Retain DB, and watch LastCount increment as the encoder turns. Confirm the value updates at the OB1 cycle rate.
- Power cycle test 1 (no motion): With the encoder stationary, stop the CPU, switch off the 24 V supply for 10 seconds, restore power, and confirm OB100 sets the HSC to the buffered value. The I address should display the pre-outage count. If it displays 0, OB100 is not executing or the HW_ID is wrong.
- Power cycle test 2 (motion during outage): Mark a known count, switch off, rotate the encoder by N pulses manually, switch back on, and confirm the count has increased by N. If the count is unchanged, the shadow DB is not being updated each cycle, or the retain attribute is missing on the DB tag.
- STOP-to-RUN test: Toggle the CPU from RUN to STOP and back without removing power. Confirm the same restore behaviour. This isolates the warm restart path; if it fails but the power cycle test passes, the STOP-to-RUN handler is the issue.
- Cold restart test: Perform a memory reset (MRES) on the CPU, reload the project, and confirm LastCount starts at 0 (because LastCountValid is also reset). The HSC should display 0 on the first scan, not the pre-reset value.
- Diagnostic buffer check: Open Online > Diagnostics > Diagnostic buffer and look for HSC events (configuration change, error, gate state change) during the test cycles. Filter by "HSC" to reduce noise.
- Retain budget verification: Open the CPU properties, navigate to "Retentive memory", and verify total usage is well below the CPU limit. A CPU 1214C allows 14 kB; the HSC backup is 4 bytes, leaving plenty of room.
- Download behaviour test: With the HSC at a known value, perform an online download of a trivial program change and observe whether the retained count is preserved (CPU prompt "Preserve retentive data" must be confirmed). If the count resets, the retain attribute is missing or the download replaced the retain memory image.
12. Cross-Platform Comparison: ControlLogix 1756 HSC
Engineers porting S7-1200 designs to ControlLogix (or maintaining both) will note the conceptual difference. The Allen-Bradley 1756 HSC module, per Rockwell Automation publication 1756-UM007, retains the Stored Value internally across power cycles. The module's "Store and Reset" and "Start" commands write the present value to a non-volatile tag, and the stored value is preserved "until it is overwritten by new data from the next leading edge of a pulse on Input Z."
This means the 1756 module implements the retain pattern at the module level; the CPU program does not need a backup DB. The S7-1200 architecture leaves the retain responsibility to the user program because the HSC lives in the CPU firmware and exposes only the I-address mirror. When migrating a retain pattern from Logix to S7-1200, the equivalent is exactly the buffered-DB approach described in section 4.
| Feature | S7-1200 HSC | 1756 HSC (ControlLogix) |
|---|---|---|
| Retain on power-off | Not automatic; user must shadow into retentive DB | Stored value retained in module non-volatile memory |
| Reset mechanism | Reinitialise HSC, or write NEW_CV via CTRL_HSC | Store and Reset command on the module |
| Count exposed to program | I address (e.g., %ID1000) and CV output of CTRL_HSC | Present Value tag in module-defined tag set |
| Reference value | RV input on CTRL_HSC | Configurable in module properties |
| Program burden | Backup DB + startup OB | None for retain; only for application logic |
| Maximum frequency | 100 kHz typical, 1 MHz on 1217C | Up to 1 MHz depending on module catalog |
The trade-off is module-level integration (1756) versus CPU-level integration (S7-1200). For applications that mix platforms or migrate from Logix, the retain pattern is a familiar engineering exercise rather than a hardware feature.
13. Diagnostic Interrupts and STATUS Byte
The CTRL_HSC STATUS output reports HSC state. A non-zero STATUS indicates an error or warning. Common values documented in the TIA Portal help include:
- STATUS = 0: no error.
- STATUS <> 0: error; refer to the TIA Portal online help for the specific code (typically a configuration or wiring fault).
For HSC-related diagnostic interrupts, enable "Hardware interrupt" in the HSC's device configuration and route the interrupt to an OB (OB40 on S7-1200). The interrupt can be triggered on CV = RV, on direction change, on the gate opening or closing, or on the Z pulse of an A/B encoder. Use the interrupt OB to update the shadow DB immediately, rather than waiting for the next OB1 cycle, if the application has tight timing requirements.
14. Troubleshooting Matrix
| Symptom | Likely Root Cause | Remediation |
|---|---|---|
| Count is 0 after power-on, encoder has not moved | OB100 not executing, or NewCV not pulsing | Confirm OB100 is in the program blocks; add a one-shot on NewCV; check the HW_ID of the CTRL_HSC instance. |
| Count is 0 after power-on, encoder has moved | Shadow DB not updated each cycle, or retain attribute missing | Verify OB1 copies the I address into the retentive DINT every cycle; check the DB's Retain column or the DB properties "Retain" attribute. |
| Count resets to a fixed value, then climbs again | NewCV is held high in OB1, overwriting the live count | Use a one-shot edge on NewCV; do not tie it directly to the OB1 first-scan bit. Inspect the network driving NewCV in online mode. |
| Count is correct at power-on but drifts on every STOP | OB1 not updating the shadow before the STOP, or STOP drops retain | Verify the OB1 network runs every cycle, not inside a conditional block; confirm the CPU's retain attribute is set in properties. |
| Diagnostic buffer: "HSC configuration error" | Encoder wiring, filter setting, or terminal assignment | Check wiring against the CPU data sheet; set input filter to minimum; confirm terminal is assigned to the HSC in device config. |
| Retain value resets after program download | "Memory of retain" not used (S7-1500 only) or "Preserve retain" not selected | For S7-1500, set retain to "Memory of retain"; otherwise download with PLC in STOP and accept the reset, or enable "Preserve retentive data" in the download dialog. |
| Online: retain symbol shows dash | Retain attribute not set on the variable | Right-click the tag in the DB, choose "Set retain bit" or set via the Retain column in the declaration table. |
| Count is wrong but the encoder is good | HSC initial value or reference value never loaded | Confirm OB100 sets CV via CTRL_HSC; check the hardware identifier matches the configured HSC; check the CV input wiring (CV, not RV). |
| Count is 0 after restart, then counts normally | OB100 not loaded; the first cycle of OB1 sees count = 0 and writes 0 to the shadow | Ensure OB100 fires before OB1; if the project has multiple startup OBs (OB100, OB101), confirm the correct one is used. |
| Retain works in V13 but not V18 project | High_Speed_Counter used instead of CTRL_HSC; parameter names differ | Either switch to CTRL_HSC, or update the NewCV wiring to match the High_Speed_Counter parameter list (different instance DB structure). |
| Direction reversed after power-on | DIR input not restored in OB100, or external direction input latched | Set DIR explicitly in OB100 from a retentive flag; confirm the direction input wiring and filter group. |
15. Field-Proven Caveats
- First-scan order matters. If OB1 runs before OB100 has pushed the retained value, the very first cycle will copy 0 into the shadow and wipe the backup. This is a common bug in projects that have not used OB100. The Siemens execution model is OB100, then OB1, then cyclic; verify the project does not override the startup OB or insert an OB with a higher priority that runs first.
- HSC instance DB and shadow DB are different. The instance DB is created when you place the CTRL_HSC block and holds configuration constants. The shadow DB is a separate global DB the user creates for retention. Do not mark the instance DB as retain and expect it to solve the problem; the instance DB does not hold the live count.
- Multi-channel retention. For encoders on HSC1, HSC3, and HSC5, use three distinct DINTs in the same shadow DB. The retain setting is per-tag, not per-DB, so partial retention is possible. A single DINT per channel, plus a DTL timestamp for the last write, is a clean layout.
- Sign of CV. DINT is signed (–2,147,483,648 to 2,147,483,647). A count of half-revolution plus one of an A/B encoder can roll over. The retention pattern preserves the wrapped value; the application logic must handle the wrap if the range matters. For 32-bit unsigned counting, the HSC instruction output is still a DINT, so the upper range above 2,147,483,647 wraps to negative; convert at the application level if the application expects unsigned.
- Download behaviour. On S7-1200, a download that includes a new hardware configuration or new program resets the retain area by default. Use "Download to device" with the option to "Preserve retain data" where available; otherwise accept the reset during commissioning. For S7-1500, "Memory of retain" survives the download.
- Direction signal handling. If DIR is hardware-controlled (e.g., from a digital input), a power outage that flips the direction bit at restart can produce a different count than expected. Document the direction logic in the project comment and consider latching the direction in a retentive flag in OB100.
- Filter group on shared inputs. The S7-1200 applies the input filter at the filter-group level, not per-terminal. If the HSC is wired to terminals I0.0 and I0.1, and I0.2 through I0.7 are in the same filter group, the slower filter for the standard DI applies to the HSC terminals. Place the HSC terminals on a filter group by themselves, or accept the slower filter and reduce the encoder frequency accordingly.
- Signal board HSC limits. HSC channels on the signal board (SB) have lower maximum frequencies than the onboard HSC channels. The exact limit depends on the SB catalog number and the CPU; consult the SB data sheet. If the application requires > 100 kHz, use the onboard HSC channels, not the SB.
- Diagnostic buffer noise. During normal operation, the diagnostic buffer may log HSC gate state changes, especially if the gate is toggled frequently. Filter the diagnostic buffer view to "HSC" and "configuration change" to reduce noise; the diagnostic buffer is a ring buffer and old entries are overwritten.
16. Commissioning Checklist
- Wire encoder to the HSC terminals per the CPU data sheet. Verify supply voltage and ground reference.
- Configure HSC in device configuration: enable, mode, I address, filter, gate, initial value.
- Create HSC_Retain global DB with LastCount, LastCountValid, FirstRunDone, BootTime, all marked Retain.
- Insert CTRL_HSC in OB1; wire HW_ID. Verify online that CV tracks the encoder.
- Add shadow write network in OB1: HSC_Retain.LastCount := CTRL_HSC.CV.
- Add load network in OB100: write LastCount to CV, pulse NewCV via HSC_LoadPending one-shot.
- Compile, download to PLC. Accept any retain reset on first download.
- Run the eight verification steps from section 11. Document the result for the maintenance log.
- If the CPU is in a high-vibration or high-EMI environment, add input debounce or shielded cable to the encoder wiring; the HSC will not filter electrical noise that the encoder injects into the input.
- Archive the project and the verification log to the machine documentation folder. Future modifications that re-download the program will reset retain unless the operator confirms "Preserve retentive data".
17. FAQ
Why does my S7-1200 HSC value reset to zero on every power cycle even with a retentive DB?
The HSC current value is stored in a fixed input (I) address (default ID1000-ID1010) inside the process image, which is not part of the retentive memory area. The HSC instance DB does not hold the live count. Create a separate retentive global DB, copy the count into it every OB1 cycle, and push the value back via CTRL_HSC in OB100 using a one-shot on the NewCV input.
What is the difference between CTRL_HSC and High_Speed_Counter in TIA Portal?
CTRL_HSC is the legacy instruction (V11 and later). High_Speed_Counter was introduced in V14 with more counting and measurement modes. Both remain supported; Siemens recommends High_Speed_Counter for new V14+ projects and CTRL_HSC for migration of V11-V13 code. The retain pattern described in this article works with both, because the retain logic lives in the application code.
Can I use OB1 alone, without OB100, to retain the count?
No. OB1 runs after the HSC has already been initialised to zero. You need a startup OB (OB100 for warm restart) to push the retained value into the HSC before OB1 first reads it. Mark the load bit (NewCV) as a one-shot to avoid overwriting the live count every cycle, otherwise pulses will be lost.
How much retentive memory does the buffered DINT use?
A single DINT uses 4 bytes. The retain budget for a CPU 1214C is 14 kB; for a CPU 1215C/1217C it is 10 kB. The HSC backup is well within budget; total retain usage is shown in the CPU properties under "Retentive memory" and should remain below the CPU limit across all retentive tags, timers, and counters.
Does the ControlLogix 1756 HSC module handle retain automatically?
Yes. Per Rockwell publication 1756-UM007, the 1756 HSC retains the Stored Value in non-volatile memory and overwrites it only on the next pulse on Input Z or on a Store/Reset command. The S7-1200 HSC does not provide this module-level retain; the user program must implement the buffered pattern with a retentive DB and OB100, as described in this article.
What input filter should I set for a 100 kHz encoder on the CPU 1214C?
The default input filter on the 1214C HSC terminals is typically 6 µs or higher. At 100 kHz, a 6 µs filter passes the pulses, but the pulse width must be at least 5 µs. For quadrature at 4x evaluation (so 400,000 counts/s for a 100 kHz A/B encoder), the filter must be below 2.5 µs. Set the filter to the lowest value supported by the filter group the HSC terminals belong to, and isolate the HSC terminals from standard DI filter groups.
Will the retain value survive an online program download?
On S7-1200, only if the download dialog option "Preserve retentive data" is selected, and only if the new program does not change the structure of the retentive DB. On S7-1500, marking tags as "Memory of retain" preserves the value across online download, even if the program structure changes. For S7-1200 projects, perform a controlled download in STOP mode and accept the reset, or plan the retain tags to minimise the impact of a download.