Sign in to follow this  
Followers 0
waterboy

CMP Expression problem

17 posts in this topic

What is wrong with the following expression when used in a CMP instruction (logix 5k) (( ValueTag >= Hi_SP ) OR ( ValueTag <= Lo_SP )) Edited by waterboy

Share this post


Link to post
Share on other sites
is that because of "Hi SP" and "Lo SP" should be one word not two ?

Share this post


Link to post
Share on other sites
No, that was just an example tag name for the post. The real tagname is correct, and I corrected it in the post Edited by waterboy

Share this post


Link to post
Share on other sites
A CMP instruction can only contain one comparison operator, you have ">=" and "<=" in your expression which makes it invalid. Use parallel branches with a GEQ ValueTag Hi_SP on one branch, and LEQ ValueTag Lo_SP on the other. That arrangement will actually be faster execution than a single CMP instruction. If you want to be fancy use a single LIM instruction, where the LowLimit and HighLimit parameters are set to your Hi_SP and Lo_SP respectively (i.e. the LowLimit is higher than the HighLimit).

Share this post


Link to post
Share on other sites
The Help file lists <=, >= and <> as valid operators. Reducing the expression to < or > did not correct the error. Seems that adding the OR is the root of this evil. Its treating it like a new tag, When I browse and select the OR function it add is as OR( - with an opening paren. Must not be the OR I expect it to be?

Share this post


Link to post
Share on other sites
Exactly, this OR is a bitwise operator that takes two double integers and compare them bit by bit resulting in another DINT having a "1" in every position where either original operand has a "1" Some times this things become a true headache Bye

Share this post


Link to post
Share on other sites
Perhaps you missed the important part of my post.....

Share this post


Link to post
Share on other sites
I saw that. But show me where it says that in the help file. I thought you were referring to the doubleoperator.

Share this post


Link to post
Share on other sites
You are correct, it doesn't say so in the CMP instruction help. In fact the help seems to infer that you can combine multiple relationships..... But I know it to be the case, you can only have one comparison operator. Edited by daba

Share this post


Link to post
Share on other sites
I agree, the help does infer exactly that. I also agree it doesnt work. Since it highlights only the OR function, I was mislead... it seems. I'll see if Rockwell has a decent explaination why the docs dont spell that out.

Share this post


Link to post
Share on other sites
Does it work if you delete everything after the OR statement (including the OR)??

Share this post


Link to post
Share on other sites
Yes it does. but its useless for the intended purpose. The LEQ and GRT combination works fine - this has become educational at this point.

Share this post


Link to post
Share on other sites
You need LEQ and GEQ, not GRT, to maintain the context of your CMP expression that isn't accepted. I'm still racking my brains to remember how I learnt that only one comparison operator is allowed..... I started training for RA in 2007, and maybe it was in the course material then, but it isn't now, I've checked. Even so, I always make a point of it when discussing this instruction with trainees, on the basis that the expression evaluation ought to be able to deal with the sort of combinational logic you want. It definitely seems logical to me, but unfortunately I don't get a say in how instructions work, I just have to point out the good points, and the bad points. I still maintain that LIM is a better instruction to use... Whichever way round you configure the High and Low Limit values, the output of LIM will be true when the test value is equal to either of the hi and lo limit values.... i.e. reversing the limit values does not invert the output of the LIM.

Share this post


Link to post
Share on other sites
So, in a similar vein, could I program "CMP Tag_1=5 OR 10 OR 76345447"? The editor accepts it, but I'm not sure what it would do at runtime (and I don't have a handy PLC to check). Obviously what I'm aiming for is to check if Tag_1 is 5, 10, or ...that other number :)

Share this post


Link to post
Share on other sites
This is why I use structured text for compares...

Share this post


Link to post
Share on other sites
No, that would not work. The processor would firstly evaluate the bit-wise logical OR of 5, 10, and 76345447, then it would do the comparison once both sides of the comparison operator had been reduced to a single value. If you want to see if a tag is one of several values, you should place EQU instructions on branches.... Or you could perform a "lookup" using a File Search/Compare FSC instruction. The parallel branches would be more readable, and execute faster.

Share this post


Link to post
Share on other sites
Ah. OK, bitwise OR changes things. Parallel EQU's it is then. Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0