How can link BIT2 type to a variable?

906 Views Asked by At

In most input cards of Beckhoff, there are two variables, Limit 1 and limit 2. They have a BIT2 datatype, size of 0.2 bytes, which means they have two bits, as shown in the following figure for Limit 1:

Bit0: Value smaller/equal Limit 1
Bit1: Value bigger/equal Limit 1

enter image description here

so there are some questions: what kind of datatype should I define to link this variable to it, and how can I access both of its bits.

Any help would be appreciated.

3

There are 3 best solutions below

0
Roald On BEST ANSWER

You can use the BIT datatype in TwinCAT, but this is only available within a struct or a function block.

You can for example define a STRUCT as follows (see also InfoSys):

TYPE Limits:
STRUCT
    SmallerThanOrEqualTo : BIT;
    LargerThanOrEqualTo : BIT;
END_STRUCT
END_TYPE

It should be possible to link an instance of this struct to the variable.

Note that using BIT access can be a bit slower than using bit masks:

However, bit access takes significantly longer. Therefore, you should only use the data type BIT if you want to define the data in a specified format.

Toni Kucic did a speed comparison and found BIT access (13.5 ns) to be around a factor 5 slower than bit masks (2.8 ns). Full results:

codesys timing of bit access versus dot access

BIT datatypes do have the advantage that they are much more memory efficient:

A BIT element requires 1 bit of memory space, and you can use it to address individual bits of a structure or function block using its name. BIT elements, which are declared sequentially, are consolidated to bytes. This allows you to optimize memory usage compared to BOOL types, which each occupy at least 8 bits.

1
Jacek Domański On

I tried few things and I can't find anything, that you can link directly to Limit1. What you can do instead is to link a 16bit variable to Status, which Limit1 and Limit2 are parts of

enter image description here

Limit1 will be available at bits 2 and 3, Limit2 at bits 4 and 5. Just ignore the Status_2EB64646 type, as it is not an actual type (Or maybe it is in some library?) and use any 16bit variable. UINT worked for me.

0
DonMiguelSanchez On

I prefer to link them to an USINT data type variable, USINT is an 8-bit data type but allows me to have directly the numeric value

enter image description here

In order to link them just need to change some filters during the variable linking procedure: enter image description here

in the "Show Variable Types" section, select "All Types" and it will show every available variable from your plc project instance, after selecting the variable the following pop up will show up:

enter image description here

Confirm the standard values to avoid offset changes and then your variable will be linked!

i hope this approach helps someone.

Saludos Miguel