9.4.1 SIMATIC timer functions
Timer functions are used to implement dynamic processes in the user program.
The SIMATIC timer functions are an operand area in the CPU's system memory and their number is limited. Table 9.3 shows the operations possible in conjunction with a timer operand. How the SIMATIC timer functions respond is described in de- tail in Chapter 12.3 “SIMATIC timer functions” on page 477.
For programming, enter the timer operation in a line or drag the corresponding symbol with the mouse from the program elements catalog under Basic instruc- tions > Basic instructions > Timer operations to the working area. The operation is followed by a space and then the time operand (T) to which you can assign a sym- bolic name in the PLC tag table.
When programming a SIMATIC timer function you must make sure that the opera- tions are in the correct order: first enable, then start and reset, and finally load time value and scan status. In so doing, you only program the operations required for the function to be executed.
When starting a SIMATIC timer function, the control processor obtains the defined duration from accumulator 1. When and how this value enters the accumulator is unimportant. In order to make your program easier to read, you should preferably load the duration into the accumulator directly prior to the start operation, either as a constant with direct specification of the duration in data format S5TIME or as a tag with the duration as content. Loading of a value into the accumulator is de- scribed in Chapter 13.2.3 “Loading and transferring with STL” on page 510.
Note that a valid duration must also be present in accumulator 1 even if the timer function is not started when processing the start operation.
Table 9.3 Operations for SIMATIC timer operands Operation Operand Function
SP SE SD SS SF
Timer operand Timer operand Timer operand Timer operand Timer operand
Start a SIMATIC timer function as pulse Start a SIMATIC timer function as extended pulse Start a SIMATIC timer function as ON delay Start a SIMATIC timer function as retentive ON delay Start a SIMATIC timer function as OFF delay FR
R
Timer operand Timer operand
Enabling a SIMATIC timer function Resetting a SIMATIC timer function L
LC
Timer operand Timer operand
Direct loading of a time value Coded loading of a time value A, AN
O, ON X, XN
Timer operand Timer operand Timer operand
Status scan of a SIMATIC timer function and linking according to AND Status scan of a SIMATIC timer function and linking according to OR Status scan of a SIMATIC timer function and linking according to exclusive OR
In Fig. 9.15, the time “Fan5.on_delay” is started as an ON delay by the positive edge of #Fan5.start. The duration of 3 seconds was previously loaded into the accumula- tor as the constant S5T#3S. Following expiry of the duration, the timer function
“Fan5.off_delay” is started with the duration present as a value in the
#Follow-up_time tag. The status of the timer function “Fan.off_delay” simultaneous- ly has signal state “1” so that fan 5 is switched on following the ON delay. Once the start signal #Fan5.start has signal state “0”, fan 5 continues to run for the follow-up time and is then switched off.
Example of clock generator
The somewhat more complex example in Fig. 9.16 shows a clock generator with a different pulse-to-pause ratio implemented by means of a single timer function.
The JC statement Conditional jump is executed if the result of logic operation is “1”.
A #Fan5.start L S5T#3S
SD “Fan5.on_delay”
A “Fan5.on_delay”
L #Follow-up_time SF “Fan5.off_delay”
A “Fan5.off_delay”
= #Fan5.drive
//Start as ON delay
//Start as OFF delay //Scan status
Fig. 9.15 Example of application of SIMATIC timer functions with STL
AN #Start_input R “Timer function”
R #Output
JC M1
A “Timer function”
JC M2
AN #Output
= #Output
L #Pulse_duration
JC M2
L #Pause_duration M2: AN “Timer function”
SE “Timer function”
M1: ... //Further program
#Start_input starts the clock generator.
If the timer “Timer function” is not running or has expired, it is started as an extended pulse.
The binary scaler #Output changes its sig- nal state with each (new) start of the timer and thus also determines the duration –
#Pulse_duration or #Pause_duration – with which the timer is started.
Fig. 9.16 Example of clock generator with different pulse-to-pause ratio
9.4.2 SIMATIC counter functions
Counter functions are used to implement counting tasks in the user program. The SIMATIC counter functions are an operand area in the CPU's system memory and their number is limited. Table 9.4 shows the counter operations possible in con- junction with a counter operand. How a SIMATIC counter function responds is de- scribed in detail in Chapter 12.5 “SIMATIC counter functions” on page 495.
For programming, enter the counter operation in a line or drag the corresponding symbol with the mouse from the program elements catalog under Basic instruc- tions > Basic instructions > Counter operations in a line. The operation is followed by a space and then the counter operand (C) to which you can assign a symbolic name in the PLC tag table.
When programming a SIMATIC counter function you must make sure that the op- erations are in the correct order: first enable, then count, set and reset, and finally load count value and scan status. In so doing, you only program the operations re- quired for the function to be executed.
When setting a SIMATIC counter function, the control processor obtains the initial count value from accumulator 1. When and how this value enters the accumulator is unimportant. In order to make your program easier to read, you should prefera- bly load the initial count value into the accumulator directly prior to the set opera- tion, either as a constant with direct specification of the count value in data format W#16# or C# or as a tag with the count value as content. Loading of a value into the accumulator is described in Chapter 13.2.3 “Loading and transferring with STL” on page 510.
Note that a valid count value must also be present in accumulator 1 even if the counter function is not set when processing the set operation.
Fig. 9.17 shows the counting of workpieces up to a specific quantity. The counter
#Parts_counter is set by the #Quantity_set tag to a start value of 120. Each positive Table 9.4 Operations for SIMATIC counter operands
Operation Operand Function CU
CD S
Counter operand Counter operand Counter operand
Increment a SIMATIC counter function by one unit Decrement a SIMATIC counter function by one unit Set a SIMATIC counter function to a start value FR
R
Counter operand Counter operand
Enabling a SIMATIC counter function Resetting a SIMATIC counter function L
LC
Counter operand Counter operand
Direct loading of a count value Coded loading of a count value A, AN
O, ON X, XN
Counter operand Counter operand Counter operand
Status scan of a SIMATIC counter function and linking according to an AND logic operation
Status scan of a SIMATIC counter function and linking according to OR Status scan of a SIMATIC counter function and linking according to an exclusive OR logic operation
edge at the #Workpiece_identified tag decrements the count value by one unit. If a value of zero is reached – the counter status is then “0” – #Quantity_reached is set.
9.4.3 IEC timer functions
Timer functions are used to implement dynamic processes in the user program.
With a CPU 400, an IEC timer function is a system function block (SFB) in the oper- ating system and is called in the user program like a function block. A detailed de- scription of the IEC timer functions is provided in Chapter 12.4 “IEC timer func- tions” on page 491.
For programming, drag the corresponding symbol (TP, TON, or TOF) with the mouse from the program elements catalog under Basic instructions > Timer opera- tions into a line on the working area. When positioning, you select either as single instance or as local instance. The instance data block generated automatically when selecting as a single instance is saved in the project tree under Program blocks > System blocks > Program resources.
With the IEC timer functions, a binary tag must be connected to the IN input, and a duration to the PT input. You can also directly access the output parameters using the instance data, for example with “DB_name”.Q or “DB_name”.ET for a single in- stance.
Fig. 9.18 shows the IEC timer function #MessageDelay, which saves its data as a local instance in the instance data block of the calling function block. If the #Measure- ment_too_high tag has a signal state “1” for longer than 10 s, #Message_too_high is set.
A #Workpiece_identified
CD “Parts_counter” //Count down A #Quantity_set
L C#120
S “Parts_counter”
//Load default value
//Set counter to default value AN “Parts_counter”
= #Quantity_reached
//Scan status of counter
Fig. 9.17 Example of application of a SIMATIC counter function with STL
CALL #MessageDelay Time
IN := #Measurement_too_high PT := T#10S
Q := #Message_too_high ET :=
//Start as ON delay
//10 s duration //ET is not required Fig. 9.18 Example of IEC timer function with STL
9.4.4 IEC counter functions
A counter function implements counting processes in the user program. With a CPU 400, an IEC counter function is a system function block (SFB) in the operating sys- tem and is called in the user program like a function block. A detailed description of the IEC counter functions is provided in Chapter 12.6 “IEC counter functions” on page 502.
For programming, drag the corresponding symbol (CTUD, CTU or CTD) with the mouse from the program elements catalog under Basic instructions > Counter oper- ations into a line on the working area. When positioning, you select either as single instance or as local instance. The instance data block generated automatically when selecting as a single instance is saved in the project tree under Program blocks > Sys- tem blocks > Program resources.
With the IEC counter functions, a binary tag must be connected to at least one counter input (CU or CD). Connection of the other function inputs and outputs is optional. You can also directly access the output parameters using the instance data, for example with “DB_name”.QD or “DB_name”.CV for a single instance.
Fig. 9.19 shows the IEC counter function #Lock_counter, which is called as a local instance. It has saved its data in the instance data block of the calling function block.
A component of the counter can be addressed globally with the name of the instance and the component name, for example #Lock_counter.CV. The example shows the passages through a lock, either forward or backward.
A “Light_barrier2”
FP “Light_barrier2_Edge_trigger_flag”
A “Light_barrier1”
= #temp_bool1 //Count up
A “Light_barrier1”
FP “Light_barrier1_Edge_trigger_flag”
A “Light_barrier2”
= #temp_bool2 //Count down
CALL #Lock_counter Int
CU := #temp_bool1 CD := #temp_bool2 R := #Acknowledge LOAD :=
PV :=
QU :=
QD :=
CV :=
//Start as //Up/down counter
Fig. 9.19 Example of IEC counter function with STL