9
Technology instructions
9.1
Table 9- 1
High-speed counter
CTRL_HSC instruction
LAD / FBD
SCL
"CTRL_HSC_0_DB" (
hsc:=_hw_hsc_in_,
dir:=_bool_in_,
cv:=_bool_in_,
rv:=_bool_in_,
period:=_bool_in_,
new_dir:=_int_in_,
new_cv:=_int_in_,
new_rv:=_dint_in_,
new_period:=_int_in_,
busy:=_bool_out_,
status:=_word_out_);
Description
Each CTRL_HSC instruction uses a structure stored in
a DB to maintain data. You assign the DB when the
CTRL_HSC instruction is placed in the editor.
STEP 7 automatically creates the DB when you insert the instruction.
In the SCL example, "CTRL_HSC_0_DB" is the name of the instance DB.
Table 9- 2
Data types for the parameters
Parameter and type
HSC
Data type
Description
IN
HW_HSC
HSC identifier
DIR1, 2
IN
Bool
1 = Request new direction
CV1
IN
Bool
1 = Request to set new counter value
RV1
IN
Bool
1= Request to set new reference value
PERIOD1
IN
Bool
1 = Request to set new period value
(only for frequency measurement mode)
NEW_DIR
IN
Int
New direction: 1= forward, -1= backward
NEW_CV
IN
DInt
New counter value
NEW_RV
IN
DInt
New reference value
NEW_PERIOD
IN
Int
New period value in seconds: 0.01, 0.1, or 1
(only for frequency measurement mode)
BUSY3
OUT
Bool
Function is busy
STATUS
OUT
Word
Execution condition code
If an update of a parameter value is not requested, then the corresponding input values are ignored.
The DIR parameter is only valid if the configured counting direction is set to "User program (internal direction control)".
You determine how to use this parameter in the HSC device configuration.
For an HSC on the CPU or on the SB, the BUSY parameter always has a value of 0.
S7-1200 Programmable controller
System Manual, 04/2012, A5E02486680-06
337
Technology instructions
9.1 High-speed counter
You configure the parameters for each HSC in the device configuration for the CPU:
counting mode, I/O connections, interrupt assignment, and operation as a high-speed
counter or as a device to measure pulse frequency.
Some of the parameters for the HSC can be modified by your user program to provide
program control of the counting process:
Set the counting direction to a NEW_DIR value
Set the current count value to a NEW_CV value
Set the reference value to a NEW_RV value
Set the period value (for frequency measurement mode) to a NEW_PERIOD value
If the following Boolean flag values are set to 1 when the CTRL_HSC instruction is executed,
the corresponding NEW_xxx value is loaded to the counter. Multiple requests (more than
one flag is set at the same time) are processed in a single execution of the CTRL_HSC
instruction.
DIR = 1 is a request to load a NEW_DIR value, 0 = no change
CV = 1 is a request to load a NEW_CV value, 0 = no change
RV = 1 is a request to load a NEW_RV value, 0 = no change
PERIOD = 1 is a request to load a NEW_PERIOD value, 0 = no change
The CTRL_HSC instruction is typically placed in a hardware interrupt OB that is executed
when the counter hardware interrupt event is triggered. For example, if a CV=RV event
triggers the counter interrupt, then a hardware interrupt OB code block executes the
CTRL_HSC instruction and can change the reference value by loading a NEW_RV value.
The current count value is not available in the CTRL_HSC parameters. The process image
address that stores the current count value is assigned during the hardware configuration of
the high-speed counter. You may use program logic to directly read the count value. The
value returned to your program will be a correct count for the instant in which the counter
was read. The counter will continue to count high-speed events. Therefore, the actual count
value could change before your program completes a process using an old count value.
Condition codes: In the case of an error, ENO is set to 0, and the STATUS output contains a
condition code.
Table 9- 3
STATUS values (W#16#)
STATUS
0
Description
No error
80A1
HSC identifier does not address a HSC
80B1
Illegal value in NEW_DIR
80B2
Illegal value in NEW_CV
80B3
Illegal value in NEW_RV
80B4
Illegal value in NEW_PERIOD
80C0
Multiple access to the high-speed counter
80D0
High-speed counter (HSC) not enabled in CPU hardware configuration
S7-1200 Programmable controller
338
System Manual, 04/2012, A5E02486680-06
Technology instructions
9.1 High-speed counter
9.1.1
Operation of the high-speed counter
The high-speed counter (HSC) counts events that occur faster than the OB execution rate. If
the events to be counted occur within the execution rate of the OB, you can use CTU, CTD,
or CTUD counter instructions. If the events occur faster than the OB execution rate, then use
the HSC. The CTRL_HSC instruction allows your user program to programmatically change
some of the HSC parameters.
For example: You can use the HSC as an input for an incremental shaft encoder. The shaft
encoder provides a specified number of counts per revolution and a reset pulse that occurs
once per revolution. The clock(s) and the reset pulse from the shaft encoder provide the
inputs to the HSC.
The HSC is loaded with the first of several presets, and the outputs are activated for the time
period where the current count is less than the current preset. The HSC provides an interrupt
when the current count is equal to preset, when reset occurs, and also when there is a
direction change.
As each current-count-value-equals-preset-value interrupt event occurs, a new preset is
loaded and the next state for the outputs is set. When the reset interrupt event occurs, the
first preset and the first output states are set, and the cycle is repeated.
Since the interrupts occur at a much lower rate than the counting rate of the HSC, precise
control of high-speed operations can be implemented with relatively minor impact to the scan
cycle of the CPU. The method of interrupt attachment allows each load of a new preset to be
performed in a separate interrupt routine for easy state control. (Alternatively, all interrupt
events can be processed in a single interrupt routine.)
Table 9- 4
Maximum frequency (KHz)
HSC
HSC1
HSC2
Single phase
Two phase and AB quadrature
CPU
100 KHz
80 KHz
High-speed SB
200 KHz
160 KHz
SB
30 KHz
20 KHz
CPU
100 KHz
80 KHz
High-speed SB
200 KHz
160 KHz
SB
30 KHz
20 KHz
HSC3
CPU
100 KHz
80 KHz
HSC4
CPU
30 KHz
20 KHz
HSC5
CPU
30 KHz
20 KHz
High-speed SB
200 KHz
160 KHz
SB
30 KHz
20 KHz
CPU
30 KHz
20 KHz
High-speed SB
200 KHz
160 KHz
SB
30 KHz
20 KHz
HSC6
S7-1200 Programmable controller
System Manual, 04/2012, A5E02486680-06
339
Technology instructions
9.1 High-speed counter
Selecting the functionality for the HSC
All HSCs function the same way for the same counter mode of operation. There are four
basic types of HSC:
Single-phase counter with internal direction control
Single-phase counter with external direction control
Two-phase counter with 2 clock inputs
A/B phase quadrature counter
You can use each HSC type with or without a reset input. When you activate the reset input
(with some restrictions, see the following table), the current value is cleared and held clear
until you deactivate the reset input.
Frequency function: Some HSC modes allow the HSC to be configured (Type of
counting) to report the frequency instead of a current count of pulses. Three different
frequency measuring periods are available: 0.01, 0.1, or 1.0 seconds.
The frequency measuring period determines how often the HSC calculates and reports a
new frequency value. The reported frequency is an average value determined by the total
number of counts in the last measuring period. If the frequency is rapidly changing, the
reported value will be an intermediate between the highest and lowest frequency
occurring during the measuring period. The frequency is always reported in Hertz (pulses
per second) regardless of the frequency-measuring-period setting.
Counter modes and inputs: The following table shows the inputs used for the clock,
direction control, and reset functions associated with the HSC.
The same input cannot be used for two different functions, but any input not being used
by the present mode of its HSC can be used for another purpose. For example, if HSC1
is in a mode that uses built-in inputs but does not use the external reset (I0.3), then I0.3
can be used for edge interrupts or for HSC2.
Table 9- 5
Counting modes for HSC
Type
Input 1
Input 2
Input 3
Function
Single-phase counter with internal
direction control
Clock
(Optional:
direction)
Count or frequency
Reset
Count
Single-phase counter with external
direction control
Clock
Direction
Count or frequency
Reset
Count
Two-phase counter with 2 clock
inputs
Clock up
Count or frequency
Reset
Count
A/B-phase quadrature counter
Phase A
Count or frequency
Reset1
Count
Clock down
Phase B
For an encoder: Phase Z, Home
S7-1200 Programmable controller
340
System Manual, 04/2012, A5E02486680-06
Technology instructions
9.1 High-speed counter
Input addresses for the HSC
Note
The digital I/O points used by high-speed counter devices are assigned during device
configuration. When digital I/O point addresses are assigned to these devices, the values of
the assigned I/O point addresses cannot be modified by the force function in a watch table.
When you configure the CPU, you have the option to enable and configure each HSC. The
CPU automatically assigns the input addresses for each HSC according to its configuration.
(Some of the HSCs allow you to select whether to use either the on-board inputs of the CPU
or the inputs of an SB.)
NOTICE
As shown in the following tables, the default assignments for the optional signals for the
different HSCs overlap. For example, the optional external reset for HSC 1 uses the same
input as one of the inputs for HSC 2.
Always ensure that you have configured your HSCs so that any one input is not being used
by two HSCs.
The following table shows the HSC input assignments for both the on-board I/O of the CPU
1211C and an SB. (If the SB has only 2 inputs, only 4.0 and 4.1 inputs are available.)
For single-phase: C is the Clock input, [d] is the optional direction input, and [R] is an
optional external reset input. (Reset is available only for "Counting" mode.)
For two-phase: CU is the Clock Up input, CD is the Clock Down input, and [R] is an
optional external reset input. (Reset is available only for "Counting" mode.)
For AB-phase quadrature: A is the Clock A input, B is the Clock B input, and [R] is an
optional external reset input. (Reset is available only for "Counting" mode.)
Table 9- 6
HSC input assignments for CPU 1211C
HSC
CPU on-board input (0.x)
HSC 1 1
1-phase
[d]
[R]
[d]
[R]
2-phase
CU
CD
[R]
CU
CD
[R]
[R]
AB-phase
HSC 2 1
HSC 3
HSC 5
[R]
1-phase
[R]
[d]
[R]
[d]
2-phase
[R]
CU
CD
[R]
CU
CD
AB-phase
[R]
[R]
1-phase
[d]
2-phase
CU
CD
AB-phase
2
SB input (default 4.x) 3
1-phase
[d]
[R]
2-phase
CU
CD
[R]
[R]
AB-phase
S7-1200 Programmable controller
System Manual, 04/2012, A5E02486680-06
341
Technology instructions
9.1 High-speed counter
HSC
CPU on-board input (0.x)
0
HSC 6 2
SB input (default 4.x) 3
1
1-phase
[R]
[d]
2-phase
[R]
CU
CD
AB-phase
[R]
HSC 1 and HSC 2 can be configured for either the on-board inputs or for an SB.
HSC 5 and HSC 6 are available only with an SB. HSC 6 is available only with a 4-input SB.
An SB with only 2 digital inputs provides only the 4.0 and 4.1 inputs.
The following table shows the HSC input assignments for both the on-board I/O of the CPU
1212C and an SB. (If the SB has only 2 inputs, only 4.0 and 4.1 inputs are available.)
For single-phase: C is the Clock input, [d] is the optional direction input, and [R] is an
optional external reset input. (Reset is available only for "Counting" mode.)
For two-phase: CU is the Clock Up input, CD is the Clock Down input, and [R] is an
optional external reset input. (Reset is available only for "Counting" mode.)
For AB-phase quadrature: A is the Clock A input, B is the Clock B input, and [R] is an
optional external reset input. (Reset is available only for "Counting" mode.)
Table 9- 7
HSC input assignments for CPU 1212C
HSC
CPU on-board input (0.x)
0
HSC 1
HSC 3
HSC 5
[d]
[R]
[d]
[R]
CU
CD
[R]
CU
CD
[R]
[R]
[R]
1-phase
[R]
[d]
[R]
[d]
2-phase
[R]
CU
CD
[R]
CU
CD
AB-phase
[R]
[R]
1-phase
[d]
[R]
2-phase
CU
CD
[R]
[R]
1-phase
[R]
[d]
2-phase
[R]
CU
CD
AB-phase
[R]
1-phase
[d]
2-phase
CU
CD
[R]
[R]
AB-phase
HSC 6 2
2-phase
AB-phase
HSC 4
1-phase
AB-phase
HSC 2 1
SB input (4.x) 3
[R]
1-phase
[R]
[d]
2-phase
[R]
CU
CD
AB-phase
[R]
HSC 1 and HSC 2 can be configured for either the on-board inputs or for an SB.
HSC 5 and HSC 6 are available only with an SB. HSC 6 is available only with a 4-input SB.
An SB with only 2 digital inputs provides only the 4.0 and 4.1 inputs.
S7-1200 Programmable controller
342
System Manual, 04/2012, A5E02486680-06
Technology instructions
9.1 High-speed counter
The following two tables show the HSC input assignments for the on-board I/O of the CPU
1214C and for an optional SB, if installed.
For single-phase: C is the Clock input, [d] is the optional direction input, and [R] is an
optional external reset input. (Reset is available only for "Counting" mode.)
For two-phase: CU is the Clock Up input, CD is the Clock Down input, and [R] is an
optional external reset input. (Reset is available only for "Counting" mode.)
For AB-phase quadrature: A is the Clock A input, B is the Clock B input, and [R] is an
optional external reset input. (Reset is available only for "Counting" mode.)
Table 9- 8
HSC input assignments for CPU 1214C and CPU 1215C (on-board inputs only)
HSC
HSC
Digital input 0 (default: 0.x)
11
1-phase
[d]
[R]
2-phase
CU
CD
[R]
[R]
1-phase
[d]
[R]
2-phase
CU
CD
[R]
1-phase
[R]
C
[d]
[R]
2-phase
CU
CD
[R]
[R]
AB-phase
HSC
21
HSC 3
1-phase
[R]
[d]
2-phase
[R]
CU
CD
AB-phase
[R]
HSC 51
1-phase
[d]
[R]
2-phase
CU
CD
[R]
[R]
AB-phase
HSC 4
Digital input 1 (default: 1.x)
1-phase
[R]
[d]
2-phase
[R]
CU
CD
AB-phase
[R]
AB-phase
HSC 61
AB-phase
1
HSC 1, HSC 2, HSC 5 and HSC 6 can be configured for either the on-board inputs or for an SB.
Table 9- 9
HSC input assignments for SBs
HSC 1
SB inputs (default: 4.x) 2
0
HSC 1
1-phase
[d]
[R]
2-phase
CU
CD
[R]
[R]
AB-phase
HSC 2
1-phase
[R]
[d]
2-phase
[R]
CU
CD
AB-phase
[R]
S7-1200 Programmable controller
System Manual, 04/2012, A5E02486680-06
343
Technology instructions
9.1 High-speed counter
HSC 1
HSC 5
SB inputs (default: 4.x) 2
0
1-phase
[d]
[R]
2-phase
CU
CD
[R]
AB-phase
HSC 6
[R]
1-phase
[R]
[d]
2-phase
[R]
CU
CD
AB-phase
[R]
For CPU 1214C: HSC 1, HSC 2, HSC 5 and HSC 6 can be configured for either the on-board
inputs or for an SB.
An SB with only 2 digital inputs provides only the 4.0 and 4.1 inputs.
Accessing the current value for the HSC
Note
When you enable a pulse generator for use as a PTO, a corresponding HSC is assigned to
this PTO. HSC1 is assigned for PTO1, and HSC2 is assigned for PTO2. The assigned HSC
belongs completely to the PTO channel, and the ordinary output of the HSC is disabled. The
HSC value is only used for the internal functionality. You cannot monitor the current value
(for example, in ID1000) when pulses are occurring.
The CPU stores the current value of each HSC in an input (I) address. The following table
shows the default addresses assigned to the current value for each HSC. You can change
the I address for the current value by modifying the properties of the CPU in the Device
Configuration.
Table 9- 10
Current value of the HSC
HSC
Data type
Default address
HSC1
DInt
ID1000
HSC2
DInt
ID1004
HSC3
DInt
ID1008
HSC4
DInt
ID1012
HSC5
DInt
ID1016
HSC6
DInt
ID1020
S7-1200 Programmable controller
344
System Manual, 04/2012, A5E02486680-06
Technology instructions
9.1 High-speed counter
9.1.2
Configuration of the HSC
The CPU allows you to configure up to 6 high-speed
counters. You edit the "Properties" of the CPU to
configure the parameters of each individual HSC.
Use the CTRL_HSC instruction in your user program to
control the operation of the HSC.
Enable the specific HSC by selecting the "Enable" option
for that HSC.
Note
When you enable the high speed counter and select input points for it, the input filter settings
for these points are configured to 800 ns. Each input point has a single filter configuration
that applies to all uses: process inputs, interrupts, pulse catch, and HSC inputs.
WARNING
If the filter time for a digital input channel is changed from a previous setting, a new "0"
level input value may need to be presented for up to 20.0 ms accumulated duration before
the filter becomes fully responsive to new inputs. During this time, short "0" pulse events of
duration less than 20.0 ms may not be detected or counted.
This changing of filter times can result in unexpected machine or process operation, which
may cause death or serious injury to personnel, and/or damage to equipment.
To ensure that a new filter time goes immediately into effect, a power cycle of the CPU
must be applied.
S7-1200 Programmable controller
System Manual, 04/2012, A5E02486680-06
345
Technology instructions
9.2 PID control
After enabling the HSC, configure the other parameters, such as counter function, initial
values, reset options and interrupt events.
For information about configuring the HSC, refer to the section on configuring the CPU
(Page 123).
9.2
PID control
STEP 7 provides the following PID instructions for the S7-1200 CPU:
The PID_Compact instruction is used to control technical processes with continuous
input- and output variables.
The PID_3Step instruction is used to control motor-actuated devices, such as valves that
require discrete signals for open- and close actuation.
Note
Changes that you make to the PID configuration and download in RUN mode do not take
effect until the CPU transitions from STOP to RUN mode.
Both PID instructions (PID_3Step and PID_Compact) can calculate the P-, I-, and Dcomponents during startup (if configured for "pretuning"). You can also configure the
instruction for "fine tuning" to allow you to optimize the parameters. You do not need to
manually determine the parameters.
Note
Execute the PID instruction at constant intervals of the sampling time (preferably in a cyclic
OB).
Because the PID loop needs a certain time to respond to changes of the control value, do
not calculate the output value in every cycle. Do not execute the PID instruction in the main
program cycle OB (such as OB 1).
The sampling time of the PID algorithm represents the time between two calculations of the
output value (control value). The output value is calculated during self-tuning and rounded to
a multiple of the cycle time. All other functions of PID instruction are executed at every call.
S7-1200 Programmable controller
346
System Manual, 04/2012, A5E02486680-06