24.01.2015 Views

PLC Programming

  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Appendix C: Data types in CoDeSys<br />

Example: In the case of a variable belonging to a signed subrange type (like i, above), the function<br />

CheckRangeSigned is called; it could be programmed as follows to trim a value to the permissible<br />

range:<br />

FUNCTION CheckRangeSigned : DINT<br />

VAR_INPUT<br />

value, lower, upper: DINT;<br />

END_VAR<br />

IF (value < lower) THEN<br />

CheckRangeSigned := lower;<br />

ELSIF(value > upper) THEN<br />

CheckRangeSigned := upper;<br />

ELSE<br />

CheckRangeSigned := value;<br />

END_IF<br />

In calling up the function automatically, the function name CheckRangeSigned is obligatory, as is the<br />

interface specification: return value and three parameters of type DINT<br />

When called, the function is parameterized as follows:<br />

- value: the value to be assigned to the range type<br />

- lower: the lower boundary of the range<br />

- upper: the upper boundary of the range<br />

- Return value: this is the value that is actually assigned to the range type<br />

An assignment i:=10*y implicitly produces the following in this example:<br />

i := CheckRangeSigned(10*y, -4095, 4095);<br />

Even if y for example has the value 1000, then i still has only the value 4095 aft er this assignment.<br />

The same applies to the function CheckRangeUnsigned: function name and interface must be correct.<br />

FUNCTION CheckRangeUnsigned : UDINT<br />

VAR_INPUT<br />

value, lower, upper: UDINT;<br />

END_VAR<br />

Important: If neither of the functions CheckRangeSigned or CheckRangeUnsigned is present, no type checking<br />

of subrange types occurs during runtime! The variable i could then take on any value between –<br />

32768 and 32767 at any time!<br />

Attention: If neither of the functions CheckRangeSigned or CheckRangeUnsigned is present like described<br />

above, there can result an endless loop if a subrange type is used in a FOR loop. This will happen<br />

when the range given for the FOR loop is as big or bigger than the range of the subrange type !<br />

Example:<br />

VAR<br />

ui : UINT (0..10000);<br />

END_VAR<br />

FOR ui:=0 TO 10000 DO<br />

...<br />

END_FOR<br />

The FOR loop will never be finished, because ui cannot get bigger than 10000.<br />

Also take care of the definition of the CheckRange functions when you define the incremental value of<br />

a FOR loop !<br />

CoDeSys V2.3 10-37

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!