Skip to main content

Bit Access in Variables

Important

Implement concurrent bit access by two tasks only if the processor can execute bit access directly on the memory. All x86 and x64 systems have commands for bit access in memory. Systems such as ARM and PPC cannot access bits directly in the memory.

If two tasks execute bit access simultaneously, even though the processor cannot perform bit access directly in the memory, then proceed as follows. Use a semaphore (SysSemEnter) or a similar technique to prevent competing bit access. However, it is best to execute the bit access within a task.

With index access, individual bits can be addressed in integer variables. Using a structure variable or a function block instance, individual bits can be addressed symbolically.

Index access to bits integer variables

You can address individual bits in integer variables. To do this, append the variable with a dot and the index of the addressed bit. The bit-index can be given by any constant. Indexing is 0-based.

Syntax

<integer variable name> . <index>
<integer data typ> = BYTE | WORD | DWORD | LWORD | SINT | USINT | INT | UINT | DINT | UDINT | LINT | ULINT
Example 197. Example

In the program, the third bit of the variable wA is set to the value of variable xB. The constant c_usiENABLE acts as an index to access the third bit of the variable iX.

Index access

PROGRAM PLC_PRG
VAR
        wA : WORD := 16#FFFF;
        xB : BOOL := 0;
END_VAR

// Index access in an integer variable
wA.2 := xB;

Result: wA = 2#1111_1111_1111_1011 = 16#FFFB

Constant as index

// GVL declaration
VAR_GLOBAL CONSTANT
        gc_usiENABLE : USINT := 2;
END_VAR

PROGRAM PLC_PRG
VAR
        iX : INT := 0;
END_VAR

// Constant as index
iX.gc_usiENABLE :=  TRUE;       // Third bit in iX is set TRUE

Result: iX = 4



Symbolic bit access in structure variables

With the BIT data type, you can combine individual bits into a structure and then access them individually. Then the bit is addressed with the component name.

Example 198. Example

Type declaration of the structure

TYPE S_CONTROLLER :
STRUCT
        bitOperationEnabled : BIT;
        bitSwitchOnActive : BIT;
        bitEnableOperation : BIT;
        bitError : BIT;
        bitVoltageEnabled : BIT;
        bitQuickStop : BIT;
        bitSwitchOnLocked : BIT;
        bitWarning : BIT;
END_STRUCT
END_TYPE

Declaration and write access to a bit

PROGRAM PLC_PRG
VAR
        ControlDriveA : S_CONTROLLER;
END_VAR

// Symbolic bit access to bitEnableOperation
ControlDriveA.bitEnableOperation := TRUE;


Symbolic bit access in function block instances

In function blocks, you can declare variables for individual bits.

Example 199. Example
FUNCTION_BLOCK FB_Controller
VAR_INPUT
        bitSwitchOnActive : BIT;
        bitEnableOperation : BIT;
        bitVoltageEnabled : BIT;
        bitQuickStop : BIT;
        bitSwitchOnLocked : BIT;
END_VAR
VAR_OUTPUT
        bitOperationEnabled : BIT;
        bitError : BIT;
        bitWarning : BIT;
END_VAR
VAR
END_VAR
;

PROGRAM PLC_PRG
VAR
        fbController : FB_Controller;
END_VAR
// Symbolic bit access to bitSwitchOnActive
fbController(bitSwitchOnActive := TRUE);