Programming in List on a Mitsubishi PLC – Part 2.pdf

download Programming in List on a Mitsubishi PLC – Part 2.pdf

of 27

Transcript of Programming in List on a Mitsubishi PLC – Part 2.pdf

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    1/27

    - 1 -

    Programming in List on a Mitsubishi PLC Part 2- by Jim Rowell - 2004/02/10

    Welcome back! Last time, we pretty much ignored ladder diagrams. This time I'm giving

    you lots of them. We'll look at the ins and outs of turning those pretty pictures into

    powerful text. You'll gain insight into the block commands and the various structures thatcan be built using List. You'll leave with full knowledge of the perils and pitfalls.

    We'll be using numbered as opposed to named I/O for the examples. Dont get used to itand definitely don't be discouraged by it. Numbers work best for showing you how the

    code works but they're terrible at showing you what it's actually doing. Your own code(with proper I/O names) will be much easier to follow.

    Experienced programmers will find the initial stuff a bit on the basic side but please, keep

    reading. It gets better. The more advanced topics will rely upon terminology and phrasesintroduced in the basic ones so I suggest you don't skip. I can pretty much guarantee

    you'll find a knowledge nugget or two somewhere in the following pages (double yourmoney back, void where prohibited).

    Our focus will be on the Mitsubishi FX series but the concepts apply more or less on all

    plc's. Before using anything you learn here though, do yourself a favour by checking yourmanuals and testing your code. Take anything I say (or anyone else says) with several

    grains of salt. My mistakes don't have to be yours too.

    The Really Easy StuffOkay, let's see how List compares with Ladder while writing some code.

    Feel free to try any of the examples in your editing software. Just be aware that if youswitch between List and Ladder views without having an output on each rung (most of

    the examples don't), you'll receive an error. All you need to do is add an output to the endof the List code. AllRighty then...

    We'll start at the beginning of a rung (good choice huh?).

    First we type: LD X0

    X0

    No surprises there. As you can see, we have begun a new rung with a normally open

    contact. We always have to start with the LD (or LDI) command.

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    2/27

    - 2 -

    Now let's add: AND X1

    X

    0

    X

    1

    The AND command places a contact in series with all of the preceding logic. So far "all"consists of that single X0 contact we previously placed.

    You can perform AND as many times as you like and every new contact will be placed inseries with all of the preceding logic. That's because when the plc reads a contact, all ofthe preceding logic has already been reduced to a single result held in the accumulator

    (remember? Part 1?). The plc is simply going to perform a logical AND with theaccumulator and your new contact.

    AND X2

    AND X3

    X0

    X1

    X2

    X3

    No mystery there, right?

    At this point, the accumulator holds the value of the output of X3.

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    3/27

    - 3 -

    Now try: OR X4

    X0 X1 X2 X3

    X4

    Whoa! What the heck happened there? Unlike a logical AND, an OR command will

    place a contact in parallel with all of the preceding logic or in other words, in parallelwith your entire rung. We're back to that accumulator thing again. The plc performed a

    logical OR with the accumulator and your new contact.

    AND X5 ; places a contact in series with all of the preceding logic.

    X0

    X1

    X2

    X3

    X4

    X5

    The accumulator now holds the value of the output of X5.

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    4/27

    - 4 -

    OR X6 ; places a contact in parallel with all of the preceding logic.

    X

    0

    X

    1

    X

    2

    X

    3

    X4

    X

    5

    X6

    We could go on forever. I'm told I do sometimes.

    Really Smart PLC Rule #1:

    AND, ANI, OR & ORI commands always operate on all of the preceding logic.ie. They are always in series or parallel with the entire rung up to the current point in the

    logic.

    So Far So GoodIf you've followed along patiently to this point, you will probably agree that so far it's

    embarrassingly simple. After all, you're pretty smart. I guess it's time to tackle the thingsthat are not so obvious. First off, one of the most common things you'll encounter is the

    need to place a few contacts in series with each other followed by some contacts that arein parallel with each other but still in series with the first ones.

    Like this:

    X0

    X1

    X2

    X3

    X4

    X5

    X6

    X0 to X3 are in series. X4 to X6 are in parallel with each other and, as a group, are alsoin series with X0 to X3.

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    5/27

    - 5 -

    You simply can't do this in List using nothing but your basic AND and OR. In Part 1, we

    talked about using the ORB ("Or Block") and ANB ("And Block") commands. We'd haveto use them here.

    Something like:

    LD X0

    AND X1AND X2

    AND X3

    LD X4OR X5

    OR X6ANB

    Note: Do a quick review of Part 1 if you don't understand the use of ANB... or what the heck, just read a bit further.

    This is where people start to complain. Such a simple construct in Ladder yet in List weare already having to resort to using 2 separate blocks of code joined by ANB. (the

    complaints are occasionally followed by rioting in the streets, looting, setting of fires,etc).

    Since this is in fact a common logic sequence, it's worth letting you in on a little secret.

    Although you have probably drawn exactly such a diagram in Ladder and would be justas likely to build it that way using relays, as far as the plc is concerned, you've been

    doing it backwards all along. Huh? Yup. Try putting the OR'd contacts first and see what

    happens.

    Like this:

    X4

    X5

    X6

    X0

    X1

    X2

    X3

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    6/27

    - 6 -

    Now write that in List and you get:

    LD X4OR X5

    OR X6

    AND X1AND X2AND X3

    See? No ANB. You just made it a lot more readable. You also made it shorter. It now

    requires less memory to store the program and you made it execute faster. Even moreimportant, you learned one of the reasons why List might be a good idea. Using Ladder,

    you tend to think like a human (and that can't be good). If you are using List, you seewhat the plc sees and you have a better opportunity to write code that is efficient. Before

    anyone gets upset with me, I will quickly add that the efficiency of code is usually notthat critical compared to readability, maintainability and other factors. On the other hand,

    it's never a bad thing. There are times when efficiency is very important.

    Everyone tends to make this AND before OR mistake. The good old 3-wire start/stopcircuit taught in every electrical class is probably to blame. A good controls electrician

    has this seared into his brain. The series-wired stop buttons always came first followed bythe parallel-wired start buttons & holding contacts. But now we are inside the plc.

    In a plc, we need to take advantage of Rule #1 rather than being ruled by it. Thefollowing new rule helps us do just that.

    Really Smart PLC Rule #2:If a string of logic contains a setof series contacts and a set of parallel contacts:

    add the series set last if you want the two sets to be in series;add the parallel set last if you want the two sets to be in parallel.

    Note: This is really just an expansion of rule #1.

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    7/27

    - 7 -

    In the previous example, we avoided the ANB by putting the series set last. We had a setof series contacts and a set of parallel contacts and we wanted them in series with each

    other. Exactly what Rule #2 said to do. If we had wanted them in parallel we would havereversed it like this:

    X0

    X1

    X2

    X3

    X4

    X5

    X6

    ANB/ORBSooner or later we will have to resort to ANB/ORB. So let's do some practice with those.

    We'll minimize their use by keeping our Really Smart PLC Rule #2 in mind.

    First of all, remember that using LD will always begin a new "block" of code (what wecalled a "series" of statements in Part 1; "block" is the official, tech term). In effect,

    using the LD command in the middle of a rung suspends the logic you are working onand temporarily begins a new rung at the left rail. You end up with two blocks; the old

    unfinished one and a new temporary one. The reason we want to do this is so that whensomething applies to "all of the preceding logic" it will only be referring to what's in this

    new block. For now, the old block will be ignored. When you are ready, the new one can

    be connected to the old by using ANB or ORB.

    Pictures! We need pictures!

    LD X0 ;start block #1

    OR X1

    produces this:

    X0

    X1

    Block

    #1

    LD X0AND X1

    AND X2AND X3

    OR X4OR X5

    OR X6

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    8/27

    - 8 -

    Now let's add another LD statement (to get a new block) followed by OR.Altogether we have:

    LD X0 ;start block #1.OR X1

    LD X2 ;start block #2.OR X3

    X0

    X1

    X2

    X3

    Note that both blocks were left "open" (ie. there are no OUT or other output type

    commands that would produce a connection to the right hand rail).

    If we add an ANB at the end of our code, the two blocks will be placed in series like this:

    X0

    X1

    X2

    X3

    ANB

    Block

    #1

    Block

    #2

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    9/27

    - 9 -

    If we instead use an ORB, we'll parallel them like this:

    X0

    X1

    X2

    X3

    Using ORB in this example was rather pointless. We could have just OR'd all thecontacts together in the first place and avoided the use of ORB.

    Here's a better example showing the use of ORB:

    LD X0 ;start block #1.AND X1

    LD X2 ;start block #2.

    AND X3

    X0

    X3

    X2

    X1

    ORB

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    10/27

    - 10 -

    Now add ORB and you'll get:

    X0

    X3

    X2

    X1

    Common sense might have told you the last drawing could be coded as

    LD X0AND X1

    OR X2AND X3

    when in fact, that would have produced this:

    X0

    X3

    X

    2

    X1

    Just remember that common sense will also insist that the world is flat.So if you're not exactly giving Ptolemy a run for his money, this would be a good time to

    go back and review (starting with Part 1). Assuming you are happy with the concept of aspherical planet, let's ride forth

    When do you need ANB/ORB?

    Really Smart PLC Rule #3:Use ANB/ORB when you need to apply logic that only operates on someof the preceding

    logic.

    Identify the logic section of interest and then separate it out by surrounding it with LD &

    ANB/ORB.If you need to do an AND but only want the contact to be in series with someof thepreceding logic, you need an ORB.If you need to do an OR but only want the contact to be in parallel with someof thepreceding logic, you need an ANB.

    ORB

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    11/27

    - 11 -

    Example of AND needing an ORB:

    X0

    X3

    X2

    X1

    We only want X3 to be in series with someof the preceding logic(ie. in series with X2, but not X0 or X1).

    Here it is in List:

    LD X0

    AND X1

    LD X2 ;marks the start of the "some" stuff.AND X3 ;this AND tells us we need an ORB.

    ORB ;places the X2-X3 block in parallel with the X0-X1 block.

    Example of OR needing an ANB:

    X0

    X1

    X2

    X3

    X4

    We only want X4 to be in parallel with someof the preceding logic(ie. in parallel with X3, but nothing else).

    Here it is in List:

    LD X0OR X1

    LD X3 ;marks the start of the "some" stuff.OR X4 ;this OR tells us we need an ANB.

    ANB ;places the X3-X4 block in series with the X0-X1 block.

    AND X2

    ORB

    ANB

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    12/27

    - 12 -

    Notice I didn't include the 'AND X2' until after the ANB. I left it until the end as a

    separate operation. I didn't have to really but Rule #2 says I should. In more complexcode, it'll help keep things clearer if you develop this habit. Keep the contents of blocks

    you'll be joining as short and simple as possible.

    Okay, our next example is going to get nasty. I can almost guarantee your response will

    soon be "Holy Cow! This just isn't worth it" (actually, you may have been thinking thatfor a while now). I'll be the first to admit that learning to write clear, readable code is

    easier if "Ladder only". So why bother with all of this? Well, once you get used to it,you'll find that List has the following strengths:

    1) It's quicker to type than it is to draw (and in Ladder you still have to fill in namesand constants anyway).

    2) Comments, names and constants are clearer and more readable.3) You can see more of your program at one time on the screen and have more

    control over the layout.4) Printouts are more compact and can be edited in any word processor quite easily.

    5) More efficient code. Faster execution. Less memory required .6) Complex functions are more readable.7) Women will find you more attractive.

    None of these will make much of an impression (well, maybe the last one), until you start

    to use a lot of advanced functions. Let me give you an example. I recently wrote aprogram to control a rolling tube bender. There was nothing extraordinary about it yet

    there were sections that contained over fifty math functions in a row. In Ladder, theseappeared as endless horizontal lines, each one ending way over on the right-hand side of

    the screen in a box full of misarranged numbers. In List, it was very clear and obviouswhat was going on. It looked and read more like proper math. It's also worth pointing out

    that although the program was over 3000 steps in length, it contained only 15 block joinsand a single MPS/MPP pair. That's because it was written in List. You shouldn't judge

    List by the mess you sometimes see when you auto-convert Ladder into it.

    Regardless of the type or level of programming you are doing today, chances are you'llbe doing different things tomorrow. Being able to comfortably switch between List and

    Ladder will allow you to take advantage of the strengths of each.

    Personally, I write everything in List and use Ladder to double-check sections that don't"feel" right. I get a second perspective from Ladder that can be invaluable. You may end

    up using the opposite approach or perhaps you'll write Boolean in Ladder and switch toList for math and similar functions. Regardless, if you wish to have List available to you,

    you need an understanding of its features and rules. Besides, I'll be really ticked if afterall the typing I just did, you decide it would be a great time to try out the delete key on

    your new keyboard.

    So, assuming both you and this document still exist

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    13/27

    - 13 -

    Let's try a complex example.

    First we'll translate the ladder diagram shown below exactly as it's drawn. We'll even doit in the order suggested by the contact numbers (X0, X1, X2, etc).

    X0

    X1

    X2

    X3

    X6

    X7

    X4

    X10

    X11

    X5

    Y0

    Here's the List:

    LD X0

    AND X1

    LD X2AND X3

    ORB ; see J1 (joins the X2-X3 block with the X0-X1 block).

    LD X4AND X5

    LD X6

    AND X7

    LD X10OR X11

    ANB ; see J2 (joins X10-X11 block with the X6-X7 block).

    ORB ; see J3 (joins the block created by J2 with the X4-X5 block).

    ANB ; see J4 (joins the block created by J3 with the one created by J1).OUT Y0

    You'll note that we held off using some of the block commands until the end. We had toin order to join blocks in the correct order. ANB/ORB always operate on the two most

    recently created blocks. You are allowed to have several blocks outstanding before youhave to perform a join. The FX series allows a maximum of eight. Doing it this way, youhave to be careful not to join the wrong blocks together. It's also rather confusing.

    J2

    J4

    J1

    J3

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    14/27

    - 14 -

    By re-arranging things, we could have kept most of the block commands "inline" and

    made things a little easier to read (and less error prone). This is a much better technique.

    Like this:

    LD X0AND X1

    LD X2

    AND X3ORB ; see J1 (joins the X2-X3 block with the X0-X1 block)

    LD X6

    AND X7

    LD X10OR X11

    ANB ; see J2 (joins X10-X11 block with the X6-X7 block).

    LD X4AND X5

    ORB ; see J3 (joins the X4-X5 block with the one created by #J2).

    ANB ; see J4 (joins the block created by #J1 with the one created by J3).OUT Y0

    We still have an area that could be improved. Have a close look at X6, X7, X10, & X11

    on the drawing. Look familiar? Remember our Really Smart PLC Rule #2? Oh, youalready saw that and were just being polite? I'm impressed. The rule tells us that by

    placing X6 & X7 after X10 & X11 we can eliminate the J2 block command. Very nice.

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    15/27

    - 15 -

    Here's the final version (you don't really believe that do you?).

    LD X0AND X1

    LD X2AND X3ORB ;see J1 (joins the X2-X3 block with the X0-X1 block).

    LD X10

    OR X11AND X6

    AND X7 ;we no longer need the block join at J2.

    LD X4AND X5

    ORB ;see J3 (joins the X4-X5 block with the X10-X7 block).

    ANB ;see J4 (joins the block created by J1 with the one created by J3).OUT Y0

    This version will work the same as the other two but they'll each produce different

    diagrams in your Ladder editor (go ahead, fire up Medoc, GX or whatever and play).

    Notice the blocks joined by ANB both end in ORB and the blocks joined by ORB all endin AND. This tells us we've done a good job (according to the next rule).

    Really Smart PLC Rule #4:

    Both blocks being joined by ANB should end in OR, ORI or a block join (ANB/ORB).Both blocks being joined by ORB should end in AND, ANI or a block join (ANB/ORB).

    Notice it says that both of the blocks being joined must qualify. That's a key point.

    Breaking Rule #4 doesn't make your code illegal. It just means you haven't written it inthe best possible way. You might be using a block join when it's not needed. You could

    even be including an AND that would be better added afterthe join is completed. Therule is unforgiving. Also note it doesn't recognize LD as a proper block end so you know

    at least two contacts are needed in each block you are attempting to join.

    If we had followed this rule in the first place, the last version of our example would haveappeared automatically without requiring the two revisions. Interestingly enough, Rule #4

    even insists that you obey rule #2. Could be a conspiracy.

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    16/27

    - 16 -

    There isstillthe issue of that final ANB in our last version. A bit confusing isn't it?When quickly scanning the code, you have no idea what that ANB is referring to.

    Here's a method that is a lot more readable (never believe that "final" version stuff).

    LD X0AND X1

    LD X2

    AND X3ORB ;combine this block with the previous one.

    OUT M0 ;assign an internal relay to record the result so far.

    LD X4 ;start over fresh.

    AND X5

    LD X10

    OR X11AND X6AND X7

    ORB ;combine this block with the previous one.

    AND M0 ;add in the result from way back.

    OUT Y0

    In the real world, you'd give M0 a nice, descriptive name so that when you use it, you'llknow exactly what it refers to. You can't give ANB a name at all. It's well worth flipping

    back to the page with the first version and comparing. There's a pretty dramatic

    difference isn't there? Which one would you rather work with five years from now whenyou've forgotten what this machine even looks like?

    OUTPUTS

    We haven't yet dealt with outputs in Part 2. It's hard to write a program that does anythingwithout them. It's even harder to use them properly unless you know a few tricks. I'll now

    take you to the grand heights of advanced output design. Or not

    There are three types of output connections that one can make.I call these Simple, Cascaded and Divergent outputs.

    So, you're saying to yourself "Oh Gawd. He's making up terms and definitions now. He

    just wants this to be complicated" (yeah, I heard you). There is a purpose here. Manypeople, when writing in List, don't even realize that cascaded outputs are legal. Also, if

    you don't know the difference between cascaded and divergent outputs, you'll often createdivergent ones by mistake. And divergent outputs can lead to horrors such as MPS which

    should be avoided like prison time or processed cheese.

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    17/27

    - 17 -

    Simple OutputsYou have a simple output when you have only one or, if there are several, they are

    directly connected together. There are no contacts between any of the outputs. Nothingmysterious here.

    X0

    Y0

    Y1

    Y2

    Cascaded OutputsYou have cascaded outputs when you have more than one output and each uses all of thelogic of the preceding output and then extends that logic with more of its own.

    X0

    Y

    0

    Y1

    Y2

    X1

    X2

    X1 extends the logic of Y0. X2 extends the logic of Y1. Note the extension logic leading

    to each lower coil from the upper coil connects directly to the upper coil. There are nocontacts between that connection point and the upper coil (see arrows).

    The logic leading up to a set of cascading outputs can be built without resorting to

    anything other than LD/AND/OR, etc and, if needed, ANB/ORB. The extension logicadded for each new output is built using only AND/ANI. (once you place even 1 output,

    you are no longer allowed to use ANB/ORB). In reality, you shouldbe able to also useOR in the extension logic but the ability has been disabled in the plc. For a look at why,

    see Water Flows Downhill(in the Other Stuffsection at the end of this document).

    Note: There are NO contacts at these points

    LD X0

    OUT Y0

    OUT Y1

    OUT Y2

    LD X0

    OUT Y0AND X1

    OUT Y1AND X2

    OUT Y2

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    18/27

    - 18 -

    Divergent OutputsYou have divergent outputs when you have more than one output and each uses only

    someof the logic of the preceding output. Each output may, but is not required to, extendthat logic with more of its own (remember this last part, Rule #6 is all about it).

    X0

    Y0

    Y1

    Y2

    X1

    x

    2

    x4

    x3

    Note that X3 is sitting between the higher coil (Y0) and the connection point leading to alower coil (Y1). Y1 does not use all of the logic that Y0 does. Y1 totally ignores X3.

    The output of the X0 contact is what I call a "point of divergence". It's like a fork in theroad. The logic splits into 2 different routes. Each half of the fork ends in a coil that is not

    seen by the other.

    So what? Well picture the plc merrily processing everything in your program up to andincluding X0 in the normal manner. Then it looks at X3 and outputs Y0. Everything is

    rosy. Remember, at this point (like all points) none of the preceding logic exists anymore.All we have is the current result in the accumulator. This result includes the effect of X3.

    Unfortunately, we still have to process the logic leading up to Y1 but that's notsupposedto include X3! Basically, we now need the plc to back up a bit (to the point of

    divergence) and then execute the branch to Y1. Hmmm. Sorry... plc's don't do that.

    Really Smart PLC Rule #5:PLC logic never flows backwards.

    In order to build a set of divergent outputs, you have to use MPS (Memory Point Store)

    or some other method of saving the value of the accumulator when you come to the placewhere the multiple output paths separate (the point of divergence). Later, when it's time

    to execute the second branch, you can recall that value from storage and use it to modifythe accumulator. Whenever you see a point of divergence in your code, you know you

    need to stick an MPS there (or similar). This is the only time MPS is used in a program.

    There MUST be contacts here

    Points of divergence

    There MAY be contacts here

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    19/27

    - 19 -

    The Evil MPSI'm not going to go into too much detail on MPS since it's all in the manual. If you don't

    understand it, don't worry about it; use the alternate method described below. Let's justquickly say this: If you write MPS, the plc will store the value of the accumulator at that

    point in the program. MPS operates on a stack that is 12 storage locations deep (in the FX

    series). Each time you write MPS (Memory Point Store), you shove the previous valuesdeeper into the stack. You read the topmost value with MRD (Memory Read). This loadsthe accumulator with the value read. If instead you read the stack using MPP (Memory

    Point Pop), you load the accumulator with the current value and then bring the next valueup to the top. Subsequent uses of MRD will read that "next value". You must completely

    empty the stack before leaving a rung or you'll generate an error. In other words, forevery MPS there must also be a MPP. You can have any number of MRD's.

    Having said that, you shouldn't use MPS except in very obvious circumstances. All too

    often, you'll see MRD or MPP used many lines below the MPS. Now you have to searchup through the code to figure out what is being referenced. A better method is to assign

    an internal relay (eg. OUT M0) to that point in the logic instead of using MPS at all. Endthe rung right there and start over using a contact from the internal relay. You can now

    give that relay a very descriptive name and refer to it as often as you wish withoutworrying about matching MPP's with MPS's. Your rungs will be shorter and simpler too.

    The main reason that MPS exists is for use by the Ladder editing software itself. When

    you write in Ladder, the editor must convert your code into List because that's what getssent to the plc. The editor's conversion makes grand use of MPS (especially since unlike

    you, most people don't know about divergent versus cascaded outputs). It needs atemporary method like MPS because it's not at liberty to use internal relays (they belong

    to you) and it would do a lousy job of naming them anyway.

    The Point of all of thisSimple outputs are obvious. They'll usually take care of themselves. Cascaded outputs are

    nice. The code looks good and it executes quickly. Divergents are not really devil spawnbut we do want to know when we've made them by mistake. If you start looking, you'll be

    surprised how many times they could have been written as cascades with just a little bitof effort. Especially when written in Ladder. Which brings us to:

    Really Smart PLC Rule #6:

    If a divergent output does not have logic between itself and the point of divergence, it'san indication that things could have been re-ordered as a cascade.

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    20/27

    - 20 -

    Proper divergent outputs.

    X0

    Y0

    Y1

    X2

    X1

    It codes as:

    LD X0MPS

    AND X1OUT Y0

    MPPAND X2

    OUT Y1

    Divergent outputs that should be rewritten as cascaded:

    X0

    Y0

    Y1

    X1

    It codes as:

    LD X0MPSAND X1

    OUT Y0MPP

    OUT Y1Note that MPP is followed immediately by OUT.

    This is an indication that it should be rewritten as:

    Point ofdivergence

    Point of

    divergence

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    21/27

    - 21 -

    Same as above but with cascaded outputs.

    X0

    y1

    y0x

    1

    It codes as:

    LD X0OUT Y1

    AND X1OUT Y0

    Not only can some divergents be re-written as cascades, simple outputs need looking at

    too. For instance, when first exposed to List, many people are tempted to write code withrepetitive logic such as:

    LD X0

    OUT Y0LD X0

    AND X1OUT Y1

    LD X0AND X1

    AND X2OUT Y2

    They wouldn't have done it like that in Ladder and there's no rule that says they have to

    do it that way in List. We could have accomplished the same result like this:

    LD X0OUT Y0

    AND X1OUT Y1

    AND X2OUT Y2

    You don't have to end a block of code just because you've reached an output.

    This is NOT a

    Point of divergence

    Simple Outputs with Repetitive Logic

    Same Logic with Cascaded Outputs

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    22/27

    - 22 -

    You CAN keep going! Of course, there are many times when repeating some of the logic(perhaps via an internal relay) will actually be a good idea. It allows you to break things

    up into sections and separates ideas. Knowing what your options are is the first steptowards writing optimal code.

    - OTHER STUFF -

    Reversing Your Logic or "What I really meant was..."Let's say you've just completed a long, involved piece of artwork (a masterpiece of logic)that views things from the perspective of "if all things are on then do something". After

    looking at it, you realize that although the result is correct, it doesn't explain things in thebest possible way. What you really wish you'd written was the same logic but expressed

    as "if some things are off then don't do anything". Before you start a mind-numbingrevision (or worse, start unloading the dishwasher) there is a simple "no thinking

    required" method for doing it. Which leads us to...

    Really Smart PLC Rule #7:If you invert every contact, logical operation and output in a logic sequence, there will be

    no change in the operation of the outputs.

    A full inverse is where you invert both the contact and the operation in one shot.It works like this:

    LD & LDI are the full inverse of each other (we only have a contact).AND & ORI are the full inverse of each other (both a contact and an operation).

    OR & ANI are the full inverse of each other (both a contact and an operation).ANB & ORB are the full inverse of each other (we only have an operation).

    OUT & INV OUT are the full inverse of each other (we only have an output).

    example:LD X0 ;if x0 is on

    AND X1 ;and x1 is onOR X2 ;or x2 is on

    OUT Y0 ;then turn on y0.

    is the same as...

    LDI X0 ;if x0 is off (full inverse of LD)ORI X1 ;or x1 is off (full inverse of AND)

    ANI X2 ;and x2 is off (full inverse of OR)INV ;then don't

    OUT Y0 ; turn on y0.

    Note the use of INV ("Invert"). It takes the value of the accumulator and inverts it. Someplc's offer a ready-to-go inverted version of OUT (usually called something similar to

    OUT_NOT) but it's NOT there in the FX series (pun intended). Fortunately, we canmimic it by using INV followed by OUT. Having to use INV may not be as efficient as

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    23/27

    - 23 -

    OUT_NOT but it is very versatile since we can use it for other things anywhere in ourcode. The bad news is it's NOT available on the FXON & FXOS.

    As you can see, this type of logic reversal is quite easy to perform in List. If you try the

    same thing in Ladder, you'll find it's a lot more difficult especially with complex logic.

    IF/THEN/ELSEIf/then/else is a very heavily used feature in almost every computer language everdeveloped.

    eg. in Pascal one could write this:If (Beer = 0) or (Chips = 0) then GetGroceries

    Else WatchTV;

    If the first line is true (ie. beer & chips really are equal to 0), the statement following theword "then" will execute but the statement following "Else" will not. If the first line is not

    true, then only the statement following "Else" will execute.

    We can do similar on the plc using INV. In fact, you can quite literally think "else" whenusing INV in this manner.

    LDI BEER ;if no beer

    ORI CHIPS ;or no chipsOUT GET_GROCERIES ;then get groceries

    INV ;elseOUT WATCH_TV ; watch TV

    As a side note, the above is a good example of cascaded outputs. Nonetheless, I don't

    recommend using it on your next job application.

    An alternative to using INV here would be to replace it with LDI GET_GROCERIES. Iprefer INV as it more closely associates the two possible results together. Instead of a

    cascade, LDI would start a new rung.

    Notice how easy it is to read the code when you put meaningful names in place of the I/Onumbers we've been using. It works well. I'll refrain from comments such as "Ladders are

    for changing light bulbs" if you'll admit List is not as bad as you thought.

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    24/27

    - 24 -

    Water Flows Downhill (but Ladder doesn't know that)I mentioned earlier that you are not allowed to use OR (or ORI) with cascaded outputs.

    I'll try and explain that here.

    Have a look at the following diagram.

    X0

    X0

    Y0

    Y1

    If this were wired with real relays in the real world, both Y0 and Y1 would always be on.

    It doesn't matter whether X0 is on or off; one of the contacts will always power up bothcoils. There is no point to this circuit but it illustrates the idea.

    This is how you would code it in order to obtain the same behavior:

    LD X0ORI X0

    OUT Y0OUT Y1

    Now check out this code. It's a set of cascaded outputs where the upper output is

    extended using ORI.LD X0

    OUT Y0ORI X0

    OUT Y1

    Under the current rules of engagement, thisshouldconvert into the same drawing as wehave above but its operation is very different from the relay version.

    Remember that once something is executed, the plc forgets about it. All it keeps is thecurrent result in the accumulator. If X0 is on, it will turn on Y0. After that, in line 3 of the

    List, we are in effect saying "if the accumulator is on OR if X0 is off then turn on Y1. Nomatter what it decides to do about Y1, it will never go back and change Y0. Y0 has been

    handled and forgotten about. Y0 will only go on if X0 is on. Y1 will go on no matterwhat. Guess what? Kaboom! One major pitfall for someone translating a relay diagram

    into a plc program. Rather, I should say, it's only a pitfall for someone who thinks thatjust because both versions will convert in their head to the same ladder diagram, he can

    use either version of List to represent the relays. However, if you let your editor auto-convert this List into Ladder, it will either "correct" it for you (Medoc Dos) or get very

    confused (GX Developer).

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    25/27

    - 25 -

    Medoc will change the cascaded outputs into simple ones as in the first example.

    LD X0ORI X0

    OUT Y0

    OUT Y1

    This should convert back again into the same drawing as we have above (it does).

    Written this way, Y0 & Y1 will operate the same as the real relays. But it has a differentmeaning than what we told it we wanted. Personally, I think it's unforgivable that Medoc

    has the audacity to change this code without so much as a warning. GX is not muchbetter. It refuses to convert it but doesn't tell you why or what the problem is. Just sort of

    hangs there in limbo. Not exactly good behavior.

    If you send the cascaded version to the plc without converting it to Ladder, it willgenerate an error that makes no sense ("Number of LD/LDI instructions is more than

    ANB/ORB instructions"). It really should operate the way we described above (theKaboom method). It iscorrect and might even be a useful bit of logic for you. On theother hand it could be quite the opposite of what you (or the next guy to read your code)were expecting. Worse, viewing it in Ladder will either cause changes that may become

    permanent or, make you look unprofessional when it fails to convert. Mitsubishi simplymade it an illegal combination without fully dealing with the situation. In any event, it's

    good to be aware of the issue. It also illustrates something I've been gently hinting at forsome time:

    PLC logic never flows backwards. Did I mention that?

    While we're on the subject, take note that if you had a different situation and needed to

    use an ANI instead of the ORI in the cascaded version, you'd avoid all of the problems.The Ladder editors would be happy and all will work exactly like everyone expects. Y1

    would never go on in this particular example but you get the point.

    This whole cascaded OR thing is not really the fault of List, the plc or even the Ladderlanguage. The problem lies with the Ladder editing software. Current editors have no

    way to draw it that isn't misleading. They couldeasily have used a downward pointingarrow (or a diode) instead of a solid vertical line. Until they figure this out and make

    cascaded OR's legal, you have no choice in the matter. You can't use them. Notsomething you'll miss anyway.

    The moral of this is that it's worth having a plc in the shop for testing purposes. If you

    don't have access to one, before heading out to a client's site, at least attempt a conversionin GX before deciding that everything is hunky-dory. Medoc users have no recourse other

    than caution.

    ***********************************************

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    26/27

    - 26 -

    QUICK SUMMARY OF THE RULES

    Really Smart PLC Rule #1:AND, ANI, OR & ORI commands always operate on all of the preceding logic.

    Really Smart PLC Rule #2:If a string of logic contains a setof series contacts and a set of parallel contacts:

    add the series set last if you want the two sets to be in series;

    add the parallel set last if you want the two sets to be in parallel.

    Really Smart PLC Rule #3:Use ANB/ORB when you need to apply logic that only operates on someof the precedinglogic.

    Really Smart PLC Rule #4:Both blocks being joined by ANB should end in OR, ORI or a block join (ANB/ORB).

    Both blocks being joined by ORB should end in AND, ANI or a block join (ANB/ORB).

    Really Smart PLC Rule #5:PLC logic never flows backwards.

    Really Smart PLC Rule #6:

    If a divergent output does not have logic between itself and the point of divergence, it'san indication that things could have been re-ordered as a cascade.

    Really Smart PLC Rule #7:

    If you invert every contact, logical operation and output in a logic sequence, there will beno change in the operation of the outputs.

    *************************************

    **********

  • 7/29/2019 Programming in List on a Mitsubishi PLC Part 2.pdf

    27/27

    I hope I've managed to provide you with some insight into the Instruction List language.Maybe you can even see yourself trying it out in your next project instead of

    automatically using Ladder. If nothing else, perhaps seeing a different approach hasdeepened your understanding of plc's and given you some ideas of your own. It's all

    worthwhile, right?

    Some of the people reading this will be beginners so I can't end without making a coupleof comments about the need to be very careful. The machinery we work on is dangerous

    and it's your job as a designer to remove as much of that danger as possible. In officesoftware development, a bad design is an irritant and errors can be expensive. Machinery

    on the other hand, is a whole different animal. Those same problems can rip someone'sarm off. Think carefully about everything you write. Worry about the design. Get help.

    Ask questions. Test, test, test. And then test some more. It's a case of Hero or Zero.There's not much in between.

    That's all for now. If you have anything to add, I'd love to hear from you.

    Comments and criticism (contract offers, bank drafts, etc) are all welcome.

    Happy coding!

    Jim Rowell

    Rowell [email protected]

    www.RowellElectric.ca

    mailto:[email protected]:[email protected]://www.rowellelectric.ca/http://www.rowellelectric.ca/http://www.rowellelectric.ca/mailto:[email protected]