Masking bits with Step7

CBTussey

Member
Join Date
Dec 2003
Location
Lexington, NC
Posts
8
I am trying load a word from a data block and transfer it to an output byte, however I only want to write the first 4 bits of the db-word to the first 4 bits of the output word and mask the other 4 bits in the output word so I can use them elsewhere in the program without this load/transfer interfering with their previous state. Does anyone have any idea how I can do this? Or has anyone done something like this? I am using Step7 v5.2 and programming in STL. Does what I'm asking make sense? Sometimes it doesn't come out exactly like I'm thinking it!

Thanks in advance
 
I am trying load a word from a data block and transfer it to an output byte,

You are trying to load a 16-bit word into an 8-bit byte... hmmmm....

however I only want to write the first 4 bits of the db-word to the first 4 bits of the output word

However, you only want to copy the first four bits of the original double-word??? or double-byte-word??? into the first four bits of the output word... a 16-bit word?

My first comment is that you are pretty sloppy with your terminology. You would do yourself a great favor to clear up your thinking and use the right terms... it can only help you, if you do.

Now, If I read you right, I think you want to copy the first 4-bits from one word into the first four bits of another word. Is that so?

Let's try it with bytes...
(It works the same with words)

This is in generic terms rather than S7-specific.



Source Byte (V.b100)
7 6 5 4 3 2 1 0
+---+---+---+---+---+---+---+---+
| H | G | F | E | D | C | B | A |
+---+---+---+---+---+---+---+---+

AND MASK (V.b200)
7 6 5 4 3 2 1 0
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+

AND Result (V.b300)
7 6 5 4 3 2 1 0
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | D | C | B | A |
+---+---+---+---+---+---+---+---+

V.b300 = V.b100 AND V.b200


The source word V.b100 is still intact as...
7 6 5 4 3 2 1 0
+---+---+---+---+---+---+---+---+
| H | G | F | E | D | C | B | A |
+---+---+---+---+---+---+---+---+


.
Now, if you want to use the rest of the source word somewhere else,
the word is there and you can mask-off the source as you need to
draw out any other bits.

If you then want to acquire the other four bits...


Source Byte (V.b100)
7 6 5 4 3 2 1 0
+---+---+---+---+---+---+---+---+
| H | G | F | E | D | C | B | A |
+---+---+---+---+---+---+---+---+

AND MASK (V.b210)
7 6 5 4 3 2 1 0
+---+---+---+---+---+---+---+---+
| 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+

AND Result (V.b310)
7 6 5 4 3 2 1 0
+---+---+---+---+---+---+---+---+
| H | G | F | E | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+

V.b310 = V.b100 AND V.b210

...or any combination you want.


.
If you then want those bits (5-8), in V.b310, to be in the
first four bits (1-4), in V.b310, use Rotate.

7 6 5 4 3 2 1 0
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | H | G | F | E |
+---+---+---+---+---+---+---+---+

V.b310 = Rotate (1-nibble) V.b310


.
That is, Rotate V.b310 by 1-nibble and...
write the result back into V.b310

I can't remember if that will work at the byte level...
I think it will...
but it will certainly work at the word level which is where
you seem to be working... maybe... I'm really not sure.

If you only want to extract a single bit and place it into the LSB
position (bit-0) then create a single bit mask, perform the AND and
then use Shift-Right.

Anytime you want to do any kind of exercise like this, do it yourself
first, with a pencil and paper. Pay attention to what you are doing,
step by step. Then tell the processor to do the same thing.

But you must have your terminology straight! If you programmed the
routine in the same manner you asked the question... well, no wonder
it doesn't work.
 
This a the WORD command to do what you are asking.

L DB1.dbw0
L W#16#F
AW
T QW0
NOP 0

But I think you are looking at a BYTE. The easiest way for everyone to understand is to just use ladder logic. ( 4 Times )

DBx.dbwx q x.x
-----| |----------------------()
 
Thanks RRobbins! That's what I ended up doing, however I didn't know if there was a way to mask the bits I don't want to use. Thanks for the help.

Terry, I'm sorry I'm not smart enough to post a question intelligent enough to deserve a non-sarcastic reply from you, but in the future I'd appreciate it if you just didn't reply at all rather than insult my intelligence and bash my skills. All of us weren't born perfect, some of us are still learning believe it or not.

Thanks
Chris
 

Similar Topics

Happy Friday, I am trying to log scan errors related to Modbus Comms for a G306. I am able to use the system function GetDeviceStatus(x) to...
Replies
4
Views
1,925
I am trying to create a network setup that would take many devices that are working together and have them appear as one device or IP on a network...
Replies
3
Views
2,280
Hi all- So I took the Ethernet class at Rockwell Automation, and I still have the workbook to prove it. It touched on assigning subnet masks, a...
Replies
3
Views
2,241
Is there an instruction in RSL5K that would allow me to mask an array? Basically the same concept as using an MVM instruction on a DINT. It is...
Replies
3
Views
1,321
Hey guys, i am a little confused on this and was looking for some help. I was trying to configure a sequence output. Everytime i try and enter a...
Replies
13
Views
3,199
Back
Top Bottom