Condition Detection

learner

Lifetime Supporting Member
Join Date
Apr 2007
Location
British Columbia
Posts
81
Hi,

I have 10 bits in OR configuration. any of this bits could be on at anytime based on a certain condition. however I can't have two or more bits to be on at the same time. I am looking for a logic to help me detect the situation that two or more bits will turn the OTE on. In case this help, these 10 bits are Valid paths for a process but only one valid path at a time must exist.

Thank you
 
There are quite a few neat algorithms for counting true bits in a word out there, but since you are only looking for two or more, the simplest method would be a branch.

Move (probably with a mask), your 10 bits into a single integer, then write 9 branches driving a single "More than one bit set" output.

The branches will be (in AB speak):

Code:
 BST XIC CheckWord.00 GRT CheckWord 1 NXB
     XIC CheckWord.01 GRT CheckWord 2 NXB
     XIC CheckWord.02 GRT CheckWord 4 NXB
     XIC CheckWord.03 GRT CheckWord 8 NXB
     XIC CheckWord.04 GRT CheckWord 16 NXB
     XIC CheckWord.05 GRT CheckWord 32 NXB
     XIC CheckWord.06 GRT CheckWord 64 NXB
     XIC CheckWord.07 GRT CheckWord 128 NXB
     XIC CheckWord.08 GRT CheckWord 256 BND  OTE MultiBitsSet

You do not have to explicitly check for the 9th bit condition, as at that point it is the only one that can be set.

There are other methods, most involving a loop, or some 'magic', but if you don't need to know the exact count of bits set, the above is probably the simplest and quickest.
 
Thanks

Thank you for your quick reply. As simple as it looks I am a bit confused. I will go through what you mentioned step by step to see where I misunderstood you:

Move (probably with a mask), your 10 bits into a single integer:
Ok I can do that. I can use a rung for my bits to energize the Bits of an integer we call CheckWord. (XIC My first Bit---------> OTE CheckWord.00)

then write 9 branches driving a single "More than one bit set" output:
OK I understand this. No problem.

BST XIC CheckWord.00 GRT CheckWord 1 NXB
XIC CheckWord.01 GRT CheckWord 2 NXB
XIC CheckWord.02 GRT CheckWord 4 NXB
XIC CheckWord.03 GRT CheckWord 8 NXB
XIC CheckWord.04 GRT CheckWord 16 NXB
XIC CheckWord.05 GRT CheckWord 32 NXB
XIC CheckWord.06 GRT CheckWord 64 NXB
XIC CheckWord.07 GRT CheckWord 128 NXB
XIC CheckWord.08 GRT CheckWord 256 BND OTE MultiBitsSet

Am I supposed to see if CheckWord is Greater than 1 or 2 or 4 and so on and if it is then energize the output? if the answer is Yes then why you have a XIC in your code in front of for example CheckWord.00?

I'm sorry for this question I'm just a bit confused.

Thanks
 
It is really straightforward.....

Start with the first bit (bit 0).... If that is ON, (XIC True), it would give Checkword a value of 1, but only if no other bits are set. If the Checkword value is greater than 1, there must be another bit set in Checkword.

Each branch tests to see if Checkword has a value that isn't the result of the single bit being ON.

If this logic were extended to 16 bits (for an INTeger), or 32 bits for a DINT, then the GRT would not work, because the last bit would make the value of Checkword negative, so the GRT instructions would fail to see that bit on with another. I would use NEQ instructions instead of GRTs, and that would overcome the problem that the value can be negative.
 
Last edited:
Got it

OK. Thank you. I got it. This is a nice simple logic. I can use this for many bits as well. I can even use DINT instead of an INT and check the status of a lot more bits.

Thanks again
 
There is a more elegant way.

If you only need to know if more than one one bit is set, and not how many bits are set, then there is a simple bit hack that will tell you that.


MyDint <> (MyDint AND(NOT(MyDint-1)))


You can use it as an expression in a CMP instruction or build it out a SUB,NOT,AND, and NEQ instruction.

Here is an example from a state engine in one of my programs.

TC0818141.jpg
 
Just curious for the future cases, is there an instruction similar to that which can specify which bits or how many bits are on?

Thanks
 
Thanks for the Hint regarding the negative number on the last bit of an INT or DINT. Although the NEQ will definitely works, the GRT would also work because if the last bit of a DINT is set the value would be -2147483648. now if another bit in that DINT is set will actually make the value of the DINT greater than -2147483648 which serves the purpose.

For example if both bits.0 and bit.31 are on the DINT value would be -2147483647 which is actually greater than -2147483648. So it still works.

Thanks again
 
Thanks for the Hint regarding the negative number on the last bit of an INT or DINT. Although the NEQ will definitely works, the GRT would also work because if the last bit of a DINT is set the value would be -2147483648. now if another bit in that DINT is set will actually make the value of the DINT greater than -2147483648 which serves the purpose.

For example if both bits.0 and bit.31 are on the DINT value would be -2147483647 which is actually greater than -2147483648. So it still works.

Thanks again

No, the GRTs are comparing against the values produced when the bit that is being inspected is on with no other bits on.

Say bit 0 is on, that gives the DINT a value of 1... Then if another bit comes on, say bit 4, that will make the DINT have a value of 17. Clearly, in this case the GRT will work. But if bit 31 were on with bit 0, then the DINT value would be -2147483647, so the GRT would fail.

But I like the bit-hack suggested by TConnolly in post#6.

2014-08-19_081735.jpg
 
Last edited:

Similar Topics

I have a question about process interlock naming. I want to hear opinions, as well as if there is a relevant standard. When naming specific...
Replies
3
Views
666
Hello: Question for SCADA/HMI experts. One of our customers is responsible for planning and selection of key technologies for manufacturing...
Replies
12
Views
2,960
We have a repair hole that carriers come into. As they come in they pass a RFID reader and each carrier has a tag. The repair hole can hold 5...
Replies
2
Views
1,947
Hey Folks. At our plants we've been talking about monitoring the current on motors through the VFD parameters to gather a bit of historical /...
Replies
17
Views
3,735
All, is it possible for a compactlogix to have an overflow fault if the destination of calculation is REAL, the variables are REAL but being...
Replies
1
Views
1,356
Back
Top Bottom