Sign in to follow this  
Followers 0
gromit

PLC5 logic to identify lowest of 5 values

18 posts in this topic

I'm looking for logic scheme to identify the lowest of five values. So, if F8:1=40.4, F8:2=20.2, F8:3=50.5, F8:4=30.3 & F8:5=10.1 I need logic to tell me which one is the lowest. . . effectively identifying F8:5 as the lowest. I will then use this point/bit to execute some logic.

Share this post


Link to post
Share on other sites
Seeing as they're all in sequential registers, I would use indirect addressing. Set a "pointer" integer to 1 Set a "lowest number so far" float to a number higher than the highest number you'll be comparing If F8:[pointer] is less than [lowest number so far] then move F8:[pointer] into [lowest number so far] Provided pointer is less than 5, increment pointer From there you can use a JMP and LBL to repeat the last two steps, which will perform all the comparisons in one scan, and then execute your logic after that. Or, you can just let it roll around and do the next comparison on the next scan, so the comparison will take 5 scans. If you use this method, you will need to block your following logic from executing until the pointer reaches 5. FYI I've not done indirect addressing on a PLC5 so I'm not sure of the syntax; what I've shown above is almost certainly not right, but it should at least give you the idea.

Share this post


Link to post
Share on other sites
Thanks ASForrest. My concern with indirect addressing is that if I implement it wrong it can fault the processor. So, I will see if I can find a spare PLC to load it on and test it first.

Share this post


Link to post
Share on other sites
Do you need to know that the value is in F8:5, or do you just need the value? If you only need the value, then just: ------------------------------ MOV F8:1 F8:10 LES F8:2 F8:10 -------- MOV F8:2 F8:10 LES F8:3 F8:10 -------- MOV F8:3 F8:10 LES F8:4 F8:10 -------- MOV F8:4 F8:10 LES F8:5 F8:10 -------- MOV F8:5 F8:10 If you need to know that it's F8:5, then use the same basic logic, but move an integer (1 - 5) into a N register and use that as a pointer.

Share this post


Link to post
Share on other sites
A sort instruction will do that for you. See pdf sortinstruction.pdf

Share this post


Link to post
Share on other sites
Huh, hadn't come across the sort instruction! Good call Mickey. Note to the OP - there is also a risk of faulting your processor if you set up your sort instruction wrong ;) JRoss's example is a much simpler and far less likely to fault the processor option. I tend to overengineer things like this sometimes because I tend to think things like "sure, this works for 5 values, but what if I had 500? How can I make this easily scalable and reusable?" So if it's a one off with 5 values, I've probably overthought it and the other two options here are probably much more suitable than mine ;)

Share this post


Link to post
Share on other sites
Yeah, great instruction! Although in this case he may not want to move the data around, for example if those float values are scaled analog inputs. He'd have to move the array to another area first. So yeah, for 500 values (or even 50) that would be a lot simpler to code.

Share this post


Link to post
Share on other sites
Wow, that's a lot of good info. Regarding JROSS's question, I need to know which one is the lowest. . . F8:5 for example. I'm really interested in the SORT command, so I will look into that further.

Share this post


Link to post
Share on other sites
http://www.plcs.net/downloads/index.php?PHPSESSID=mp7mb8p7hnp1ek0qiugmbksrl4&direction=0&order=&directory=Allen_Bradley This is a link to PLCS.net download area, there is a slc500 prog. to sort numbers. Steve

Share this post


Link to post
Share on other sites
I believe there are instructions you can add to the end of the 1st ladder file, LAD2, to keep the processor for faulting due to math overflow or divide by 0. Of course, if there is a condition that would generate the fault, you will now not know so program carefully.

Share this post


Link to post
Share on other sites
If you need to know which of the addresses holds the value, then the sort probably won't work, as it just reorders the values in place.

Share this post


Link to post
Share on other sites
Why not? After the instruction (sort) executes the first address is the lowest value. Just move that address to a temporary register (lowest value)

Share this post


Link to post
Share on other sites
If you need to know which of the ADDRESSES holds the lowest value... Say he's comparing five tank levels and wants to know which tank has the lowest level. A sort would rearrange the levels to give him the lowest value, but not tell him which tank it's from.

Share this post


Link to post
Share on other sites
HMMM? Right you are. With only five values an "Equal to" instruction(s) to compare it to the five tanks should take care if that. Edited by Mickey

Share this post


Link to post
Share on other sites
True.

Share this post


Link to post
Share on other sites
Can you post a screen shot of this logic? Maybe I'm making this way too hard in my mind

Share this post


Link to post
Share on other sites
Once the lowest value is determined, whatever method you use then compare it the a actual tank value.

Share this post


Link to post
Share on other sites
Very good. Thanks Mickey.

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