
5.2 Standard Function Blocks 223
LD Counter_UD.QU
ST MaxReached
LD Counter_DU.QD
ST MinReached
LD Counter_UD.CV
ST CounterValue
R:= %IX1.4,
LD := %IX2.1,
PV := %IW5);
MaxReached := Counter_UD.QU;
MinReached := Counter_UD.QD;
CounterValue := Counter_UD.CV;
Example. 5.20. The up/down counter CTUD
In this example, each input of the up/down counter
Counter_UD
is used. This is,
however, not always necessary.
The inputs CU and CD can be activated simultaneously by a rising edge. In this
case the current counter value would not change if the minimum or maximum had
not already been reached.
Counter_UD
in Example 5.20 counts up with each rising edge at
%IX2.0
and
counts down with each falling edge at
%IX3.0
. The pre-set counter value at PV is
loaded from
%IW5
if the load input LD is active when the FB is called. No rising
edge is needed in this case.
Timer
Example 5.21 is an example of the usage of timer FBs. It demonstrates clearly how
instances of timers maintain their values, especially those of the input parameters,
between calls.
In principle, each input variable of a timer (or any FB) can be set immediately
before calling. Such run-time parameter changes could be used to allow the same
timer to be used to control several process times simultaneously. Such program-
ming is, however, seldom used in practice as it makes the program difficult to read
and can easily lead to errors.
It is sufficient to set the pre-set timer value PT for each instance only once, with
the first call, and then to re-use it for later invocations. This means that calling the
timer primarily serves to start the timer with input IN.
The output variables of a timer can be checked at any point in the program, i.e.
they need not be evaluated immediately after calling the timer.
The output parameters are set at each call of the timer FB, i.e. they are updated
with the current values of the physical timer running in the background. The timer
value may therefore become obsolete between two timer calls. Therefore, in order
to avoid distorting the desired time control, it must be ensured that the timer FB is
called sufficiently frequently in a periodic task, not too long before Q or ET are
evaluated.
Output Q shows whether the time has elapsed or not, and output ET shows the
time still remaining.
Timers are thus usually called in the following steps: