Acceso a bits en variables
Importante
Implemente el acceso a bits simultáneo por dos tareas solo si el procesador puede ejecutar el acceso a bits directamente en la memoria. Todos los sistemas x86 y x64 tienen comandos para acceder a bits en la memoria. Los sistemas como ARM y PPC no pueden acceder a los bits directamente en la memoria.
Si dos tareas ejecutan el acceso a bits simultáneamente, aunque el procesador no pueda realizar el acceso a bits directamente en la memoria, proceda de la siguiente manera. Utilice un semáforo (SysSemEnter
) o una técnica similar para evitar el acceso a bits competitivos. Sin embargo, es mejor ejecutar el bit de acceso dentro de una tarea.
Con acceso de índice, los bits individuales se pueden direccionar en variables enteras. Usando una variable de estructura o una instancia de bloque de funciones, los bits individuales pueden direccionarse simbólicamente.
Índice de acceso a variables enteras de bits
Puede direccionar bits individuales en variables enteras. Para hacer esto, agregue la variable con un punto y el índice del bit direccionado. El índice de bits puede estar dado por cualquier constante. La indexación se basa en 0.
Sintaxis
<integer variable name> . <index> <integer data typ> = BYTE | WORD | DWORD | LWORD | SINT | USINT | INT | UINT | DINT | UDINT | LINT | ULINT
En el programa, el tercer bit de la variable wA
se establece en el valor de la variable xB
. El constante c_usiENABLE
actúa como un índice para acceder al tercer bit de la variable iX
.
Acceso al índice
PROGRAM PLC_PRG VAR wA : WORD := 16#FFFF; xB : BOOL := 0; END_VAR // Index access in an integer variable wA.2 := xB;
Resultado: wA = 2#1111_1111_1111_1011 = 16#FFFB
Constante como índice
// 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
Resultado: iX = 4
Acceso a bits simbólicos en variables de estructura
Con el BIT
tipo de datos, puede combinar bits individuales en una estructura y luego acceder a ellos individualmente. Luego, el bit se direcciona con el nombre del componente.
Declaración de tipo de la estructura
TYPE S_CONTROLLER : STRUCT bitOperationEnabled : BIT; bitSwitchOnActive : BIT; bitEnableOperation : BIT; bitError : BIT; bitVoltageEnabled : BIT; bitQuickStop : BIT; bitSwitchOnLocked : BIT; bitWarning : BIT; END_STRUCT END_TYPE
Declaración y acceso de escritura a un bit
PROGRAM PLC_PRG VAR ControlDriveA : S_CONTROLLER; END_VAR // Symbolic bit access to bitEnableOperation ControlDriveA.bitEnableOperation := TRUE;
Acceso a bits simbólicos en instancias de bloques de funciones
En los bloques de funciones, puede declarar variables para bits individuales.
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);