Bit Masking (Filter) Example

Jordanx

Member
Join Date
Jun 2008
Location
Edmonton Alberta
Posts
3
Hey everyone,

I had another user account named Rubbs, but I changed ISP's and had to lose the email address I was registered under.
First off, I'd like to say thanks to everyone who participates in this site, its been a tremendous help to me over the last year.
I had a recent project where I had to do a simple task of masking out individual bits from 16 bit registers to read status's off a remote alarm station.
I thought that someone may like to have a quick easy example (maybe not the most code efficient, but easy to read) should they have to do a similar task in the future.

Without further ado:

Lets say you have 16 alarms out in the field that have been packaged into a single register. How do I remove them individually?

You need to mask (or filter) out each bit. A quick and easy way to do this is with two instructions. AND and EQUAL.
Forgive the formatting but a 16 bit register would look like:
0000 0000 0000 0000
Lets say you want the last, (least significant digit in most PLC's), to be represented in a single boolean or digital.

Again:
0000 0000 0000 000X
Where X is the digit you want.
The concept of an AND function is as follows:

0 AND 0 = 0
1 AND 0 = 0
0 AND 1 = 0
1 AND 1 = 1

Thus if you AND your field value against a 1, you will always receive the true representation of the field (switch, discrete etc) back into your resulting holding register.

So looking at a 16 bit register, what do I AND against my field value to get out individual bits?

You can simply use real whole numbers against your 16 bit register you read from the field.

Respectively as you increase each bit from a 0 to a 1 in the register you get a corresponding number.

0000 0000 0000 0001 = 1
0000 0000 0000 0010 = 2
0000 0000 0000 0100 = 4
0000 0000 0000 1000 = 8
0000 0000 0001 0000 = 16
0000 0000 0010 0000 = 32
0000 0000 0100 0000 = 64

See the pattern? The number doubles each time you move the 1 to the left of its last position.

Getting to the live example, how can I write actual ladder logic to extract each bit?

Using a simple example do the following:

[Register from Field] AND [MASK or Filter; 1] = [RESULT]
[RESULT] EQUAL [MASK] = [BOOLEAN]

Now in this example, your boolean will show the live value of the least significant digit in your 16 bit register.

Just remember that different PLC's can change the order of the bits so you may have to do some testing to determine in which way your machine interprets the register.

I have attached a simple picture to give you a better view of ladder logic, in case the ASCII drawing is confusing. There are lots of ways to do this, but sometimes you need just a simple and easy way to get started! After that the creative juices can start flowing.

example_mask.JPG


Hopefully this helps you guys out!
--Jordan
 
What Brand? 40001 looks like modicon to me

Does your PLC type allow moving a register into internal bits?

In GE 90/30 i just MOVE the register into M bits

Alternatively does it allow BIT TEST

Or simply XOR with the mask

Cheers
 
Agreed. A while back I had someone ask me how do simply remove a single bit from a register, and I had to explain the concept of how it was done.

This example was more to explain what the concept was, and how it can be applied in different situations.

In applications like GE and AB they have more advanced structures and instructions that can be used, but I find myself on a daily basis having to integrate ancient technology to a newer system.

In my project I was actually reading remote I/O from a Murphy TTDJ compressor panel into a newer display module and PLC, so using application specific instructions (like GE and AB) are not always possible.

--J
 

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,943
Basically all info in the pic. If I toggle the latch it comes back in.
Replies
9
Views
261
See the screenshot of EIP tag list. We are trying to read in a digital input that is hard-wired. It is shown here as I31.1. I believe we cannot...
Replies
7
Views
310
A couple days ago I accidentally toggled an alwasyoff bit. The issue is it was set up as a single OTU on a rung, nothing else, and used as XICs...
Replies
3
Views
249
Hi I have an old panel builder 32 that I’m replacing for a factory talk me hmi . In the plc for the old panel builder there is a coms bit for the...
Replies
0
Views
85
Back
Top Bottom