Course IC-1004 Introduction To Programming Design For Controllers
Course IC-1004 Introduction To Programming Design For Controllers
com
Course № IC-1004
This document is the course text. You may review this material at
your leisure either before or after you purchase the course. To
purchase this course, click on the course overview page:
http://www.pdhengineer.com/pages/IC-1004.htm
or type the link into your browser. Next, click on the Take Quiz button
at the bottom of the course overview page. If you already have an
account, log in to purchase the course. If you do not have a
PDHengineer.com account, click the New User Sign Up link to create
your account.
After logging in and purchasing the course, you can take the online
quiz immediately or you can wait until another day if you have not yet
reviewed the course text. When you complete the online quiz, your
score will automatically be calculated. If you receive a passing score,
you may instantly download your certificate of completion. If you do
not pass on your first try, you can retake the quiz as many times as
needed by simply logging into your PDHengineer.com account and
clicking on the link Courses Purchased But Not Completed.
PDHengineer.com
5870 Highway 6 North, Suite 310
Houston, TX 77084
Toll Free: 877 500-7145
administrator@PDHengineer.com
Introduction to Programming Design for Controllers
James Vogel P.E.
Introduction to Programming
The modern concept of the computer originated in the mid 1940’s. In 1945, John Von
Neumann proposed a computer architecture that included storing instructions and
data in the computer. This is the computer architecture of most computers today, and
is referred to as the “Von Neumann” computer architecture. The programming of early
computers involved preparing a sequence of machine coded instructions. In 1954,
John Backus invented the first popular high level programming language, Fortran
(Formula Translator). Fortran is oriented to scientific programming and allowed one to
write programs that could run on different computers. Fortran facilitated writing
programs such as Finite Element Analysis programs that aided the engineering analysis
of structures and mechanical components.
The number of computing devices has grown from several research computers to over
a billion computers today. In addition to the computer and programmable logic
controllers (PLC), embedded computers are very popular, especially in hand-held
devices. The demand for programming has been driven by the need to program the
many computing devices currently available. An international standard, IEC 61131-3
was published in 1993 for standardization of programmable controller programming.
The purpose of the standard was to make programming more consistent among PLC
systems.
Program Development
A program is defined as a set of coded instructions for insertion into a machine, which
then performs the desired sequence of operations. Creating a program can be divided
into seven steps:
1. Define the problem. Specify inputs, outputs and the processing task.
2. Analyze the requirements. Divide the processing task into subtasks. Create a
hierarchical chart
3. Logic design of the program. Prepare flowcharts, pseudocode, and state
diagrams as required.
4. Code the program. Convert the design into a formal programming language.
Write source code.
5. Compile and run. Convert the source code to a machine executable code. Run
the program.
6. Test the program. Run test scenarios to verify outputs are correct. Correct
programming errors.
7. Document and maintain. Prepare user manuals and maintain the program.
Specifying the program’s inputs and outputs is an essential part of defining the
programming problem. All inputs have to be converted into a data type before
processing. The basic unit of data is the bit, a binary digit. The bit has a value of 0 or 1.
This can represent the state of a switch, relay, or sensor. A bit can also represent an
output control signal that can turn on lights, motors, valves and pumps. It is often
referred to as a Boolean (BOOL) data type. This is a common data type for inputs and
1
Introduction to Programming Design for Controllers
James Vogel P.E.
There are a number of other useful data types. A sixteen bit signed number is often
referred to as an integer (INT). An integer can represent positive and negative numbers
of up to 32,767. This data type is useful for counting. Decimal representation of numbers
is useful to represent the output of sensors such as weight, pressure and temperature.
Decimal numbers require 32 bits and are referred to as a float or REAL data type. Eight
bits is called a byte and is sufficient to code a character. The ASCII code refers to a
binary coding scheme for coding characters. Textual data is usually longer than one
character and requires more than one byte. The string of characters is a text or STRING
data type. This data type is useful for messages. There are also DATE and TIME data
types. Strong data typing can prevent mismatching data which can have
unpredictable results. Data typing is common to most high level programming
languages.
TrafficLight
Logic Design
2
Introduction to Programming Design for Controllers
James Vogel P.E.
The decision symbol indicates a test or condition that will result in two possible paths:
one for when the condition is true, “yes”; and one for when the condition is false, “no”.
Start/Stop
Operation
Subroutine*
RedLightX
Change light to red Is remote
Set wait time based on signal on?
Yes No
remote signal
YellowLight
Turn on yellow light
Turn off green light
Wait 5 seconds Return
3
Introduction to Programming Design for Controllers
James Vogel P.E.
VARIABLES
price $0.50, $1.00
change $0.00, $0.25, $0.50
quarter = $0.25
selection = “coffee”, “coke”
dollarReader = True, False
Pseudocode programs include words and symbols that describe computer operations
such as input, output, AND, OR, add (+), subtract (-), multiply (*) and divide (/). The “=”
can mean a comparison of two values, or an assignment of a value to a variable.
Pseudocode can implement the fundamental control structures: series of instructions
(sequence);“if…then...else” statements (selection); “do while” statements for repeating
instructions based on a condition being true(iteration). These are shown below.
START:
output "Make A Selection"
input selection
if selection = “coffee” then
price = $0.50
else
price = $1.00
endif
if dollarReader = True
change = $1.00 – price
dowhile change is greater than or equal to $0.25
dispense a quarter
change = change - $0.25
endwhile
dispense selection
output "Thank You"
endif
END
State diagrams define a system using states and transitions between states. States are
written in the circles and directional lines show the transitions to the next state. Only one
state is active at one time. A state diagram for a simple coffee machine is given below.
Here we can see that when powered up, the machine will start in an idle state. The
transitions are based on the inputs of sensors in the coffee machine. The states can
indicate the operations that need to be further defined in detail.
4
Introduction to Programming Design for Controllers
James Vogel P.E.
Coding is the conversion of the program design into a formal programming language.
The programming language used can depend on whether the language is available
for the controller or computing device. It can also depend on the skills of the personnel
available for the programming task. The following includes examples of programs using
the IEC standard controller programming languages. The end result of coding of the
program is referred to as source code.
Ladder Diagram (LD) language is a graphical language based on relay ladder logic.
This is the popular method of programming controllers. Ladder Diagrams are based on
relay and switching logic. It is more intuitive and easy for electricians and electrical
technicians to understand. Program instructions are entered on “rungs”. Input
components are inserted on the left side and the outputs on the right. Common input
components are Normally Open and Normally Closed contacts. Common outputs are
relay solenoids, motors, valves and lights. Ladder diagrams can also include advanced
functions such as timers like the one shown below. A Traffic Light sequencing program is
shown below:
The diagram looks complicated, but it is simple. Each light has a timer and the timer
turns on the light. After a preset wait time, the timer turns off the light and starts the next
timer in the sequence. The diagram shows the timer enable (EN) output turning on the
light by closing normally open (EN) contacts. After the preset wait time, the timer done
(DN) output turns off the light by opening normally closed (DN) contacts and starts the
next timer by closing the normally open RedLight.DN contacts.
5
Introduction to Programming Design for Controllers
James Vogel P.E.
Ladder diagram programs can also include calculations, counting, control loops and
subroutines. The ladder diagram strength is in performing switching and relay logic.
Instruction list (IL) language is more closely related to the instructions that the computer
executes. Mnemonics designate computer operations. A computer programmer is
more familiar with this approach. The following program reduces the voltage if a motor
speed is greater than 1000. It also turns on a green light.
The IL language can perform logical operations (AND, OR, NOT) ; calculations using
ADD, SUB, MUL, DIV instructions; selections and loops. This IL program uses instructions for
a conditional test (GT) and a conditional jump (JMPCN) to code the selection structure.
The IL language can also include subroutines and functions such as timers. Although
this is not as intuitive as ladder logic, it is easier to have the instruction list language
implemented on a programmable controller. Programming using the Instruction List
language is similar to using assembler code and can result in fast executing code.
Structured Text (ST) is a structured high-level programming language that is similar to the
Pascal programming language. The language is designed for easier coding of control
structures. This programming is more suited for an engineer with a programming
6
Introduction to Programming Design for Controllers
James Vogel P.E.
Structure Text also includes “IF… THEN… ELSE” statements for implementing the selection
structure and “WHILE...DO” statements for iterations. The “WHILE…DO” statement is
useful for checking whether a sensor is on, or a setting is reached, like a time setting,
temperature setting or tank level. The “FOR…DO” statements are used for a fixed
number of repetitions. This is useful for situations involving a fixed count. ST strength is in
performing complex calculations and it is easy to read and understand.
Function Block Diagram (FDB) is a graphical language that uses function blocks and
connections that show data and signal flow. The FBD is more common in the process
industries. The following compares a function block and structured text. This shows a
divide function block. A divide by zero can cause serious errors for a computer and can
halt operations. This function block checks to be sure “b” is not zero, before
proceeding. If b is 0, it provides a 0 output.
7
Introduction to Programming Design for Controllers
James Vogel P.E.
FUNCTION_BLOCK divide
VAR_INPUT
a: INT;
divide b: INT;
END_VAR
a VAR_OUTPUT
c c: INT;
b END_VAR
IF b <> 0 THEN
c := a / b;
ELSE
c := 0;
END_IF;
END_FUNCTION_BLOCK
Initialization
In this example, the program sequences traffic lights through red, green and yellow and
also sequences a crosswalk (WALK/DON’T WALK) sign, simultaneously. Each step shown
has actions and a transition condition (bar). The transition condition needs to be met
before proceeding to the next step. The transition condition for this case is that the timer
must complete the set time. Unlike other languages, the SFC allows simultaneous
execution of steps in parallel branches. SFC also allows steps to be programmed in
other languages. The strength of SFC is the capability for synchronization of steps. It is
also suited for complex control problems.
8
Introduction to Programming Design for Controllers
James Vogel P.E.
Other High Level Languages and Programs. In addition to the previously mentioned
standard languages, controllers can be programmed in higher level languages like the
C programming language. C is an ANSI standard language; however, it is not included
in the controller programming language standard. Another language that is not a
standard controller language is Visual Basic. The programming of visual display
interfaces does not usually require the precision timing of controller programs. Visual
Basic is a programming language that includes many features that can allow one to
create visual displays of processes.
The previous examples show traffic light sequencing, a motor control, and calculations
in the different programming languages. There are many systems that use computing
devices that require programming: process control, alarm systems, HVAC, robots,
assembly lines, motion control, gas monitoring, and power generation. The programs
can involve many lines of code and can be in different languages. This has provided
an overview of the different programming languages and code one could see.
Conclusion
9
Introduction to Programming Design for Controllers
James Vogel P.E.
Bibliography
Farrell, Joyce, Programming Logic and Design, Course Technology, Boston, Mass 2002
Kernighan, Brian W. and Pike, Rob, The Practice of Programming, Addison-Wesley,
Reading, Mass, 1999
Lewis, R. W., Programming Industrial Control Systems using IEC 1131-3 Revised Edition,
Institute of Electrical Engineers, UK, 1998
Web Sites
Hugh Jack. Automated Manufacturing Systems; PLCs, GNU Free Document License
1996-2007 http://claymore.engineer.gvsu.edu/~jackh/books/plcs/
Bjarne Stroustrup. The C++ Programming Language. AT&T labs, Inc and TAMU. 2007.
http://www.research.att.com/~bs/C++.html
10