Comparing Bits

Jack

Member
Join Date
Jun 2003
Posts
22
I am telling a relay card to turn on in four different sequences. The outputs are tied back to an input card to verify contact closure. I tried using the NEQ statement to compare I/O but it returns an error saying it is to the word level. Is there a command that will let me compare bits?
Thanks
 
Supply a little more information on what you are trying to accomplish, what plc you are using, are there other things using the word you want to monitor and what are you wanting to compare the bits to?
I am not familiar with all PLCs.
Basically if you view your word isn't that giving you bit status for that word? As I said, above, you will need to supply a little more info.

Roger
 
XOR

If you are interested in just comparing whether two bits are NOT in the same state, try the following code:


A B
---+---| |---|/|---+---
| |
| A B |
+---|/|---| |---+


AKA - the exclusive OR.


If you are interested in all four bits, then you may want to manipulate the bit pattern at the word level (i.e., bet all four bits sets into two distinct words, and then do the NEQ that you tried.


It sounds like you are using an Allen Bradly PLC. If so, you may want to comapre the bits you are interested in using the MEQ instrucion. Keep in mind that the bits that you are comparing must be in the same bit positions for this to work.

But you can't do NEQ I:1/0 B3:0/0, the NEQ only works on words, not bits. You could try NEQ I:1 B3:0, but that looks at all 16 bits in each word, which is probably undesirable.
 
XOR with a mask is the way to go.

This assume the fours least signficant bits of I:1/0 must match the four least significant bits of O:0/0.


Code:
XOR I:1/0 O:0/0 N7:0       ; BIT THAT ARE DIFFERENT ARE SET
AND N7:1 0FH N7:0          ; SAVE ONLY THE BIT WE CARE ABOUT
EQU N7:0 0 OTE B3:0/0      ; TRUE IF ALL THE BIT WE CARE ABOUT ARE
                           ; EQUAL
 
Working with a AB SLC 500, I indexed the four output words as 11111111, 10101010, 01010101, and 00000000. I was hoping to use one routine to check the outputs via the input card. Allen, your code will tell me if the input card is working but my goal was to see that the output card matched what was commanded.

The index is in N9:0 through N9:3. I can do a compare that will tell me if they match or not but I want to break it down to track errors per relay.
 
Jack said:
Allen, your code will tell me if the input card is working but my goal was to see that the output card matched what was commanded

Allen's code WILL let you see that the relays are functioning. You just have to put the correct addresses in place of "A" and "B".

Replace "A" with your output address, and "B" with it's associated input address. You'll have 4 of these (one for each I/O check) and the rung will go true when the commanded output does not match the recieved status. And if you give each of these an output coil, you'll be able to track the individual relays.


| OUT 1 IN 1
|----] [------]/[----+----------( Relay 1 Failed )
| |
| OUT 1 IN 1 |
|----]/[------] [----+
|
| OUT 2 IN 2
|----] [------]/[----+----------( Relay 2 Failed )
| |
| OUT 2 IN 2 |
|----]/[------] [----+
|
| OUT 3 IN 3
|----] [------]/[----+----------( Relay 2 Failed )
| |
| OUT 3 IN 3 |
|----]/[------] [----+
|
| OUT 4 IN 4
|----] [------]/[----+----------( Relay 2 Failed )
| |
| OUT 4 IN 4 |
|----]/[------] [----+
|


.
Get it?

beerchug

-Eric

P.S. These are DISCRETE outputs, Peter... :p
 
Eric, you are correct, Allen's code will let me see if the outputs are closed and the inputs see them. But the purpose of the test was to command certain relays to close and then verify they did. If a problem on the card prevented the relay from closing, I would not know that.

But then Allen and Peter both put me on the right track with the XOR statement. What I ended up doing (and I *think* it works) was to

MOV
N9:[N7:0]-----N9:0 through N9:3 contain the output mask
O:2.0-----------N7:0 is the index and is bumped after timing

XOR
N9:[N7:0]-----This XOR's the commmand with what the input
I:1.0------------card sees and places the result in N7:3
N7:3

Then I examine the 8 bits in N7:3 one at a time and store them with an ADD command in my error counter N10:0 through N10:7. This keeps a total error count. Since the outputs are constantly sequencing, I do not need an different error check for each sequence. My relay card also contains an input word so I can check the coils in the same way.
 
There are a zillion ways to do this, and what you did should work, but I still don't see why Allen's code doesn't accomplish the same thing?... :unsure:

The benefit is that with Allen's method is that other guys who look at the code will quickly understand it's purpose. Your indirect addressing method might take some time to figure out (if not documented well).

Keep in mind that you'll need to delay your check until the relays have had time to change state (IOW, let them physically move). If you examine them too quickly, you'll have erroneous data.

beerchug

-Eric
 
Keep it simple and bit tricks.

Jack, since you are only comparing four words of inputs and outputs I would use my method four times and avoid the indirect addressing.

Jack, if you want to count bits use
Code:
count = 0; 
while ( bits  <> 0 )
{ 
  count = count + 1; 
  bits = bits and ( bits - 1 );
}
This is much faster for counting sparse bits.

Chess programmers are masters at bit manipulation.
 
I used the code that was suggested...

A and NOT-A
or
NOT-A and A


I was able to detect and count any errors induced by removing wires from the input card. This allows me to compare the relay state with the input card.

My confusion is how will this code detect a missing relay or a solder bridge between two relay driver pins. Since I am testing newly manufactured cards, problems like this are likely. If I get a solder bridge between the drivers for relays 1 and 2, and both relays come on when I am only commanding one, it will not return an error. I could go back and write four subroutines checking the command against the outputs with an AND statement for each closed relay. Hmmmmm, I'll see what I can do with that.

Thanks,
Jack
 
Jack said:
I used the code that was suggested...
If I get a solder bridge between the drivers for relays 1 and 2, and both relays come on when I am only commanding one, it will not return an error.

I am as confused as Eric. His code will do that.
 
Aha!...

Jack said:
I used the code that was suggested...

A and NOT-A
or
NOT-A and A

There's where the confusion lies... That's NOT the code that was suggested... (n)

It should read A and NOT-B OR NOT-A and B

In 'plain text', if "Commanded Output" (A) is ON and "Verification Input (B) is NOT ON -OR- if "Commanded Output" (A) is NOT ON, yet "Verification Input" (B) REMAINS ON, then the relay is not functioning properly.

Now maybe it will make sense... :)

beerchug

-Eric

P.S. I screwed up on my ladder listing a few posts back, the last 2 rungs' output coils should read "Relay 3 Failed" and "Relay 4 Failed", respectively... :oops:

Select, copy, paste, paste, paste, edit, whoops!... šŸ™ƒ
 
Eric, you are right. I knew what you meant but I just wrote it down wrong. Although I've been programming quite a while, I am new at the PLC programming. My specialty was assembly language but it seems it has been on the way out for a while. Programming micro-controllers has left me stingy with the code since most times I have only one or two K to work with. Anyway, I have not yet had a chance to rewrite the code and test it. I am sure it will do what I need. I appreciate the information everyone has given me.

Jack
šŸ““
 

Similar Topics

Hello, Im here to ask all you well learned professionals a few questions which im struggling with. Im a noob and trying to learn all i can. I...
Replies
10
Views
1,824
Hello all, i have a question for you guys, how can i compare bits, bytes, words and double words that are stored in data bock with the SCL...
Replies
14
Views
10,093
Iā€™m running a micro 820 to measure a tank level, then turning on equipment at certain levels. I have an analog input (4-20) that Iā€™m storing and...
Replies
10
Views
284
How do you go about implementing, on the M580 PLC, how many days, hours, minutes before a predefine event in the future? The RRTC_DT yields the...
Replies
3
Views
1,816
This is a Citect SCADA question I have Six variables of type REAL (Float) to compare and determine (identify) which variable has the highest...
Replies
4
Views
1,422
Back
Top Bottom