AT-Declaration
To bind a project variable to a flexible (*) or direct address, you can specify the address when declaring the variables. By using an appropriate variable name, you can give the address a meaningful name.
Automatic addressing It is recommended not to use direct addressing for allocated variables, but to use the * placeholder instead. |
Syntax:
<identifier> AT <address> : <data type>;
If an address is specified, the position in the memory and the size are expressed via special strings. An address is marked with the percent sign %, then follows the memory area prefix, the optional size prefix and the memory position.
%<memory area prefix> ( <size prefix> )? <memory position>
<memory area prefix> : I | Q | M
<size prefix> : X | B | W | D
<memory position> : * | <number> ( .<number> )*
Memory area prefix
I | Input memory area for inputs For physical inputs via input drivers ("sensors") |
Q | Output memory area for outputs Physical outputs via output drivers ("actuators") |
M | Flag memory area |
Size prefix
X | Single bit |
B | Byte (8 bits) |
W | Word (16 bits) |
D | Double word (32 bits) |
Examples:
IbSensor1 AT%I* : BOOL;
IbSensor2 AT%IX7.5 : BOOL;
If you do not explicitly specify a single bit address, Boolean variables are allocated byte by byte. Example: a value change of bVar AT %QB0 concerns the area from QX0.0 bis QX0.7. |
If you assign a variable to an address, you must observe the following:
- In the implementation editor, you cannot write-access variables that are assigned to an input with direct addressing. This leads to a compiler error.
- If you use AT declarations with direct addressing to structure or function block components, all instances use the same memory. This corresponds to the use of static variables in classic programming languages such as "C".
- The memory layout of structures is likewise dependent on the target system.
Valid addresses The keyword AT must follow a valid address. Further information on this can be found in the section Reference Programming > Operands > Addresses. Pay attention to possible overlaps in the case of byte addressing mode. |
Examples
Variable declarations:
IbSensor AT%I* : BOOL; | In the address specification, the placeholder * is specified instead of the memory position. This enables TwinCAT to perform flexible and optimized addressing automatically. |
InInput AT%IW0 : WORD; | Variable declaration with address specification of an input word |
ObActuator AT%QB0 : BOOL; | Boolean variable declaration Note: for Boolean variable one byte is allocated internally if no single bit address is specified. A value change of ObActuator consequently affects the range from QX0.0 to QX0.7. |
IbSensor AT%IX7.5 : BOOL; | Boolean variable declaration with explicit specification of a single bit address. Only input bit 7.5 is read during access. |
Other addresses:
%QX7.5 %Q7.5 | Single bit address of the output bit 7.5 |
%IW215 | Word address of the input word 215 |
%QB7 | Byte address of the output byte 7 |
%MD48 | Address of a double word at memory location 48 in the flag area |
%IW2.5.7.1 | The interpretation depends on the current controller configuration (see below) |
See also:
- Reference Programming > Operands > Addresses