Jordanx
Member
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.
Hopefully this helps you guys out!
--Jordan
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.
Hopefully this helps you guys out!
--Jordan