~~NOCACHE~~ ====== LamaPLC: Simatic S7 SCL commands: Timer / counter functions ====== {{ :simatic:lama19.png?100|Simatic S7 SCL commands with examples}} ===== Counter operations ===== ==== IEC Counters ==== Counters - similar to IEC times - can be called in two ways, embedded or with a separate DB. I don't feel like describing this separately, please take a look at the times: [[:simatic:scl_commands#iec_timers|IEC Timers]]. [[simatic:scl_commands|>> Back to LamaPLC main menu (SCL commands)]] ==== CTU: Count up ==== {{anchor:ctu}} The function that only counts upwards. The CTU counts the positive edges of the "CU:" parameter. As soon as it reaches the value of "PV" with the calculation, the output "Q" changes to "TRUE". The function continues to count. The function can be restarted with the "R" input. **CTU types:** * CTU_SINT / CTU_USINT: the calculation goes with the variable types [[:simatic:typedef#sint|SINT]], [[:simatic:typedef#usint|USINT]] * CTU_INT / CTU_UINT: the calculation goes with the variable types [[:simatic:typedef#int|INT]], [[:simatic:typedef#uint|UINT]] * CTU_DINT / CTU_UDINT: the calculation goes with the variable types [[:simatic:typedef#dint|DINT]], [[:simatic:typedef#udint|UDINT]] * CTU_LINT / CTU_ULINT: the calculation goes with the variable types [[:simatic:typedef#lint|LINT]], [[:simatic:typedef#ulint|ULINT]] (only on S7-1500 PLC) \\ \\ |< 100% 15% 15% 15%>| |[[:simatic:tia_knowhow#fb|_FB_]] **CTU (**| | | | | |**CU:** [[:simatic:typedef#bool|BOOL]] **,**|input|**Counter signal:** The function monitors the positive edge of this binary signal. The counter counted, if signal change from "FALSE" to "TRUE".| | |**R:** [[:simatic:typedef#bool|BOOL]] **,**|input|**Reset:** Reset CV value.| | |**PV:** [[:simatic:typedef#int|INT]] **,** |input|**Set value to Q:** Value at which the Q output is set| | |**Q:** [[:simatic:typedef#bool|BOOL]] **,**|output|**Result:** When the counter reaches "PV".| | |**CV:** \\ [[:simatic:typedef#int|INT]] / \\ [[:simatic:typedef#char|CHAR]] / \\ [[:simatic:typedef#wchar|WCHAR]] / \\ [[:simatic:typedef#date|DATE]] |output|**Current counter value**| |**);**|||| === Example === {{:simatic:ctu_example.png|CTU example}} The example program below can be downloaded here: {{ :simatic:ctu_example.scl |ctu_example.scl}}. \\ \\ Here is a description of how to import the downloaded program to the TIA Portal: [[:simatic:tia_knowhow#import|Import source code to the TIA portal]]. [[simatic:scl_commands|>> Back to LamaPLC main menu (SCL commands)]] ==== CTD: Count down ==== {{anchor:ctd}} The function that only counts downwards. The CTD counts the positive edges of the "CD" parameter, the current counter value of the CV parameter is decremented by one. The function can be restarted with "LD" input, in this case, the countdown starts again from the "PV" value. If the current counter value is less than or equal to zero, the "Q" parameter is set to signal state "TRUE". **CTD types:** * CTD_SINT / CTD_USINT: the calculation goes with the variable types [[:simatic:typedef#sint|SINT]], [[:simatic:typedef#usint|USINT]] * CTD_INT / CTD_UINT: the calculation goes with the variable types [[:simatic:typedef#int|INT]], [[:simatic:typedef#uint|UINT]] * CTD_DINT / CTD_UDINT: the calculation goes with the variable types [[:simatic:typedef#dint|DINT]], [[:simatic:typedef#udint|UDINT]] * CTD_LINT / CTD_ULINT: the calculation goes with the variable types [[:simatic:typedef#lint|LINT]], [[:simatic:typedef#ulint|ULINT]] (only on S7-1500 PLC) \\ \\ |< 100% 15% 15% 15%>| |[[:simatic:tia_knowhow#fb|_FB_]] **CTD (**| | | | | |**CD:** [[:simatic:typedef#bool|BOOL]] **,**|input|**Counter signal:** The function monitors the positive edge of this binary signal. The counter downcounted, if signal change from "FALSE" to "TRUE".| | |**LD:** [[:simatic:typedef#bool|BOOL]] **,**|input|**Reset:** Load PV value to CV. (Counter newstart)| | |**PV:** [[:simatic:typedef#int|INT]] **,** |input|**Downcounter start value:** The value from which the counter counts down. It can be reloaded with the LD parameter.| | |**Q:** [[:simatic:typedef#bool|BOOL]] **,**|output|**Result:** If the current counter value is less than or equal to zero, the parameter is set to signal state "TRUE".| | |**CV:** \\ [[:simatic:typedef#int|INT]] / \\ [[:simatic:typedef#char|CHAR]] / \\ [[:simatic:typedef#wchar|WCHAR]] / \\ [[:simatic:typedef#date|DATE]] |output|**Current counter value**| |**);**|||| === Example === {{:simatic:ctd_example.png|CTD example}} The example program below can be downloaded here: {{ :simatic:ctd_example.scl |ctd_example.scl}}. \\ \\ Here is a description of how to import the downloaded program to the TIA Portal: [[:simatic:tia_knowhow#import|Import source code to the TIA portal]]. [[simatic:scl_commands|>> Back to LamaPLC main menu (SCL commands)]] ==== CTUD: Count up ==== {{anchor:ctud}} The function that counts up- and downwards. The CTUD counts up the positive edges of the "CU" input, and counts down with "CD" input. As soon as it reaches the value of "PV" with the calculation, the output "QU" changes to "TRUE". The function continues to count. The function can be restarted with the "R" input. The downcounts function can be restarted with "LD" input, in this case, the countdown starts again from the "PV" value. If the current counter value is less than or equal to zero, the "QD" parameter is set to signal state "TRUE". **CTUD types:** * CTUD_SINT / CTUD_USINT: the calculation goes with the variable types [[:simatic:typedef#sint|SINT]], [[:simatic:typedef#usint|USINT]] * CTUD_INT / CTUD_UINT: the calculation goes with the variable types [[:simatic:typedef#int|INT]], [[:simatic:typedef#uint|UINT]] * CTUD_DINT / CTUD_UDINT: the calculation goes with the variable types [[:simatic:typedef#dint|DINT]], [[:simatic:typedef#udint|UDINT]] * CTUD_LINT / CTUD_ULINT: the calculation goes with the variable types [[:simatic:typedef#lint|LINT]], [[:simatic:typedef#ulint|ULINT]] (only on S7-1500 PLC) \\ |< 100% 15% 15% 15%>| |[[:simatic:tia_knowhow#fb|_FB_]] **CTUD (**| | | | | |**CU:** [[:simatic:typedef#bool|BOOL]] **,**|input|**Counter up signal:** The function monitors the positive edge of this binary signal. The counter counted up, if signal change from "FALSE" to "TRUE".| | |**CD:** [[:simatic:typedef#bool|BOOL]] **,**|input|**Counter down signal:** The function monitors the positive edge of this binary signal. The counter downcounted, if signal change from "FALSE" to "TRUE".| | |**R:** [[:simatic:typedef#bool|BOOL]] **,**|input|**Reset upcounter:** Reset upcounter CV value.| | |**LD:** [[:simatic:typedef#bool|BOOL]] **,**|input|**Reset downcounter:** Load PV value to CV. (Downcounter newstart)| | |**PV:** [[:simatic:typedef#int|INT]] **,** |input|**Set value to Q / startpoint for downcounter** Value at which the Q output is set / The value from which the counter counts down. It can be reloaded with the LD parameter.| | |**QU:** [[:simatic:typedef#bool|BOOL]] **,**|output|**Result:** When the counter reaches "PV".| | |**QD:** [[:simatic:typedef#bool|BOOL]] **,**|output|**Result:** If the current counter value is less than or equal to zero, the parameter is set to signal state "TRUE".| | |**CV:** \\ [[:simatic:typedef#int|INT]] / \\ [[:simatic:typedef#char|CHAR]] / \\ [[:simatic:typedef#wchar|WCHAR]] / \\ [[:simatic:typedef#date|DATE]] |output|**Current counter value**| |**);**|||| [[simatic:scl_commands|>> Back to LamaPLC main menu (SCL commands)]] ===== Timer operations ===== ==== IEC timers ==== {{anchor:iec_timers}} IEC timers (TP, TON, TOF) should definitely be called embedded from the FBs, the description of which can be found here: [[:simatic:tia_knowhow#call_fb|Call FBs]] \\ In the case of embedding, the functions must be called from the "static" block of the calling FB (just type "TON" for the "data type"): |< 100% 65% 35% >| |{{:simatic:fb_call_4.png|}}|In the case of embedding, the functions must be called from the "static" block of the calling FB (just type "TON" for the "data type". FBs must be called in the program in much the same way, of course the DB call must be omitted.| |{{:simatic:fb_call_5.png|}}|The image shows the data of the embedded FBs in the IDB of the calling FB. The great advantage of this method is that the program is easy to import / export / copy, as you only need one FB definition.| ==== TON: Generate on-delay ==== {{anchor:ton}} The TON "switch-on delay" function. It monitors the positive edge of the binary signal at the "IN" input. If the signal persists for a specified "PT" time, it outputs "Q". The timer is most commonly used to delay signals, for example: - in the case of a field signal, we want to make sure that not only one interference signal is received - in case of level signals we want to get a certain signal (many level signals "wobble") {{ :simatic:ton_iec.png |TON: Generate on-delay}} |< 100% 15% 15% 15%>| |[[:simatic:tia_knowhow#fb|_FB_]] **TON (**| | | | | |**IN:** [[:simatic:typedef#bool|BOOL]] **,**|input|**Start signal:** The function monitors the positive edge of this binary signal. The timer start, if signal is "TRUE".| | |**PT:** [[:simatic:typedef#time|TIME]] **,** |input|**Delay time:** The delay time, example: t#12s| | |**Q:** [[:simatic:typedef#bool|BOOL]] **,**|output|**Result:** When the time is up, the result will be "TRUE".| | |**ET:** [[:simatic:typedef#time|TIME]] |output|**Elapsed time**| |**);**|||| [[simatic:scl_commands|>> Back to LamaPLC main menu (SCL commands)]] === TON Examples === == CALL TON == {{:simatic:ton_example.png|TON Examples}} == TON second impuls == The example program below can be downloaded here: {{ :simatic:test_ton.scl |test_ton.scl}}. \\ \\ Here is a description of how to import the downloaded program to the TIA Portal: [[:simatic:tia_knowhow#import|Import source code to the TIA portal]]. // author OB121 / Sandor Vamos // ob121.com; 2022.04.13. // TON example: sec impuls // // More information: // https://www.ob121.com/doku.php?id=de:s7:scl_reference_timers // // FB "static" variables: // secTakt: TON_TIME // q, secChange: BOOL // secCount: INT // FB "temp" variables: // tempInt: INT // tempBool: BOOL // TON, as sec impuls generator #secTakt(IN:=NOT(#q), PT:=t#1s); // For example, the "Q" parameter can be called // directly when calling "secTakt", // OR it can be referred to as: #q := #secTakt.Q; // when the time has elapsed, the block will be called: // - secChange changes its status every second // - secCount increases every second, in the range 0..9 IF #q THEN #secChange := NOT (#secChange); #secCount := #secCount + 1; IF #secCount > 9 THEN #secCount := 0; END_IF; END_IF; // monitor #tempInt := #secCount; #tempBool := #secChange; [[simatic:scl_commands|>> Back to LamaPLC main menu (SCL commands)]] ==== TOF: Generate off-delay ==== {{anchor:tof}} The TOF "switch-off delay" function. It monitors the positive edge of the binary signal at the "IN" input. If the signal persists for a specified "PT" time, it outputs "Q". The timer is most commonly used to delay signals, for example: - in the case of a field signal, we want to make sure that not only one interference signal is received - in case of level signals we want to get a certain signal (many level signals "wobble") {{ :simatic:tof_iec.png |TON: Generate off-delay}} |< 100% 15% 15% 15%>| |[[:simatic:tia_knowhow#fb|_FB_]] **TOF (**| | | | | |**IN:** [[:simatic:typedef#bool|BOOL]] **,**|input|**Start signal:** The function monitors the positive edge of this binary signal. The timer start, if signal is "TRUE".| | |**PT:** [[:simatic:typedef#time|TIME]] **,** |input|**Delay time:** The delay time, example: t#12s| | |**Q:** [[:simatic:typedef#bool|BOOL]] **,**|output|**Result:** When the time is up, the result will be "TRUE".| | |**ET:** [[:simatic:typedef#time|TIME]] |output|**Elapsed time**| |**);**|||| == TOF Examples == {{:simatic:tof_example.png|TOF Examples}} [[simatic:scl_commands|>> Back to LamaPLC main menu (SCL commands)]] ==== TP: Generate pulse ==== {{anchor:tp}} The TP "pulse generator" function. It monitors the positive edge of the binary signal at the "IN" input, and generates pulses in effect equal "PT" length from this. It is commonly used to extend very short-lived signals because, for example, the cycle time of a WinCC HMI is typically 1 sec. If the signals are "extended" for 2 seconds, the HMI can also process them. {{ :simatic:tp_iec.png |TP: Generate pulse}} |< 100% 15% 15% 15%>| |[[:simatic:tia_knowhow#fb|_FB_]] **TP (**| | | | | |**IN:** [[:simatic:typedef#bool|BOOL]] **,**|input|**Start signal:** The function monitors the positive edge of this binary signal. The timer start, if signal is first time "TRUE".| | |**PT:** [[:simatic:typedef#time|TIME]] **,** |input|**Extending time:** example: t#12s| | |**Q:** [[:simatic:typedef#bool|BOOL]] **,**|output|**Result:** "Q" will be TRUE for the duration of the signal extension.| | |**ET:** [[:simatic:typedef#time|TIME]] |output|**Elapsed time**| |**);**|||| [[simatic:scl_commands|>> Back to LamaPLC main menu (SCL commands)]] ==== TONR: Time accumulator ==== {{anchor:tonr}} The TONR "time accumulator" function. It practically collects the times, and if the input signal "IN" has already existed for the right amount of time, it indicates a result "Q". The timer function can be reset with the reset input "R" to set null the amount of time collected. {{ :simatic:tonr_iec.png |TONR: Time accumulator}} |< 100% 15% 15% 15%>| |[[:simatic:tia_knowhow#fb|_FB_]] **TONR (**| | | | | |**IN:** [[:simatic:typedef#bool|BOOL]] **,**|input|**Start signal:** The function monitors the positive edge of this binary signal. The timer running, if signal is "TRUE".| | |**R:** [[:simatic:typedef#bool|BOOL]] **,**|input|**Reset signal:** The timer function can be reset with the reset input "R" to set null the amount of time collected.| | |**PT:** [[:simatic:typedef#time|TIME]] **,** |input|**Extending time:** example: t#12s| | |**Q:** [[:simatic:typedef#bool|BOOL]] **,**|output|**Result:** After collecting the "PT" amount of time, the "Q" output will be "TRUE". The function can then be restarted with the "R" input.| | |**ET:** [[:simatic:typedef#time|TIME]] |output|**Elapsed / collected time**| |**);**|||| \\ \\ {{tag>Simatic SCL TIA Math CTU CTD CTUD IEC_timers TP TON TOF counter timer}} \\ This page has been accessed for: Today: {{counter|today}}, Until now: {{counter|total}}