IEC wins this round

ndzied1

Lifetime Supporting Member
Join Date
Aug 2002
Location
Chicago, Illinois
Posts
2,855
I have the need to find the max and min of three values. Using RSLogix 500 on a SLC 5/04 this becomes a little bit of a trick. I worked it out a couple different ways and came up with the code shown below.

After that, is the same finding of Min and Max with an IEC function block. Hands down this is easier!!!



Ladder Version - Max & Min of Three Registers
+--[GT]----+ +--[GT]----+ +--[MOV]---+
---+ Reg1 +-----+----+ Reg1 +----+ Reg1 +
| Reg2 | | | Reg3 | | Max |
+----------+ | +----------+ +----------+
|
| +--[LE]----+ +--[MOV]---+
+----+ Reg1 +----+ Reg3 +
| | Reg3 | | Max |
| +----------+ +----------+
|
| +--[LT]----+ +--[MOV]---+
+----+ Reg2 +----+ Reg2 +
| | Reg3 | | Min |
| +----------+ +----------+
|
| +--[GE]----+ +--[MOV]---+
+----+ Reg2 +----+ Reg3 +
| Reg3 | | Min |
+----------+ +----------+

+--[LE]----+ +--[GT]----+ +--[MOV]---+
---+ Reg1 +-----+----+ Reg2 +----+ Reg2 +
| Reg2 | | | Reg3 | | Max |
+----------+ | +----------+ +----------+
|
| +--[LE]----+ +--[MOV]---+
+----+ Reg2 +----+ Reg3 +
| | Reg3 | | Max |
| +----------+ +----------+
|
| +--[LT]----+ +--[MOV]---+
+----+ Reg1 +----+ Reg1 +
| | Reg3 | | Min |
| +----------+ +----------+
|
| +--[GE]----+ +--[MOV]---+
+----+ Reg1 +----+ Reg3 +
| Reg3 | | Min |
+----------+ +----------+

=========================================================
Function Block Version Max &

+--MAX----+
Reg1--+ +--Max
| |
Reg2--+ |
| |
Reg3--+ |
+---------+

+--MIN----+
Reg1--+ +--Min
| |
Reg2--+ |
| |
Reg3--+ |
+---------+

 
I would have coded the SLC a little differently

MOV Reg1 -> Max
If GRT Reg2, Max then MOV Reg2 -> Max
If GRT Reg3, Max then MOV Reg3 -> Max

Mov Reg1 -> Min
If LES Reg2, Min then MOV Reg2 -> Min
If LES Reg3, Min then MOB Reg3 -> Min
 
But what if there were 4 or 5 numbers?

How many parameters can the IEC min and max functions have? There must be a limit.

Code:
If reg1 > reg2 then
   max = reg1
   min = reg2
 
if reg1 <= reg2 then
   max = reg2
   min = reg1
 
If reg3 > max then
   max = reg3
 
if reg3 < min then
   min = reg3

This is only marginally better
 
Using indirect addresing and FOR loops (if available) may solve this problem in a more compact and flexible way. Even with way more than 3 variables.

They just have to be in a contigious block of memory, that's all.
 
A bubble sort would be simple to make in ladder with a counter, indirect addressing, and JMP-LBL pair to construct the For-Loop. Thus min and max could be easily found for any number of variables and the list could be arranged from largest to smallest, or vice-versa.
 
Oh NO! I didn't see that or did I?

Alaric said:
A bubble sort

Bubble sorts are very inefficient. There are better sorting algorithms like shell, heap, and quick sort.
http://linux.wku.edu/~lamonml/algor/sort/sort.html

Using any sorting method to find a min and max is very inefficient unless the number of items is small.

I would prefer to use ST for this:

max_reg := max(max(reg1,reg2),max(reg3,reg4))
max_reg := max(max(max(reg1,reg2),max(reg3,reg4),max(reg5,reg6),max(reg7,reg8))

This is very efficient as there is no need for counters and loops and the nesting can be as deep as required but after 3 levels it is probably best to use ONE for loop as Ladderlogic suggested above.
 
The inefficiency does not have any bearing on original statement which was "simple to make in ladder"

Ladder itself is not always efficient. As I usually program with Logix-5000 I would have also probably used ST, but the original post was with Logic-500.
 
Last edited:
Bernie,

You have a much better ladder answer than mine. I'll use it in the real application.

How many parameters can the IEC min and max functions have?

Interesting question. I originally used the older Moeller Sucosoft as my IEC example. The newer software (X-Soft) based on CoDeSys only has Min and Max functions w/ 2 parameters. This makes a lot more sense. In that case, the FBD would look like below. Not sure there is any functional limit to the number of blocks chained together. In any case, having to get min/max on three parameters is the perfect cunundrum as any more any you are likely to do it in an array using a loop.

CoDeSys FBD Max/Min

+--MAX----+ +--MAX----+
Reg1--+ +-----------+ +----Max
| | | |
Reg2--+ | Reg3-----+ |
+---------+ +---------+

+--MIN----+ +--MIN----+
Reg1--+ +-----------+ +----Min
| | | |
Reg2--+ | Reg3-----+ |
+---------+ +---------+




A bubble sort would be simple to make in ladder with a counter, indirect addressing, and JMP-LBL pair to construct the For-Loop
No where near as easily as as it could be done in ST.
 
Peter Nachtwey said:
How many parameters can the IEC min and max functions have? There must be a limit.

As far as IEC regulations go, I don't believe there is, unless specific vendors choose to limit them.
With ST, I have used many values inside MAX and MIN functions.

Paulus
 
I wouldn't use the SLC's limited instruction set as a way of justifying IEC. IEC, like many standards, is prone to following the lowest common denominator. Or to be cynical about it, what the manufacturers (who sponsored the development of the standard) could actually achieve without major changes to their existing product lineup. There are many more modern PLC's around now which have instruction sets that far exceed those in IEC1131-3.

Has this standard really made such an improvement to our everyday working lives?

I would much rather use a PLC that had a rich and varied instruction set that was optimized for the hardware rather than a IEC compliant editor that compiled ladder for a PLC with a limited instruction set. Maybe if the IEC compliant editor could compile programs for PLCs from different manufacturers, that would be different. But I'm not holding my breath for that.
 
Has this standard (IEC61113-3) really made such an improvement to our everyday working lives?

BIG NOT! Perhaps, only for the manufacturers and companies who profit from training.
First because the people need to have the aptitude to recognize, situations and to classify in elementary problems.
Therefore, a norm cannot be efficient if nomination of the problems does not exist

Post 03 -Peter Nachtwey
Not need = (equal) in last instrution?!

Which the real question?
  1. Max Min - Between the three values => Two results
  2. Máx Min - Of each combination => Six results
The CoDeSys FBD Max/Min is OK! Smaller
If more speed possible code use gates logic.

NAND REG1 REG2
IF(bit error overflow)
OFF is max=REG1 min=REG2
ON is max=REG2 min=REG1
NAND REG1 REG3
IF(bit error overflow)
OFF is max=REG1 min=REG3
ON is max=REG3 min=REG1
NAND REG2 REG3
IF(bit error overflow)
OFF is max=REG2 min=REG3
ON is max=REG3 min=REG2

Is get 01-Two results for 02-six results rename max min not common.
 
Last edited:
ndzied1 said:
A bubble sort would be simple to make in ladder with a counter, indirect addressing, and JMP-LBL pair to construct the For-Loop
No where near as easily as as it could be done in ST.

No arguement there.

Could you just imagine having to try and construct a b-tree or something like that in ladder? 🔨
 
Alaric said:
As I usually program with Logix-5000 I would have also probably used ST, but the original post was with Logic-500.
Why not use RSLogix5000's File sort (SRT) instruction?
(Also available in PLC5's)
 

Similar Topics

I am trying to deal with fragmented data, and have been getting stuck. So far I am able to command the Fuchs VE Software version correctly and I...
Replies
0
Views
160
Hi all, I am getting back to using IEC for schematics and was wondering about the 81346-2:2019 codes. What does everyone use for a diffuse...
Replies
3
Views
550
I just took a skim through 81346-2:2019 and was wondering which codes you all used for Safety devices like Safety Controllers, EStops, and guard...
Replies
0
Views
550
I know this is a PLC forum but there are many people from around the world that might be able to help me. I have an aspect question (IEC). I am...
Replies
18
Views
1,845
Hi everyone, I'm working on a Mitsubishi Q series PLC whose code was developed in Mistubishi's GX IEC Developer v7.04. I looked online for GX IEC...
Replies
9
Views
1,446
Back
Top Bottom