LamaPLC: Simatic S7 SCL commands: Timer / counter functions

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: IEC Timers.

>> Back to LamaPLC main menu (SCL commands)

CTU: Count up

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 SINT, USINT
  • CTU_INT / CTU_UINT: the calculation goes with the variable types INT, UINT
  • CTU_DINT / CTU_UDINT: the calculation goes with the variable types DINT, UDINT
  • CTU_LINT / CTU_ULINT: the calculation goes with the variable types LINT, ULINT (only on S7-1500 PLC)



_FB_ CTU (
CU: BOOL ,inputCounter signal: The function monitors the positive edge of this binary signal. The counter counted, if signal change from “FALSE” to “TRUE”.
R: BOOL ,inputReset: Reset CV value.
PV: INT , inputSet value to Q: Value at which the Q output is set
Q: BOOL ,outputResult: When the counter reaches “PV”.
CV:
INT /
CHAR /
WCHAR /
DATE
outputCurrent counter value
);

Example

CTU example

The example program below can be downloaded here: ctu_example.scl.

Here is a description of how to import the downloaded program to the TIA Portal: Import source code to the TIA portal.

>> Back to LamaPLC main menu (SCL commands)

CTD: Count down

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 SINT, USINT
  • CTD_INT / CTD_UINT: the calculation goes with the variable types INT, UINT
  • CTD_DINT / CTD_UDINT: the calculation goes with the variable types DINT, UDINT
  • CTD_LINT / CTD_ULINT: the calculation goes with the variable types LINT, ULINT (only on S7-1500 PLC)



_FB_ CTD (
CD: BOOL ,inputCounter signal: The function monitors the positive edge of this binary signal. The counter downcounted, if signal change from “FALSE” to “TRUE”.
LD: BOOL ,inputReset: Load PV value to CV. (Counter newstart)
PV: INT , inputDowncounter start value: The value from which the counter counts down. It can be reloaded with the LD parameter.
Q: BOOL ,outputResult: If the current counter value is less than or equal to zero, the parameter is set to signal state “TRUE”.
CV:
INT /
CHAR /
WCHAR /
DATE
outputCurrent counter value
);

Example

CTD example

The example program below can be downloaded here: ctd_example.scl.

Here is a description of how to import the downloaded program to the TIA Portal: Import source code to the TIA portal.

>> Back to LamaPLC main menu (SCL commands)

CTUD: Count up

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 SINT, USINT
  • CTUD_INT / CTUD_UINT: the calculation goes with the variable types INT, UINT
  • CTUD_DINT / CTUD_UDINT: the calculation goes with the variable types DINT, UDINT
  • CTUD_LINT / CTUD_ULINT: the calculation goes with the variable types LINT, ULINT (only on S7-1500 PLC)


_FB_ CTUD (
CU: BOOL ,inputCounter up signal: The function monitors the positive edge of this binary signal. The counter counted up, if signal change from “FALSE” to “TRUE”.
CD: BOOL ,inputCounter down signal: The function monitors the positive edge of this binary signal. The counter downcounted, if signal change from “FALSE” to “TRUE”.
R: BOOL ,inputReset upcounter: Reset upcounter CV value.
LD: BOOL ,inputReset downcounter: Load PV value to CV. (Downcounter newstart)
PV: INT , inputSet 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: BOOL ,outputResult: When the counter reaches “PV”.
QD: BOOL ,outputResult: If the current counter value is less than or equal to zero, the parameter is set to signal state “TRUE”.
CV:
INT /
CHAR /
WCHAR /
DATE
outputCurrent counter value
);

>> Back to LamaPLC main menu (SCL commands)

Timer operations

IEC timers

IEC timers (TP, TON, TOF) should definitely be called embedded from the FBs, the description of which can be found here: 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”):

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.
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

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”)

TON: Generate on-delay

_FB_ TON (
IN: BOOL ,inputStart signal: The function monitors the positive edge of this binary signal. The timer start, if signal is “TRUE”.
PT: TIME , inputDelay time: The delay time, example: t#12s
Q: BOOL ,outputResult: When the time is up, the result will be “TRUE”.
ET: TIME outputElapsed time
);

>> Back to LamaPLC main menu (SCL commands)

TON Examples

CALL TON

TON Examples

TON second impuls

The example program below can be downloaded here: test_ton.scl.

Here is a description of how to import the downloaded program to the TIA Portal: 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;

>> Back to LamaPLC main menu (SCL commands)

TOF: Generate off-delay

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”)

TON: Generate off-delay

_FB_ TOF (
IN: BOOL ,inputStart signal: The function monitors the positive edge of this binary signal. The timer start, if signal is “TRUE”.
PT: TIME , inputDelay time: The delay time, example: t#12s
Q: BOOL ,outputResult: When the time is up, the result will be “TRUE”.
ET: TIME outputElapsed time
);
TOF Examples

TP: Generate pulse

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.

TP: Generate pulse

_FB_ TP (
IN: BOOL ,inputStart signal: The function monitors the positive edge of this binary signal. The timer start, if signal is first time “TRUE”.
PT: TIME , inputExtending time: example: t#12s
Q: BOOL ,outputResult: “Q” will be TRUE for the duration of the signal extension.
ET: TIME outputElapsed time
);

>> Back to LamaPLC main menu (SCL commands)

TONR: Time accumulator

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.

TONR: Time accumulator

_FB_ TONR (
IN: BOOL ,inputStart signal: The function monitors the positive edge of this binary signal. The timer running, if signal is “TRUE”.
R: BOOL ,inputReset signal: The timer function can be reset with the reset input “R” to set null the amount of time collected.
PT: TIME , inputExtending time: example: t#12s
Q: BOOL ,outputResult: After collecting the “PT” amount of time, the “Q” output will be “TRUE”. The function can then be restarted with the “R” input.
ET: TIME outputElapsed / collected time
);




This page has been accessed for: Today: 1, Until now: 290