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.

Defined data types<br />

FUNCTION CheckBounds : INT<br />

VAR_INPUT<br />

index, lower, upper: INT;<br />

END_VAR<br />

IF index < lower THEN<br />

CheckBounds := lower;<br />

ELSIF index > upper THEN<br />

CheckBounds := upper;<br />

ELSE CheckBounds := index;<br />

END_IF<br />

The following sample program for testing the CheckBounds function exceeds the bounds of a defined<br />

array. The CheckBounds function allows the value TRUE to be assigned, not to location A[10], but to<br />

the still valid range boundary A[7] above it. With the CheckBounds function, references outside of<br />

array boundaries can thus be corrected.<br />

Test Program for the function CheckBounds:<br />

PROGRAM <strong>PLC</strong>_PRG<br />

VAR<br />

a: ARRAY[0..7] OF BOOL;<br />

b: INT:=10;<br />

END_VAR<br />

a[b]:=TRUE;<br />

Pointer<br />

Enumeration<br />

Variable or function block addresses are saved in pointers while a program is running.<br />

Pointer declarations have the following syntax:<br />

: POINTER TO ;<br />

A pointer can point to any data type or function block even to user-defined types.<br />

The function of the Address Operator ADR is to assign the address of a variable or function block to<br />

the pointer.<br />

A pointer can be dereferenced by adding the content operator "^" after the pointer identifier.<br />

Example:<br />

pt:POINTER TO INT;<br />

var_int1:INT := 5;<br />

var_int2:INT;<br />

pt := ADR(var_int1);<br />

var_int2:= pt^; (* var_int2 is now 5 *)<br />

Enumeration is a user-defined data type that is made up of a number of string constants. These<br />

constants are referred to as enumeration values.<br />

Enumeration values are recognized in all areas of the project even if they were declared within a<br />

POU. It is best to create your enumerations as objects in the Object Organizer under the register card<br />

Syntax:<br />

Data types. They begin with the keyword TYPE and end with END_TYPE.<br />

TYPE :( ,, ...,);<br />

END_TYPE<br />

A variable of the type can take on one of the enumeration values and will be initialized<br />

with the first one. These values are compatible with whole numbers which means that you can<br />

perform operations with them just as you would with INT. You can assign a number x to the variable. If<br />

the enumeration values are not initialized, counting will begin with 0. When initializing, make certain<br />

the initial values are increasing. The validity of the number will be reviewed at the time it is run.<br />

Example:<br />

TYPE TRAFFIC_SIGNAL: (Red, Yellow, Green:=10); (*The initial value for each of the colors is<br />

red 0, yellow 1, green 10 *)<br />

10-34 CoDeSys V2.3

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

Saved successfully!

Ooh no, something went wrong!