top of page
Search
Writer's pictureIvaylo Fiziev

SCL Counters


Counters? Isn't this just a variable of type INT? Why do they need special attention?

Well if you have ever used a step counter you already have the answer. It is not only about incrementing a variable. You also need to know when to increment/reset it.

SCL provides some basic counter FBs out of the box. Just like triggers they live as static variables in your code in order to preserve their state between script executions. So let's take a closer look.

Currently (v2307) we have three counter types:

  • CTU_INT - Integer up counter.

Inputs:

a) CU:BOOL - triggers counter increment on positive edge

b) PV:INT - counter max value

c) R:BOOL - resets the counter to zero on positive edge

Outputs:

a) CV:INT - current counter value

b) Q:BOOL - counter max value reached

  • CTD_INT - Integer down counter.

Inputs:

a) CD:BOOL - triggers counter decrement on positive edge

b) PV:INT - initial counter value

c) LD:BOOL - sets the counter to the initial value on positive edge

Outputs:

a) CV:INT - current counter value

b) Q:BOOL - counter reached value of zero

  • CTUD_INT - Integer up/down counter. It serves both purposes at once.

Inputs:

a) CU:BOOL - triggers counter increment on positive edge

a) CD:BOOL - triggers counter decrement on positive edge

c) R:BOOL - resets the counter to zero on positive edge

c) LD:BOOL - sets the counter to the initial value on positive edge

b) PV:INT - counter max value/ initial counter value

Outputs:

a) QU:BOOL - counter max value reached

b) QD:BOOL - counter reached value of zero

c) CV:INT - current counter value


If I go back to the example with the step up counter the code can look like this:


FUNCTION_BLOCK "MAIN"

VERSION:1.0

VAR_INPUT

step : BOOL; // True if you did a step. False otherwise

goal : INT := 10000; // Goal is 10000 steps

reset : BOOL; // Reset switch

END_VAR

VAR_OUTPUT

value : INT; // Current number of steps

done : BOOL; // Goal reached

END_VAR

VAR

counter : CTU_INT; // Up counter

END_VAR

BEGIN

#counter(CU:=#step, // Call the counter

PV:=#goal,

CV=>#value,

Q=>#done,

R:=#reset);

END_FUNCTION_BLOCK


So much for the counters.

See you in the next post!


12 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page