Structured Programming Help


Thread Starter

Donny Z.

I have written a program for an AB SLC 5/03 for motion control using high speed counter cards and encoders. I am using a Panelview 300 as the operator interface for inputing part size info.
The program works fine but is written very sloppy using trial and error. I needed to add a new process to the program and found it nearly impossible. I started looking into rewriting my program with structured programming using state diagrams and subroutines. I am using SmartDraw to draw my state diagrams. Is there software out there designed specifically for writing PLC state
diagrams? I am also having trouble figuring out the best way to place outputs controlling the positioning motors in my ladder logic as I will have to turn on the same motor in different states or subroutines depending on the part being
processed. I would like some references and examples as to how to correctly setup a structured program. Thanks.
Here is a link to an article that has some good basics in it:


A few other pointers: Years ago I used to try to write my code as small as possible, and I would try and keep my bit use and word use, eg B3
and N7 files, as sequential as possible, not skipping over addresses, etc. (There are still lots who would argue that this is the proper
way to program, but I respectfully disagree) But now memory is cheap, most PLCs have plenty for the job, and since its there already whether
you use it or not, just block it out and write your code to enhance readability. For example I'll use a permissive bit in every rung instead of an MCR insturction because at 3:00am a maintenance person can see what the problem is even if he doesn't realize there is an MCR
instruction ahead of the rung, and I get a good nights sleep. One of my favorite things about the AB processors is that you can divide your
memory up into files. Take advantage of the AB JSR instructions and memory file structure and break the program up into smaller sections.
Program each subroutine as a "black box", ie it has its input conditions and outputs. Its alot easier to maintain, and you have less of a
chance of introducing bugs when you make changes. Good up front planning and breaking down the problem into a series of smaller problems is by far the best way to attack any programming job, regardless of the language.

Sometimes I will group all my physical outputs together in one spot in the main program file, rather than distributed througout the program,
then use simple single bit logic or simple branched logic to trigger the output, eg BST XIC B103/5 NXB XIC B103/10 BND OTE O:1/0. The more complex logic for B103/5 and B103/10 are elsewhere in the program. The main reason for this is to make it easier for a maintenance person who is less familiar with PLCs to find the outputs, then locate the one he wants, then he can do a search on the bit to jump to the bits OTE intruction and see the more complex logic. While it is rarely the program that is the problem, maintenance tends to look there first, so make it easy on them, besides it keeps him from hunting through the code, and again, I am not awakened in the middle of the night as often.

Every programmers style is different, so good luck and with persistence, even a plate of spaghetti can be untangled.
BST XIC Product1 XIC StateX NXB XIC Product2 XIC StateY NXB XIC Product3 XIC StateZ (repeat as needed)BND OTE Motor1

Should be straightforward, if not, reply to the post and I can clarify.

Hi Rob, Thanks for the reply, I think I got it but just to relieve my curiosity could you get a little more detailed. Thank You.

Chris Elston

The PLC Archive here at has some stuff, or you can download the Index Table - PDF or RSS file from in the
file downloads section.

It's an entire structured machine logic program. That might give you some idea. Hope it helps.


Jeremy Pollard

Hi Donny - you have been bitten by the spaghetti code genie. Many years of practice and loss of hair will be of great service to you.

Since that won't help you right now, I would suggest going to the web site to learn a bit about Sequential function charts and the new PLCopen motion control function blocks. You may not be able to implement right now, but might serve as a design tool.

Cheers from:

Jeremy Pollard, CET
[email protected]
On The Web -
PLCopen North America - [email protected]
the Training Factory, Inc.
Programmable Controller Support Systems
The Software User Newsletter ONLINE
The Crazy Canuckian!
8 Vine Crescent, Barrie, Ontario L4N 2B3
705.739.7155 Fax 705.739.7157
My first reply was to your "multiple conditions to control one output" Just as in TConnelly's reply, I will put some complex logic input conditions to one bit output, another set of input conditions to another bit output, as many as are required, then OR (parallel branches in one rung) all those bits with the actual output as the output coil on
that rung.