In integer variables, individual bits can be accessed. For this purpose, append the index of the bit to be addressed to the variable and separate it by a dot. You can give any constant to the bit index. Indexing is 0-based.
<variablename>.<bitindex>
a : INT;
b : BOOL;
...
a.2 := b;
The third bit of the variable a will be set to the value of the variable b, this means that variable a will equal 3.
If the index is greater than the bit width of the variable, the following message will be generated:
'Index '<n>' outside the valid range for variable '<var>'!'
Bit addressing is possible with variables of the following data types:
oSINT
oINT
oDINT
oUSINT
oUINT
oUDINT
oBYTE
oWORD
oDWORD
If the data type does not allow bit accessing, the following message will be generated:
'Invalid data type '<type>' for direct indexing'.
Do not assign bit access to a VAR_IN_OUT variable.
Bit Access Via a Global Constant
If you have declared a global constant defining the bit index, you can use this constant for a bit access.
Example for a bit access via a global constant and on a variable:
1. Declaration of the global constant in a global variable list
The variable enable defines the bit that is accessed:
VAR_GLOBAL CONSTANT
enable:int:=2;
END_VAR
2. Bit access on an integer variable
Declaration in POU:
VAR
xxx:int;
END_VAR
Bit access:
xxx.enable := true; (* -> the third bit in variable xxx will be set TRUE *)
The BIT data type is a special data type which is only allowed in structures. For further information, refer to Bit Access in Structures.
Example: Bit access on BIT data types
Declaration of structure
TYPE ControllerData :
STRUCT
Status_OperationEnabled : BIT;
Status_SwitchOnActive : BIT;
Status_EnableOperation : BIT;
Status_Error : BIT;
Status_VoltageEnabled : BIT;
Status_QuickStop : BIT;
Status_SwitchOnLocked : BIT;
Status_Warning : BIT;
END_STRUCT
END_TYPE
Declaration in POU
VAR
ControllerDrive1:ControllerData;
END_VAR
Bit access
ControllerDrive1.Status_OperationEnabled := TRUE;