Binaire maths question

DeHulk

Member
Join Date
Mar 2003
Posts
74
I have a dataword ,each bit is actually an alarm, No i have to program a condition that shut down the installation when more than two bits are set at the same time.
I once saw a very short mathematical way of programming this but can't find it anymore .
Anyone has an idea ?
My regards
 
Originally posted by TimothyMoulder:

if integer > 1, fault

The OP is looking for a way to tell if any more that two bits are set (three or more). The above would indicate if any bits are set. I can't think of any fault structure where the failures are so evenly weighted that a group of any three faults would be a problem, but any two wouldn't be. But that is what was asked for.

I don't have any bright ideas but I'm guessing that a tweak to Peter Nachtwey's least significant bit finder would do the trick.

Keith
 
I'm not familiar with the S7 instruction set so I can't tell you the exact instructions to use. But every PLC I've ever used had a bit shift instruction. I think this solution is generic enough that you should be able to make it work.

First, use a move instruction to copy the alarm word into another word. Let's call this word "ExamWord". Use a "Bit Shift" instruction to sequentially examine all the bits in the "ExamWord". When a bit has a value of 1, use a counter to count it. At the end of each word examination cycle (after you've looked at all the bits), reset the counter to 0 and move the alarm word into "ExamWord" again. If you count 3 bits or more in any examination cycle, set your shutdown bit.
 
A brute force and inefficient way do do it in ladder logic.
Map each bit of the dataword to the first bit in an integer.
Then the integer will have a decimal value of either zero or one.
Add all the integers together and if value is > 2, set the alarm.


bit0 word0.0
---------] [-----------------------------( )--

bit1 word1.0
---------] [-----------------------------( )--



 
For all N>0, if N & (N-1) != 0, then at least two bits are set.

--------+---------+-
|SUB |
| N | //Subtract 1 from N
| 1 |
| A | //Store result in A
+---------+

--------+---------+-
|AND |
| N | //Bitwise And N and A
| A |
| B | //Store result in B
+---------+

ALARM
--+---------+---------{ )-
|NEQ |
| B | //If B != 0 then alarm
| 0 |
+---------+




------------------------------------------
edit:
Ooops, I should have read the post a little better, the OP was looking for MORE than two bits.

(N&(N-1)) & ((N&(N-1))-1)) != 0 should do the trick to test if three or more bits are set.



--------+---------+-
|SUB |
| N | //Subtract 1 from N
| 1 |
| A | //Store result in A
+---------+

--------+---------+-
|AND |
| N | //Bitwise And N and A
| A |
| B | //Store result in B
+---------+

--------+---------+-
|SUB |
| B | //Subtract 1 from B
| 1 |
| C | //Store result in C
+---------+

--------+---------+-
|AND |
| B | //Bitwise And B and C
| C |
| D | //Store result in D
+---------+
ALARM
--+---------+---------{ )-
|NEQ |
| D | //If D != 0 then Alarm
| 0 |
+---------+

 
Last edited:
Alaric has an interesting little trick there. It basically takes the lowest bit that is set and flips it off. To meet DeHulk's requirements, you would have to do it twice to alarm on more than 2 bits set, but it's still kinda cool. Doesn't get many points for being obvious though. Definitely requires a good rung comment.

I guess efficiency isn't everything. I like Ken Moore's solution. It may take more instructions, it may take more memory, but it's obvious. Sometimes brute force is good.

The bit shift method that juice76 suggests is good too. It has one advantage over Ken's method that would make me choose it if there were more alarms involved. It scales to multiple words of alarms better. Just keep in mind that it will take 16 scans to process a 16-bit word of alarms. It will be possible to miss a "more than 2 bits on" situation if it only lasts a few scans. If that is suitable for your situation, it's OK.
 
Last edited:
mellis said:
Alaric has an interesting little trick there. It basically takes the lowest bit that is set and flips it off. To meet DeHulk's requirements, you would have to do it twice to alarm on more than 2 bits set, but it's still kinda cool. Doesn't get many points for being obvious though. Definitely requires a good rung comment.

I noticed that the OP wanted MORE than two bits after re-reading the thread. I posted an edited suggestion above.

I agree its definitely not an obvious solution, and its even worse for the three bit solution. However DeHulk did want a mathematical way of solving it. It definitely needs a good comment. The operation should be much easier to program in ST on the S7.
 
Last edited:
The bit count instruction is ideal for this but I do not know if Siemens have it. Count the number of bits on in "n" channels, compare with number 2, if > fault output turns on.
 

Similar Topics

Hi Folks I hope this is not a dumb question,I am a newbie . I want to perform simple math functions like multiplication, addition etc in TIA...
Replies
6
Views
2,814
Hi all, I am trying to copy paste a routine from rs logix500 into rs logix5000, I get an error on rung where they are searching for scada values...
Replies
1
Views
1,852
Hi I am fairly new to PLC programming and I am self learning. I have written a simple program to detect and store the RPM and ultimately the...
Replies
0
Views
1,413
Hi All, Rockwell Compact Logix processor. I'm using an Add function that I trigger by a using an XIC instruction followed by a ONS. The add...
Replies
6
Views
2,105
Hi everyone, I am generally comfortable with AB PLC's and programming but have recently acquaired a job where I need to modify an Omron Cqm1h...
Replies
2
Views
3,719
Back
Top Bottom