S7 - addressing single bit in variable of type word

M

Thread Starter

Matt Haught

If I declare a variable in a function as type word, how do I address a single bit of that word to set/reset?
 
To set it you can make a word-OR with
2#0000001000000000 were the 1 is the bit you want to set

To reset make a word-AND with
2#1111110111111111 where the 0 represent the bit you want to reset.

If you want to use it as a real set/reset (not only a logical result) you have to add a conditonal loop in a way that you run the OR and AND instructions only when the set and reset conditions are fulfiled.
 
D

Daniel Chartier

Hello;

Any address in an S7 PLC memory area is accessible by bit, byte, word or double-word directly: this is what Siemens calls direct addressing. Just use the specific addressing format for that location.

For example, you use a input word 32 (IW32). If you wish to set/reset bit 4 of that word, use the following instructions (in STL, again as an example):

S I32.4 //set bit 4 of input byte 32
or
R I32.4 //reset bit 4 of input byte 32

This addressing format is valid for I(inputs), Q(outputs) and M(flag memory) Bits.

For a variable declared in a data block, for instance DB12.DBW24, acces bit no.4 as
DB12.DBX24.4

Have a look at the S7 online help (in Step 7) for more details.

Hope this helps,
Daniel Chartier
 
D

david mertens

If you are programming in SCL, then you can use views to access bits in a byte, word or double word.

e.g.
MYWORD : WORD;
MYBIT AT MYWORD : ARRAY 0..15 OF BOOL;

With this declaration, you define a view MYBIT on the variable MYWORD. The SCL compiler will not create any new variables for this, but inside your SCL program you can now access the individual bits with following construction:

VALUE := MYBIT[1];
or
MYBIT[1] := VALUE;
(with value of type BOOL)

to access bit 1 in the word.

Kind regards
 
G
One way of many to do it: Move the word to a temp register (LW 0 forexample). Set/Reset bit (L0.3 forexample). Move LW 0 back to word register. You must declare word as an in/out formalparameter in your function, if you want changes saved after function end.
 
just use bit addressing rather than word addressing e.g. to manipulate the first bit of MW0 just set/reset M0.0
 
G

Garrett, James P

>>SNIP
>> If I declare a variable in a function as type word, how do I address a
>> single bit of that word to set/reset?
>>SNIP

One method is to use a logical 'AND' mask to isolate the bit value:

0110 1101 - This is the variable
0010 0000 - This is the 'AND' mask value
----------
0010 0000 - This is the result of the AND test

Since the result is greater than 0, you know the tested bit was set.

-Jim
 
A
> just use bit addressing rather than word addressing e.g. to manipulate the first bit of MW0 just set/reset M0.0 <

M0.0 is not the first but the 9th bit in MW0, words in S7 are organised in the order : [lower byte (MB0)][higher byte(MB1)], this means that MW0 contains byte 0 on the left and byte 1 on the right, so M1.0 would be the first bit in MW0.

e.g. L 2#0000_0000_0000_0001
T MW0
will set bit M1.0 to 1, not M0.0 !!!

regards,
 
M
Maybe my question should be how to I access the local stack. For example:

I declare my input, output, in/out and temp variables.
In this variable declaration I have a temp variable called "MYWORD" of type word at address 6.0.
I can not use the address MYWORD.0
Is LW6.0 the correct syntax for bit 0 of MYWORD?
 
D

Donald Pittendrigh

Hi All

You can calculate the address of the word and rference any bit in it as a DBX but there is a better way. Instead of creating a word, create a struct and in the struct create 16 bool variables or an array of 16 bool variables,
then you address this data by its "word name" or "bit names".

Regards
Donald Pittendrigh
 
S

simone stefani

I.E. your input word parameter is IN_value

Try to use the code below

L P##IN_value //Load pointer to input parameter
LAR1 //Load AR1 register
U [AR1,P#0.4] //Test on TRUE 4th bit of input parameter
= OUT_Value //Notify the state of 4th bit

Bye...........

Simone Stefani
Elettrocommerciale S.r.l.
Divisione automazione
Mailto:[email protected]
 
Hi

Do you know how you can address the "word name" when you are using block that calls for word as data type. f.eks. comparison(CMP) won't accept struct as data type. Lets say you want to know if one of the bits get high, the statement: "word name"<>0 will not accept "word name" since it is struct not word.

Best regards,
Danath

>You can calculate the address of the word and rference any
>bit in it as a DBX but there is a better way. Instead of
>creating a word, create a struct and in the struct create 16
>bool variables or an array of 16 bool variables,
>then you address this data by its "word name" or "bit
>names".
 
Just a little correction: P#0.4 tests the 5th bit, not the 4th.

>I.E. your input word parameter is IN_value
>
>Try to use the code below
>
>L P##IN_value //Load pointer to input parameter
>LAR1 //Load AR1 register
>U [AR1,P#0.4] //Test on TRUE 4th bit of input
>parameter
>= OUT_Value //Notify the state of 4th bit
 
Top