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 tet. !ou'll gain insight into the block commands and the various structures that can be built using List. !ou'll leave with full knowledge of the perils and pitfalls. We'll be using numbered as opposed to named I"# for the eamples. $on%t get used to it and definitely don't be discouraged by it. &umbers work best for showing you how the code works but they're terrible at showing you what it's actually doing. !our own code 'with proper I"# names( will be much easier to follow. )perienced 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 phrases introduced 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 your money back, void where prohibited(. #ur focus will be on the *itsubishi +, series but the concepts apply more or less on all plc's. -efore using anything you learn here though, do yourself a favour by checking your manuals and testing your code. Take anything I say 'or anyone else says( with several grains of salt. *y mistakes don't have to be yours too. The Really Easy Stuff #kay, let's see how List compares with Ladder while writing some code. +eel free to try any of the eamples in your editing software. .ust be aware that if you switch between List and Ladder views without having an output on each rung 'most of the eamples don't(, you'll receive an error. /ll you need to do is add an output to the end of the List code. /ll0ighty then... We'll start at the beginning of a rung 'good choice huh1(. +irst we type2 L$ ,3 X 0 &o surprises there. /s you can see, we have begun a new rung with a normally open contact. We always have to start with the L$ 'or L$I( command. 4 5 4 &ow let's add2 /&$ ,5 X 0 X 1 The /&$ command places a contact in series with all of the preceding logic. 6o far 7all7 consists of that single ,3 contact we previously placed. !ou can perform /&$ as many times as you like and every new contact will be placed in series with all of the preceding logic. That's because when the plc reads a contact, all of the preceding logic has already been reduced to a single result held in the accumulator 'remember1 8art 51(. The plc is simply going to perform a logical /&$ with the accumulator and your new contact. /&$ ,9 /&$ ,: X 0 X 1 X 2 X 3 &o mystery there, right1 /t this point, the accumulator holds the value of the output of ,:. 4 9 4 &ow try2 #0 ,; X 0 X 1 X 2 X 3 X 4 Whoa! What the heck happened there1 <nlike a logical /&$, an #0 command will place a contact in parallel with all of the preceding logic or in other words, in parallel with your entire rung. We're back to that accumulator thing again. The plc performed a logical #0 with the accumulator and your new contact. /&$ ,= > places a contact in series with all of the preceding logic. X 0 X 1 X 2 X 3 X 4 X 5 The accumulator now holds the value of the output of ,=. 4 : 4 #0 ,? > places a contact in parallel with all of the preceding logic. X 0 X 1 X 2 X 3 X 4 X 5 X 6 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 Good If you've followed along patiently to this point, you will probably agree that so far it's embarrassingly simple. /fter all, you're pretty smart. I guess it's time to tackle the things that are not so obvious. +irst 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 are in parallel with each other but still in series with the first ones. Like this2 X 0 X 1 X 2 X 3 X 4 X 5 X 6 ,3 to ,: are in series. ,; to ,? are in parallel with each other and, as a group, are also in series with ,3 to ,:. 4 ; 4 !ou simply can't do this in List using nothing but your basic /&$ and #0. In 8art 5, we talked about using the #0- '7#r -lock7( and /&- '7/nd -lock7( commands. We'd have to use them here. 6omething like2 L$ ,3 /&$ ,5 /&$ ,9 /&$ ,: L$ ,; #0 ,= #0 ,? /&- &ote2 $o a @uick review of 8art 5 if you don't understand the use of /&-... or what the heck, Aust read a bit further. This is where people start to complain. 6uch a simple construct in Ladder yet in List we are already having to resort to using 9 separate blocks of code Aoined by /&-B. 'the complaints are occasionally followed by rioting in the streets, looting, setting of fires, etc(. 6ince this is in fact a common logic se@uence, it's worth letting you in on a little secret. /lthough you have probably drawn eactly such a diagram in Ladder and would be Aust as likely to build it that way using relays, as far as the plc is concerned, you've been doing it backwards all along. Cuh1 !up. Try putting the #0'd contacts first and see what happens. Like this2 X 4 X 5 X 6 X 0 X 1 X 2 X 3 4 = 4 &ow write that in List and you get2 L$ ,; #0 ,= #0 ,? /&$ ,5 /&$ ,9 /&$ ,: 6ee1 &o /&-. !ou Aust made it a lot more readable. !ou also made it shorter. It now re@uires less memory to store the program and you made it eecute faster. )ven more important, you learned one of the reasons why List might be a good idea. <sing Ladder, you tend to think like a human 'and that can't be good(. If you are using List, you see what the plc sees and you have a better opportunity to write code that is efficient. -efore anyone gets upset with me, I will @uickly add that the efficiency of code is usually not that critical compared to readability, maintainability and other factors. #n the other hand, it's never a bad thing. There are times when efficiency is very important. )veryone tends to make this /&$ before #0 mistake. The good old :4wire start"stop circuit taught in every electrical class is probably to blame. / good controls electrician has this seared into his brain. The series4wired stop buttons always came first followed by the parallel4wired start buttons D holding contacts. -ut now we are inside the plc. In a plc, we need to take advantage of 0ule E5 rather than being ruled by it. The following new rule helps us do Aust that. Really Smart PLC Rule #2: If a string of logic contains a set of series contacts and a set of parallel contacts: add the series set last if yo want the two sets to be in series! add the parallel set last if yo want the two sets to be in parallel" Note: This is really Aust an epansion of rule E5. 4 ? 4 In the previous eample, we avoided the /&- by putting the series set last. We had a set of series contacts and a set of parallel contacts and we wanted them in series with each other. )actly what 0ule E9 said to do. If we had wanted them in parallel we would have reversed it like this2 X 0 X 1 X 2 X 3 X 4 X 5 X 6 AN!"R 6ooner or later we will have to resort to /&-"#0-. 6o let's do some practice with those. We'll minimiFe their use by keeping our 0eally 6mart 8LG 0ule E9 in mind. +irst of all, remember that using L$ will always begin a new 7block7 of code 'what we called a 7series7 of statements in 8art 5> 7block7 is the official, tech term(. In effect, using the L$ command in the middle of a rung suspends the logic you are working on and temporarily begins a new rung at the left rail. !ou end up with two blocks> the old unfinished one and a new temporary one. The reason we want to do this is so that when something applies to 7all of the preceding logic7 it will only be referring to what's in this new block. +or now, the old block will be ignored. When you are ready, the new one can be connected to the old by using /&- or #0-. 8ictures! We need pictures! L$ ,3 >start block E5 #0 ,5 produces this2 X 0 X 1 4 H 4 -lock E5 L$ ,3 /&$ ,5 /&$ ,9 /&$ ,: #0 ,; #0 ,= #0 ,? &ow let's add another L$ statement 'to get a new block( followed by #0. /ltogether we have2 L$ ,3 >start block E5. #0 ,5 L$ ,9 >start block E9. #0 ,: X 0 X 1 X 2 X 3 &ote that both blocks were left 7open7 'ie. there are no #<T or other output type commands that would produce a connection to the right hand rail(. If we add an /&- at the end of our code, the two blocks will be placed in series like this2 X 0 X 1 X 2 X 3 4 I 4 /&- -lock E5 -lock E9 If we instead use an #0-, we'll parallel them like this2 X 0 X 1 X 2 X 3 <sing #0- in this eample was rather pointless. We could have Aust #0'd all the contacts together in the first place and avoided the use of #0-. Cere's a better eample showing the use of #0-2 L$ ,3 >start block E5. /&$ ,5 L$ ,9 >start block E9. /&$ ,: X 0 X 3 X 2 X 1 4 J 4 #0- &ow add #0- and you'll get2 X 0 X 3 X 2 X 1 Gommon sense might have told you the last drawing could be coded asB L$ ,3 /&$ ,5 #0 ,9 /&$ ,: when in fact, that would have produced this2 X 0 X 3 X 2 X 1 .ust remember that common sense will also insist that the world is flat. 6o if you're not eactly giving 8tolemy a run for his money, this would be a good time to go back and review 'starting with 8art 5(. /ssuming you are happy with the concept of a spherical planet, let's ride forthB When do you need /&-"#0-1 Really Smart PLC Rule ##: #se AN$%OR$ when yo need to apply logic that only operates on some of the preceding logic" Identify the logic section of interest and then separate it out by surrounding it with L$ D /&-"#0-. If you need to do an /&$ but only want the contact to be in series with some of the preceding logic, you need an #0-. If you need to do an #0 but only want the contact to be in parallel with some of the preceding logic, you need an /&-. 4 53 4 #0-
)ample of /&$ needing an #0-2 X 0 X 3 X 2 X 1 We only want ,: to be in series with some of the preceding logic 'ie. in series with ,9, but not ,3 or ,5(. Cere it is in List2 L$ ,3 /&$ ,5 L$ ,9 >marks the start of the 7some7 stuff. /&$ ,: >this /&$ tells us we need an #0-. #0- >places the ,94,: block in parallel with the ,34,5 block. )ample of #0 needing an /&-2 X 0 X 1 X 2 X 3 X 4 We only want ,; to be in parallel with some of the preceding logic 'ie. in parallel with ,:, but nothing else(. Cere it is in List2 L$ ,3 #0 ,5 L$ ,: >marks the start of the 7some7 stuff. #0 ,; >this #0 tells us we need an /&-. /&- >places the ,:4,; block in series with the ,34,5 block. /&$ ,9 4 55 4 #0- /&- &otice I didn't include the '/&$ ,9' until after the /&-. I left it until the end as a separate operation. I didn't have to really but 0ule E9 says I should. In more comple code, it'll help keep things clearer if you develop this habit. Keep the contents of blocks you'll be Aoining as short and simple as possible. #kay, our net eample is going to get nasty. I can almost guarantee your response will soon be 7Coly Gow! This Aust isn't worth it7 'actually, you may have been thinking that for a while now(. I'll be the first to admit that learning to write clear, readable code is easier if 7Ladder only7. 6o why bother with all of this1 Well, once you get used to it, you'll find that List has the following strengths2 5( It's @uicker to type than it is to draw 'and in Ladder you still have to fill in names and constants anyway(. 9( Gomments, names and constants are clearer and more readable. :( !ou can see more of your program at one time on the screen and have more control over the layout. ;( 8rintouts are more compact and can be edited in any word processor @uite easily. =( *ore efficient code. +aster eecution. Less memory re@uired . ?( Gomple functions are more readable. H( Women will find you more attractive. &one 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 eample. I recently wrote a program to control a rolling tube bender. There was nothing etraordinary about it yet there were sections that contained over fifty math functions in a row. In Ladder, these appeared as endless horiFontal lines, each one ending way over on the right4hand side of the screen in a bo full of misarranged numbers. In List, it was very clear and obvious what was going on. It looked and read more like proper math. It's also worth pointing out that although the program was over :333 steps in length, it contained only 5= block Aoins and a single *86"*88 pair. That's because it was written in List. !ou shouldn't Audge List by the mess you sometimes see when you auto4convert Ladder into it. 0egardless of the type or level of programming you are doing today, chances are you'll be doing different things tomorrow. -eing able to comfortably switch between List and Ladder will allow you to take advantage of the strengths of each. 8ersonally, I write everything in List and use Ladder to double4check sections that don't 7feel7 right. I get a second perspective from Ladder that can be invaluable. !ou may end up using the opposite approach or perhaps you'll write -oolean in Ladder and switch to List for math and similar functions. 0egardless, if you wish to have List available to you, you need an understanding of its features and rules. -esides, I'll be really ticked if after all the typing I Aust did, you decide it would be a great time to try out the delete key on your new keyboard. 6o, assuming both you and this document still eistB 4 59 4 Let's try a comple eample. +irst we'll translate the ladder diagram shown below eactly as it's drawn. We'll even do it in the order suggested by the contact numbers ',3, ,5, ,9, etc(. X 0 X 1 X 2 X 3 X 6 X 7 X 4 X 1 0 X 1 1 X 5 Y 0 Cere's the List2 L$ ,3 /&$ ,5 L$ ,9 /&$ ,: #0- > see .5 'Aoins the ,94,: block with the ,34,5 block(. L$ ,; /&$ ,= L$ ,? /&$ ,H L$ ,53 #0 ,55 /&- > see .9 'Aoins ,534,55 block with the ,?4,H block(. #0- > see .: 'Aoins the block created by .9 with the ,;4,= block(. /&- > see .; 'Aoins the block created by .: with the one created by .5(. #<T !3 !ou'll note that we held off using some of the block commands until the end. We had to in order to Aoin blocks in the correct order. /&-"#0- always operate on the two most recently created blocks. !ou are allowed to have several blocks outstanding before you have to perform a Aoin. The +, series allows a maimum of eight. $oing it this way, you have to be careful not to Aoin the wrong blocks together. It's also rather confusing. 4 5: 4 .9 .; .5 .: -y re4arranging things, we could have kept most of the block commands 7inline7 and made things a little easier to read 'and less error prone(. This is a much better techni@ue. Like this2 L$ ,3 /&$ ,5 L$ ,9 /&$ ,: #0- > see .5 'Aoins the ,94,: block with the ,34,5 block( L$ ,? /&$ ,H L$ ,53 #0 ,55 /&- > see .9 'Aoins ,534,55 block with the ,?4,H block(. L$ ,; /&$ ,= #0- > see .: 'Aoins the ,;4,= block with the one created by E.9(. /&- > see .; 'Aoins the block created by E.5 with the one created by .:(. #<T !3 We still have an area that could be improved. Cave a close look at ,?, ,H, ,53, D ,55 on the drawing. Look familiar1 0emember our 0eally 6mart 8LG 0ule E91 #h, you already saw that and were Aust being polite1 I'm impressed. The rule tells us that by placing ,? D ,H after ,53 D ,55 we can eliminate the .9 block command. Lery nice. 4 5; 4 Cere's the final version 'you don't really believe that do you1(. L$ ,3 /&$ ,5 L$ ,9 /&$ ,: #0- >see .5 'Aoins the ,94,: block with the ,34,5 block(. L$ ,53 #0 ,55 /&$ ,? /&$ ,H >we no longer need the block Aoin at .9. L$ ,; /&$ ,= #0- >see .: 'Aoins the ,;4,= block with the ,534,H block(. /&- >see .; 'Aoins the block created by .5 with the one created by .:(. #<T !3 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 *edoc, M, or whatever and play(. &otice the blocks Aoined by /&- both end in #0- and the blocks Aoined by #0- all end in /&$. This tells us we've done a good Aob 'according to the net rule(. Really Smart PLC Rule #$: $oth blocks being &oined by AN$ shold end in OR, ORI or a block &oin 'AN$%OR$(" $oth blocks being &oined by OR$ shold end in AND, ANI or a block &oin 'AN$%OR$(" &otice it says that both of the blocks being Aoined must @ualify. That's a key point. -reaking 0ule E; doesn't make your code illegal. It Aust means you haven't written it in the best possible way. !ou might be using a block Aoin when it's not needed. !ou could even be including an /&$ that would be better added after the Aoin is completed. The rule is unforgiving. /lso note it doesn't recogniFe L$ as a proper block end so you know at least two contacts are needed in each block you are attempting to Aoin. If we had followed this rule in the first place, the last version of our eample would have appeared automatically without re@uiring the two revisions. Interestingly enough, 0ule E; even insists that you obey rule E9. Gould be a conspiracy. 4 5= 4 There is still the issue of that final /&- in our last version. / bit confusing isn't it1 When @uickly scanning the code, you have no idea what that /&- is referring to. Cere's a method that is a lot more readable 'never believe that 7final7 version stuff(. L$ ,3 /&$ ,5 L$ ,9 /&$ ,: #0- >combine this block with the previous one. #<T *3 >assign an internal relay to record the result so far. L$ ,; >start over fresh. /&$ ,= L$ ,53 #0 ,55 /&$ ,? /&$ ,H #0- >combine this block with the previous one. /&$ *3 >add in the result from way back. #<T !3 In the real world, you'd give *3 a nice, descriptive name so that when you use it, you'll know eactly what it refers to. !ou can't give /&- 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 there1 Which one would you rather work with five years from now when you've forgotten what this machine even looks like1 "%TP%TS We haven't yet dealt with outputs in 8art 9. It's hard to write a program that does anything without 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. #r notB There are three types of output connections that one can make. I call these Simple, Cascaded and Divergent outputs. 6o, you're saying to yourself 7#h Mawd. Ce's making up terms and definitions now. Ce Aust wants this to be complicated7 'yeah, I heard you(. There is a purpose here. *any people, when writing in List, don't even realiFe that cascaded outputs are legal. /lso, if you don't know the difference between cascaded and divergent outputs, you'll often create divergent ones by mistake. /nd divergent outputs can lead to horrors such as *86 which should be avoided like prison time or processed cheese. 4 5? 4 Simple "utputs !ou have a simple output when you have only one or, if there are several, they are directly connected together. )here are no contacts between any of the otpts. &othing mysterious here. X 0 Y 0 Y 1 Y 2 Cas&aded "utputs !ou have cascaded outputs when you have more than one output and each uses all of the logic of the preceding output and then extends that logic with more of its own. X 0 Y 0 Y 1 Y 2 X 1 X 2 ,5 etends the logic of !3. ,9 etends the logic of !5. &ote the etension logic leading to each lower coil from the upper coil connects directly to the upper coil. There are no contacts 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 L$"/&$"#0, etc and, if needed, /&-"#0-. The etension logic added for each new output is built using only /&$"/&I. 'once you place even 5 output, you are no longer allowed to use /&-"#0-(. In reality, you shold be able to also use #0 in the etension logic but the ability has been disabled in the plc. +or a look at why, see *ater +lows Downhill 'in the Other ,tff section at the end of this document(. 4 5H 4 &ote2 There are &# contacts at these points L$ ,3 #<T !3 #<T !5 #<T !9 L$ ,3 #<T !3 /&$ ,5 #<T !5 /&$ ,9 #<T !9 'i(er)e*t "utputs !ou have divergent outputs when you have more than one output and each uses only some of the logic of the preceding output. )ach output may, but is not re@uired to, etend that logic with more of its own 'remember this last part, 0ule E? is all about it(.
X 0 Y 0 Y 1 Y 2 X 1 x 2 x 4 x 3 &ote that ,: is sitting between the higher coil '!3( and the connection point leading to a lower coil '!5(. +1 does *ot use all of the lo)i& that +, does- !5 totally ignores ,:. The output of the ,3 contact is what I call a 7point of divergence7. It's like a fork in the road. The logic splits into 9 different routes. )ach half of the fork ends in a coil that is not seen by the other. 6o what1 Well picture the plc merrily processing everything in your program up to and including ,3 in the normal manner. Then it looks at ,: and outputs !3. )verything is rosy. 0emember, at this point 'like all points( none of the preceding logic eists anymore. /ll we have is the current result in the accumulator. This result includes the effect of ,:. <nfortunately, we still have to process the logic leading up to !5 but that's not supposed to include ,:! -asically, we now need the plc to back up a bit 'to the point of divergence( and then eecute the branch to !5. Cmmm. 6orry... plc's don't do that. Really Smart PLC Rule #.: -./ logic never flows backwards" In order to build a set of divergent outputs, you have to use *86 '*emory 8oint 6tore( or some other method of saving the value of the accumulator when you come to the place where the multiple output paths separate 'the point of divergence(. Later, when it's time to eecute the second branch, you can recall that value from storage and use it to modify the accumulator. Whenever you see a point of divergence in your code, you know you need to stick an *86 there 'or similar(. This is the only time *86 is used in a program. 4 5I 4 There *<6T be contacts here 8oints of divergence There */! be contacts here The E(il /PS I'm not going to go into too much detail on *86 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 Aust @uickly say this2 If you write *86, the plc will store the value of the accumulator at that point in the program. *86 operates on a stack that is 59 storage locations deep 'in the +, series(. )ach time you write *86 '*emory 8oint 6tore(, you shove the previous values deeper into the stack. !ou read the topmost value with *0$ '*emory 0ead(. This loads the accumulator with the value read. If instead you read the stack using *88 '*emory 8oint 8op(, you load the accumulator with the current value and then bring the net value up to the top. 6ubse@uent uses of *0$ will read that 7net value7. !ou must completely empty the stack before leaving a rung or you'll generate an error. In other words, for every *86 there must also be a *88. !ou can have any number of *0$'s. Caving said that, you shouldn't use *86 ecept in very obvious circumstances. /ll too often, you'll see *0$ or *88 used many lines below the *86. &ow you have to search up through the code to figure out what is being referenced. A 0etter method is to assign an internal relay 'eg. #<T *3( to that point in the logic instead of using *86 at all. )nd the rung right there and start over using a contact from the internal relay. !ou can now give that relay a very descriptive name and refer to it as often as you wish without worrying about matching *88's with *86's. !our rungs will be shorter and simpler too. The main reason that *86 eists 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 gets sent to the plc. The editor's conversion makes grand use of *86 'especially since unlike you, most people don't know about divergent versus cascaded outputs(. It needs a temporary method like *86 because it's not at liberty to use internal relays 'they belong to you( and it would do a lousy Aob of naming them anyway. The Poi*t of all of this 6imple outputs are obvious. They'll usually take care of themselves. Gascaded outputs are nice. The code looks good and it eecutes @uickly. $ivergents are not really devil spawn but 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 Aust a little bit of effort. )specially when written in Ladder. Which brings us to2 Really Smart PLC Rule #1: If a divergent otpt does not have logic between itself and the point of divergence, it0s an indication that things cold have been re1ordered as a cascade" 4 5J 4 8roper divergent outputs. X 0 Y 0 Y 1 X 2 X 1 It codes as2 L$ ,3 *86 /&$ ,5 #<T !3 *88 /&$ ,9 #<T !5 $ivergent outputs that should be rewritten as cascaded2 X 0 Y 0 Y 1 X 1 It codes as2 L$ ,3 *86 /&$ ,5 #<T !3 *88 #<T !5 &ote that *88 is followed immediately by #<T. This is an indication that it should be rewritten as2 4 93 4 8oint of divergence 8oint of divergence 6ame as above but with cascaded outputs. X 0 y 1 y 0 x 1 It codes as2 L$ ,3 #<T !5 /&$ ,5 #<T !3 &ot only can some divergents be re4written as cascades, simple outputs need looking at too. +or instance, when first eposed to List, many people are tempted to write code with repetitive logic such as2 L$ ,3 #<T !3 L$ ,3 /&$ ,5 #<T !5 L$ ,3 /&$ ,5 /&$ ,9 #<T !9 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 this2 L$ ,3 #<T !3 /&$ ,5 #<T !5 /&$ ,9 #<T !9 !ou don't have to end a block of code Aust because you've reached an output. 4 95 4 This is &#T a 8oint of divergence 6imple #utputs with 0epetitive Logic 6ame Logic with Gascaded #utputs !ou G/& keep going! #f 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 step towards writing optimal code. 2 "T3ER ST%FF 2 Re(ersi*) +our Lo)i& or 45hat 6 really mea*t 7as---4 Let's say you've Aust completed a long, involved piece of artwork 'a masterpiece of logic( that views things from the perspective of 7if all things are on then do something7. /fter looking at it, you realiFe that although the result is correct, it doesn't eplain things in the best possible way. What you really wish you'd written was the same logic but epressed as 7if some things are off then don't do anything7. -efore you start a mind4numbing revision 'or worse, start unloading the dishwasher( there is a simple 7no thinking re@uired7 method for doing it. Which leads us to... Really Smart PLC Rule #8: If yo invert every contact, logical operation and otpt in a logic se2ence, there will be no change in the operation of the otpts" / full inverse is where you invert both the contact and the operation in one shot. It works like this2 L$ D L$I are the full inverse of each other 'we only have a contact(. /&$ D #0I are the full inverse of each other 'both a contact and an operation(. #0 D /&I are the full inverse of each other 'both a contact and an operation(. /&- D #0- are the full inverse of each other 'we only have an operation(. #<T D I&L #<T are the full inverse of each other 'we only have an output(. eample2 L$ ,3 >if 3 is on /&$ ,5 >and 5 is on #0 ,9 >or 9 is on #<T !3 >then turn on y3. is the same as... L$I ,3 >if 3 is off 'full inverse of L$( #0I ,5 >or 5 is off 'full inverse of /&$( /&I ,9 >and 9 is off 'full inverse of #0( I&L >then don't #<T !3 > turn on y3. &ote the use of I&L '7Invert7(. It takes the value of the accumulator and inverts it. 6ome plc's offer a ready4to4go inverted version of #<T 'usually called something similar to #<TN&#T( but it's &#T there in the +, series 'pun intended(. +ortunately, we can mimic it by using I&L followed by #<T. Caving to use I&L may not be as efficient as 4 99 4 #<TN&#T but it is very versatile since we can use it for other things anywhere in our code. The bad news is it's &#T available on the +,#& D +,#6. /s you can see, this type of logic reversal is @uite easy to perform in List. If you try the same thing in Ladder, you'll find it's a lot more difficult especially with comple logic. 6F!T3EN!ELSE If"then"else is a very heavily used feature in almost every computer language ever developed. eg. in 8ascal one could write this2 If '-eer O 3( or 'Ghips O 3( then MetMroceries )lse WatchTL> If the first line is true 'ie. beer D chips really are e@ual to 3(, the statement following the word 7then7 will eecute but the statement following 7)lse7 will not. If the first line is not true, then only the statement following 7)lse7 will eecute. We can do similar on the plc using I&L. In fact, you can @uite literally think 7else7 when using I&L in this manner. L$I -))0 >if no beer #0I GCI86 >or no chips #<T M)TNM0#G)0I)6 >then get groceries I&L >else #<T W/TGCNTL > watch TL /s a side note, the above is a good eample of cascaded outputs. &onetheless, I don't recommend using it on your net Aob application. /n alternative to using I&L here would be to replace it with L$I M)TNM0#G)0I)6. I prefer I&L as it more closely associates the two possible results together. Instead of a cascade, L$I would start a new rung. &otice how easy it is to read the code when you put meaningful names in place of the I"# numbers we've been using. It works well. I'll refrain from comments such as 7Ladders are for changing light bulbs7 if you'll admit List is not as bad as you thought. 4 9: 4 5ater Flo7s 'o7*hill 90ut Ladder does*:t ;*o7 that< I mentioned earlier that you are not allowed to use #0 'or #0I( with cascaded outputs. I'll try and eplain that here. Cave a look at the following diagram. X 0 X 0 Y 0 Y 1 If this were wired with real relays in the real world, both !3 and !5 would always be on. It doesn't matter whether ,3 is on or off> one of the contacts will always power up both coils. 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 behavior2 L$ ,3 #0I ,3 #<T !3 #<T !5 &ow check out this code. It's a set of cascaded outputs where the upper output is etended using #0I. L$ ,3 #<T !3 #0I ,3 #<T !5 <nder the current rules of engagement, this shold convert into the same drawing as we have above but its operation is very different from the relay version. 0emember that once something is eecuted, the plc forgets about it. /ll it keeps is the current result in the accumulator. If ,3 is on, it will turn on !3. /fter that, in line : of the List, we are in effect saying 7if the accumulator is on #0 if ,3 is off then turn on !5. &o matter what it decides to do about !5, it will never go back and change !3. !3 has been handled and forgotten about. !3 will only go on if ,3 is on. !5 will go on no matter what. Muess what1 Kaboom! #ne maAor pitfall for someone translating a relay diagram into a plc program. 0ather, I should say, it's only a pitfall for someone who thinks that Aust because both versions will convert in their head to the same ladder diagram, he can use either version of List to represent the relays. Cowever, if you let your editor auto4 convert this List into Ladder, it will either 7correct7 it for you '*edoc $os( or get very confused 'M, $eveloper(. 4 9; 4
*edoc will change the cascaded outputs into simple ones as in the first eample. L$ ,3 #0I ,3 #<T !3 #<T !5 This should convert back again into the same drawing as we have above 'it does(. Written this way, !3 D !5 will operate the same as the real relays. -ut it has a different meaning than what we told it we wanted. 8ersonally, I think it's unforgivable that *edoc has the audacity to change this code without so much as a warning. M, is not much better. It refuses to convert it but doesn't tell you why or what the problem is. .ust sort of hangs there in limbo. &ot eactly good behavior. If you send the cascaded version to the plc without converting it to Ladder, it will generate an error that makes no sense '7&umber of L$"L$I instructions is more than /&-"#0- instructions7(. It really should operate the way we described above 'the Kaboom method(. It is correct and might even be a useful bit of logic for you. #n the other hand it could be @uite the opposite of what you 'or the net guy to read your code( were epecting. Worse, viewing it in Ladder will either cause changes that may become permanent or, make you look unprofessional when it fails to convert. *itsubishi simply made 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 for some time2 -./ logic never flows backwards" $id I mention that1 While we're on the subAect, take note that if you had a different situation and needed to use an /&I instead of the #0I in the cascaded version, you'd avoid all of the problems. The Ladder editors would be happy and all will work eactly like everyone epects. !5 would never go on in this particular eample but you get the point. This whole cascaded #0 thing is not really the fault of List, the plc or even the Ladder language. The problem lies with the Ladder editing software. Gurrent editors have no way to draw it that isn't misleading. They cold easily have used a downward pointing arrow 'or a diode( instead of a solid vertical line. <ntil they figure this out and make cascaded #0's legal, you have no choice in the matter. !ou can't use them. &ot something 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 conversion in M, before deciding that everything is hunky4dory. *edoc users have no recourse other than caution. PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP PPPPPPPPPP 4 9= 4 =%6C> S%//AR+ "F T3E R%LES 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 set of series contacts and a set of parallel contacts: add the series set last if yo want the two sets to be in series! add the parallel set last if yo want the two sets to be in parallel" Really Smart PLC Rule ##: #se AN$%OR$ when yo need to apply logic that only operates on some of the preceding logic" Really Smart PLC Rule #$: $oth blocks being &oined by AN$ shold end in OR, ORI or a block &oin 'AN$%OR$(" $oth blocks being &oined by OR$ shold end in AND, ANI or a block &oin 'AN$%OR$(" Really Smart PLC Rule #.: -./ logic never flows backwards" Really Smart PLC Rule #1: If a divergent otpt does not have logic between itself and the point of divergence, it0s an indication that things cold have been re1ordered as a cascade" Really Smart PLC Rule #8: If yo invert every contact, logical operation and otpt in a logic se2ence, there will be no change in the operation of the otpts" PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP PPPPPPPPPP 4 9? 4 I hope I've managed to provide you with some insight into the Instruction List language. *aybe you can even see yourself trying it out in your net proAect instead of automatically using Ladder. If nothing else, perhaps seeing a different approach has deepened your understanding of plc's and given you some ideas of your own. It's all worthwhile, right1
6ome of the people reading this will be beginners so I can't end without making a couple of comments about the need to be very careful. The machinery we work on is dangerous and it's your Aob as a designer to remove as much of that danger as possible. In office software development, a bad design is an irritant and errors can be epensive. *achinery on the other hand, is a whole different animal. Those same problems can rip someone's arm off. Think carefully about everything you write. *orry about the design. Met help. /sk @uestions. Test, test, test. /nd then test some more. It's a case of Cero or Qero. There's not much in between. That's all for now. If you have anything to add, I'd love to hear from you. Gomments and criticism 'contract offers, bank drafts, etc( are all welcome. Cappy coding! .im 0owell 0owell )lectric .im.0R0owell)lectric.ca www.0owell)lectric.ca 4 9H 4