DJM40082 PLC MED
CHAPTER 4
LDMICRO
Ladder Logic for PIC and AVR
1
DJM40082 PLC MED
4.0 INTRODUCTION
PLCs are often programmed in ladder logic. This is
because PLCs originally replaced relay control systems, and
forty years later, we still haven't quite let go. A PLC, like any
microprocessor, executes a list of instructions in sequence.
Ladder logic tools abstract this; you can program the PLC by
wiring up relay contacts and coils on-screen, and the PLC
runtime will simulate the circuit that you've drawn. Some of
the relay contacts can be tied to input signals from the real
world; some of the coils can be tied to outputs. That way you
can make your simulated circuit interact with other devices,
and actually control things. That is the point.
Actually it's more general than that, because you can
incorporate timers and counters and arithmetic operations
that you couldn't (easily) perform with just relays. The circuit
concept is still useful though, partly just because it's intuitive,
but also because it abstracts the concurrency issues.
4. 1 INTRODUCTION TO Ldmicro SOFTWARE
This software is tested under all versions from Windows
2000 to Windows 10. Unconfirmed reports suggest that it
works under WINE. The download is a .exe file; there are no
other files required, so there is no installation program. Save
it somewhere on your computer and just run it, and it will
work. The manual is included in the .exe file, but you can
download it separately if you want.
The compiler generates Intel IHEX files. Most of the
programming software that I have seen expects this. Of
course you need some sort of programming gadget to get the
hex file into the chip. For the PICs, Microchip's PICkit 2 or 3,
which is available from their web store were recommendded.
It should generally be possible to use code generated by
LDmicro with a bootloader. Most AVR parts have special fuses
(BOOTRST, BOOTSZx) that will need to be configured for
whatever bootloader you are using. The PIC16 parts don't
have any specific hardware support for a bootloader, but
LDmicro generates code with the correct format to allow the
bootloader to rewrite the reset vector.
The following chips are supported:
• PIC16F628(A)
• PIC16F88
2
DJM40082 PLC MED
• PIC16F819
• PIC16F877(A)
• PIC16F876(A)
• PIC16F887
• PIC16F886
• ATmega128
• ATmega64
• ATmega162
• ATmega32
• ATmega16
• ATmega8
It is also possible to generate C code from the ladder
program. That is less convenient, but you could use it on any
processor for which you have a C compiler.
LDmicro can generate interpretable byte code. If you are
willing to write an interpreter then you can use this to run
your ladder code on any kind of target. There is not very
much documentation on this, but I provide a sample
interpreter in mostly-portable C.
3
DJM40082 PLC MED
4.2 Ldmicro - AN OVERVIEW
a) Edit
From “Menu” panel, click “Edit” bar.
Figure 4.2: Programming Instruction
Frequently used option
i) Insert Rung Before
➔ To add new rung before previous rung.
New rung been created
ii) Insert Rung After
→ To add new rung after previous rung.
New rung been created Delete Rung
➔ To delete rung
iii)
4
DJM40082 PLC MED
b) Programming Instruction
From “Menu” panel, click “Setting” bar.
Programming Instruction section.
c) Simulate
From “Menu “panel, click “Simulate” panel.
5
DJM40082 PLC MED
To do simulation click “Simulation Mode” and then
once again click “Start Real-Time Simulation”.
To exit simulation, click “Esc” button.
d) Compile
From “Menu “ panel, click “Compile” panel.
To converting Ladder Diagram programming to
Machine Code, click “Compile”.
6
DJM40082 PLC MED
e) Config
From “Menu “panel, click “Config” panel.
To do basic setting on display.
4.3 STEPS BEFORE STARTING Ldmicro PROGRAMMING
Before starting using LDMicro software, several steps should be
taken.
a) Click ldmicro icon on Window Desktop , then the
following display will appeared on screen :-
b) Then, click Settings > MCU Parameters..
7
DJM40082 PLC MED
Set MCU Crystal Frequency (MHz) : 20.000000
The Result should be like this following figure:-
c) Again in the same Menu Panel, set the type of
microcontroller used in lab.
d) So the final result will be display as shown below.
8
DJM40082 PLC MED
e) Then as a safety procedure, save your file as preferred.
4.4 PICKIT 2 DEVELOPMENT PROGRAMMER/DEBUGGER
The PICkit™ 2 Development Programmer/Debugger
(PG164120) is a low-cost development tool with an easy to use
interface for programming and debugging Microchip’s Flash families
of microcontrollers. The full featured Windows® programming
interface supports baseline (PIC10F, PIC12F5xx, PIC16F5xx),
midrange (PIC12F6xx, PIC16F), PIC18F, PIC24, dsPIC30, dsPIC33,
and PIC32 families of 8-bit, 16-bit, and 32-bit microcontrollers, and
many Microchip Serial EEPROM products. With Microchip’s powerful
MPLAB Integrated Development Environment (IDE) the PICkit™ 2
enables in-circuit debugging on most PIC® microcontrollers. In-
Circuit-Debugging runs, halts and single steps the program while
9
DJM40082 PLC MED
the PIC microcontroller is embedded in the application. When
halted at a breakpoint, the file registers can be examined and
modified.
Figure 4. : PICkit™ 2 Development Programmer/Debugger
10
DJM40082 PLC MED
Figure 4.3 : PICkit 2 Software
4.5 HOW TO USE PICkit 2 PROGRAMMER
Connect a USB power source to the PICkit 2 unit,and also
power up the target unit seperately. Ensure the PICkit 2
“Power” LED is lit,and the “Target” LED is blinking twice in
succession to indicate the unit is in Programmer-To-Go mode
and ready to program. Connect the PICkit 2 unit ICSP
connector to the target.
https://www.rhydolabz.com/wiki/?p=6484#:~:text=Connect%20a
%20USB%20power%20source,ICSP%20connector%20to%20the%
20target.
11
DJM40082 PLC MED
4.5 SIMULATION WITH PROTEUS SOFTWARE
We will be using a PIC16F877, which is easily available
from online distributors. It comes in a number of
different packages; I chose a DIP.
This is the schematic:
The only thing that might confuse you is that the
pushbutton goes to Vdd, and there is a pull-down. You
might be more used to seeing a pushbutton to ground
with a pull-up. For TTL, this mattered. For modern CMOS
it does not, and I find this `active HIGH' arrangement
less confusing than the traditional `active LOW' circuit.
.
12
DJM40082 PLC MED
TOPIC 5
Flow Chart
13
DJM40082 PLC MED
5.0 What is a Flowchart?
A flowchart is a graphical representations of steps. It was
originated from computer science as a tool for representing
algorithms and programming logic but had extended to use in all
other kinds of processes. Nowadays, flowcharts play an extremely
important role in displaying information and assisting reasoning.
They help us visualize complex processes, or make explicit the
structure of problems and tasks. A flowchart can also be used to
define a process or project to be implemented.
5.1 WHY DO WE NEED A FLOWCHART?
You can use flowcharts in numerous fields and categorize their
advantages wholly in the following points:
• Effective documentation: Without giving any chances for
paper works, you can do sufficient documentation of the
other process. Besides, you can conduct training with the aid
14
DJM40082 PLC MED
of a flowchart. This advantage can get mainly observed in
organizations, both small and big.
• Clarity of the process: If a new process or project is about
to begin, creation of a flowchart is the first step. The step-by-
step procedure and the possible outcomes of each method
will be clearly stated, leaving no chance for confusions and
future doubts. Moreover, flowcharts provide visual clarity.
• Improved problem solving: When you make a flowchart,
problem-solving and bug fixing becomes an easy task. You
can break an issue into parts and define flowcharts
separately. You can mention all the possible solutions and
their outcomes. Then pick up the optimized solution making
it cost-effective and time conserving.
• Effective communication and co-ordination: At present,
team meetings are getting replaced by flowcharts. Yes,
rather than arranging team meetings and wasting time in
them, a framed flowchart depicting the actual process and
expected performance with the deadline is being circulated.
• Analysis & Improvement: You can also use a flowchart or
diagram to analyze the ongoing process effectively and
compare the performance with the expected one. If you find
any deviation, you can pitch-in steps for improvement.
15
DJM40082 PLC MED
5.2 FLOWCHART SYMBOLS
Different flowchart shapes have different conventional meanings.
The meanings of some of the more common shapes are as
follows:
16
DJM40082 PLC MED
TOPIC 6
Ldmicro Basic Examples
17
DJM40082 PLC MED
6.0 LADDER LOGIC PROGRAMMING WITH INSTRUCTIONS
Each symbol in ladder logic is an instruction. This can, in the
beginning, be rather confusing. But don’t worry. I will explain this
with simple examples. Let me start by giving you a simple
example. In this first example, you will be introduced to the two
first ladder logic symbols.
So what are these instructions or symbols?
They are basically logic instructions, that make you able to create a
piece of logic. That piece of logic is your ladder logic or PLC
program. If you take a closer look at the example below, you will
see two instructions (symbols).
6.1 INSTRUCTIONS REFERENCE
A) BASIC INSTRUCTION
1) CONTACTS AND COILS/RELAYS , NORMALLY OPEN
Xname Rname Yname
----] [---- ----] [---- ----] [----
If the signal going into the instruction is false, then the output
signal is false. If the signal going into the instruction is true, then
the output signal is true if and only if the given input pin, output
pin, or internal relay is true, else it is false. This instruction can
examine the state of an input pin, an output pin, or an internal
relay.
Instruction > Contact and Coil/Relays > Insert Contact : Input Pin C
18
DJM40082 PLC MED
2) CONTACTS AND COILS/RELAYS, NORMALLY CLOSED
Xname Rname Yname
----]/[---- ----]/[---- ----]/[----
If the signal going into the instruction is false, then the output
signal is false. If the signal going into the instruction is true, then
the output signal is true if and only if the given input pin, output
pin, or internal relay is false, else it is false. This instruction can
examine the state of an input pin, an output pin, or an internal
relay. This is the opposite of a normally open contact.
To set the Input Pin to be Negate, just double click the target
input pin and click at I/I Negated.
3) COIL, NORMAL Yname
----( )----
Rname
----( )----
If the signal going into the instruction is false, then the internal
relay or output pin is cleared false. If the signal going into this
instruction is true, then the given internal relay output pin is set
true. It is not meaningful to assign an input variable to a coil.
This instruction must be the rightmost instruction in its rung.
Instruction > Contact and Coil/Relays > Insert Coil : Output Pin L
19
DJM40082 PLC MED
4) COIL, NEGATED Yname
----(/)----
Rname
----(/)----
If the signal going into the instruction is true, then the given
internal relay or output pin is cleared false. If the signal going
into this instruction is false, then the given internal relay or
output pin is set true. It is not meaningful to assign an input
variable to a coil. This is the opposite of a normal coil. This
instruction must be the rightmost instruction in its rung.
To set the Input Pin to be Negate, just double click the target
input pin and click at I/I Negated.
5) COIL, SET-ONLY Yname
----(S)----
Rname
----(S)----
If the signal going into the instruction is true, then the given
internal relay or output pin is set true. Otherwise the internal
relay or output pin state is not changed. This instruction can only
change the state of a coil from false to true, so it is typically
used in combination with a reset-only coil. This instruction must
be the rightmost instruction in its rung.
6) COIL, RESET-ONLY
Rname Yname
----(R)---- ----(R)----
If the signal going into the instruction is true, then the given
internal relay or output pin is cleared false. Otherwise the
internal relay or output pin state is not changed. This instruction
instruction can only change the state of a coil from true to false,
20
DJM40082 PLC MED
so it is typically used in combination with a set-only coil. This
instruction must be the rightmost instruction in its rung.
7) COIL, TOGGLE
Rname Yname
----(T)---- ----(T)----
In the PLC ladder diagram, a coil is classified into many types as
shown in the picture below.
As we can see in the picture below a coil could be an internal relay,
an output pin on MCU or a Modbus. There are five different types of
coil. Here we select the T-trigger (T) type. The letter T means
'toggle'.
A T-trigger coil changes (toggles) its output logic state whenever
it's input change from low to high.
Let look at very simple example, I use an input contact to change
the logic state of the output coil.
When the contact Xbutton1 changes from false to true, the output
coil changes its logic state.
We can make a circuit simulation in Proteus to verify the result.
21
DJM40082 PLC MED
8) Output Latch
Let’s say that digital input is a momentary pushbutton. It is
called momentary because it has a spring inside. This means,
that the pushbutton will only be active as long as you press it.
The ladder program above works just fine. But as you might
have noticed, the output will only be active as long as the input
is active. You will have to hold your finger on the button to keep
the output activated. But let’s say that the output controls a fan
for a ventilation system. It would not be very convenient for the
operator to hold down the button all the time. We need a way to
keep the output active, even though the operator releases the
pushbutton.
22
DJM40082 PLC MED
In ladder logic there are two ways to do that:
23
DJM40082 PLC MED
EXERCISE #1
Write and simulate the following Ladder Diagram. Give a comment on
particular event.
a)
Flow chart:
Comment : ..............................................................................
..............................................................................
b)
Flow chart:
Comment : ..............................................................................
..............................................................................
c)
Flow chart:
Comment : ..............................................................................
..............................................................................
24
DJM40082 PLC MED
d)
Flow chart:
Comment : ..............................................................................
..............................................................................
e)
Flow chart:
Comment : ..............................................................................
..............................................................................
f)
Flow chart:
Comment : ..............................................................................
..............................................................................
25
DJM40082 PLC MED
g)
Flow chart:
Comment : ..............................................................................
..............................................................................
h) Interlocking circuit
Flow chart:
Comment : ..............................................................................
..............................................................................
26
DJM40082 PLC MED
B) LOGIC GATES
1) OR LOGIC
In ladder logic the OR logic is implemented with a parallel
connection of two instructions:
OR Logic implemented with Ladder Logic as parallel
connection.
On the other hand the stop button has to be not pressed for
the motor be keep being activated. But the logic relationship
between the start and motor, and the stop button is AND.
Because for the motor to be activated other the start
conditions and not the stop button had to bee true.
2) AND LOGIC
27
DJM40082 PLC MED
AND logic is implemented with ladder logic as a serial
connection of instructions:
AND Logic implemented with Ladder Logic as serial connection.
One of the major advantages of ladder logic is that you
graphically can build logic. Because if you follow the lines
from left to right at each network, you can see the logic
relationships between instructions.
3) NOT LOGIC
NOT logic or negation can be implemented with a negated
contact either in the ladder logic or as a negated physical
contact. These are also known as examine if open or normally
closed instructions.
4) NOR LOGIC
NOR Gate is operated OR Gate followed by the NOT Gate.
When both inputs are low then the output will be high.
Otherwise, the low output will occur if both inputs are high.
28
DJM40082 PLC MED
5) NAND GATE
NAND gate is operated as an AND gate and followed by the
inverter.
Working: In NAND Gate, the output will be low when both
inputs are high. For all other cases, the output will be high.
6) EX-OR GATE
Working: If both inputs are high or low, the output will
become low. For any other input condition, the output will be
high.
29
DJM40082 PLC MED
7) EX-NOR
Working: When both inputs (I1 and I2) are high or low then the
output will high. If anyone input is high or low then the output
will become low.
8) Rotate Right (ROR)& Rotate Left (ROL)
A shift instruction commonly found in the high level
programming language such as C. Here in the PLC ladder
diagram they include a circular shift instruction that support a
carry bit.
The shift operation works in two directions - right and left.
After the shift operation the data just change its position but
all bits will not lost their value.
When the input to these instruction is true it start its circular
shift operations with any specific position. The position is set
in the operand2 of instruction property.
30
DJM40082 PLC MED
For a simple example I use these two instruction to chase the LEDs.
31
DJM40082 PLC MED
C) TEMPORAZATION
A temporazation is a PLC instruction measuring the amount of
time elapsed following an event. Timer instructions come in two
basic types:
Delay Turn On and Delay Turn Off timers. Both “on-delay”
and “off-delay” timer instructions have single inputs
triggering the timed function.
An “on-delay” timer activates an output only when the
input has been active for a minimum amount of time.
An “off-delay” timer activates an output only when the
input has been de-active for a minimum amount of time.
A retentive ondelay timer, by contrast, maintains its
elapsed time value even when the input is de-activated.
This makes it useful for keeping “running total” times for
some event. Most PLCs provide retentive and non-
retentive versions of on-delay timer instructions, such
that the programmer may choose the proper form of on-
delay timer for any particular application.
1) TON (Delay Turn On)
This instruction is a type of PLC delay timer. The timer on
delay Tdon instruction keeps track of its input high time but
it's not store the timing value like the retentive timer.
32
DJM40082 PLC MED
If the input high time into this instruction is greater than the
preset delay time, it will output true. And it will change to
false at the moment the input falls to low.
A very simple and easy ladder diagram example show a
single rung of the program that made of an input contact, a
Tdon instruction and an output coil.
Delay Time On timer applications
On-delay timers are used a lot in PLC programs and in many
different ways. For example, an on-delay timer can be used
on a led’s start button.
The timer could be set up so that the start button
(xbutton) would need to be pushed and held for 1 (ONE)
second before the output Yled would switch on.
2) TOFF (Delay Turn Off)
Referring to the timer delay related instructions in the PLC
ladder diagram instructions, we are now discussing about
another timer delay called turn-off delay (Tdoff).
33
DJM40082 PLC MED
When ever the input to this instruction changes from false to
true, it will output true for a specific delay time. The input
high time must greater or equal to the PLC cycle time.
Let see a very simple example. I use an input contact to turn
on an output coil for 1 second.
Off-delay timer applications
Off-delay timers are used when you need something to stay
running longer than everything else. For example, cooling
fans and pumps might continue to run after a machine
pauses from doing work.
3) Retentive Delay On timer
This instruction keeps track of how long its input has been
true. If its input has been true for at least 1.000 s, then the
output is true. Otherwise the output is false. The input need
not have been true for 1000 consecutive milliseconds; if the
input goes true for 0.6 s, then false for 2.0 s, and then true
for 0.4 s, then the output will go true. After the output goes
true it will stay true even after the input goes false, as long
34
DJM40082 PLC MED
as the input has been true for longer than 1.000 s. This timer
must therefore be reset manually, using the reset instruction.
The `Tname' variable counts up from zero in units of scan
times. The TON instruction outputs true when the counter
variable is greater than or equal to the given delay. It is
possible to manipulate the counter variable elsewhere, for
example with a MOV instruction.
An introductory example, use an input contact to to turn on
an output coil. The output coil logic state depends how long
the input from high duration created by the contact is high.
Retentive Delay On Applications
Retentive on-delay timers can be used to track the runtime of
equipment. They can be set up to trigger an indicator light to
let someone know the equipment has run long enough and
requires maintenance to inspect and clean it.
35
DJM40082 PLC MED
4) TIMER HIGH Thi
-[THI 1.000 s]-
When the signal going into the instruction goes from false to
true, the output signal is true during 1.000 s. The delay is
configurable. The output signal is reset after 1.000 s, the internal
timer counteris reset after 1.000 s and if the input is false.
logic
level
^ The duration of the input pulse must be
| longer than the PLC cycle
THI | _ ________
input | ___/ \_________/ \_______
|| |
| | 1s | 1s
| |<-->| |<-->|
||| ||
|vv vv
THI | ____ ____
output | ___/ \______/ \___________
--+------------------------------------> time,s
|
5) TIMER LOW Tlo
-o[TLO 1.000 s]-
When the signal going into the instruction goes from true to
false, the output signal is false during 1.000 s. The delay is
configurable. The output signal is set true after 1.000 s, the
internal timer counter is reset after 1.000 s and if the input is true.
logic
level
^ The duration of the input pulse must be
| longer than the PLC cycle
TLO | ___ _________ _______
input | \_/ \________/
|| |
| | 1s | 1s
| |<-->| |<-->|
||| ||
|vv vv
TLO | ___ ______ ___________
output | \____/ \____/
--+------------------------------------> time,s
|
36
DJM40082 PLC MED
6) CYCLIC TIMER Tmeander
-[TCY 500 ms]-
If input signal is true, this instruction produce meander with
period (500ms+500ms) = 1s and frequency 1 Hz. If the signal
going into the instruction is false, then the output signal is false. If
Tmeander value = PLC cycle time, the TCY is equal to OSC.
37
DJM40082 PLC MED
EXERCISE #2
Write and simulate the following Ladder Diagram. Give a comment on
particular event.
a) TON
Flow chart:
Comment : ..............................................................................
..............................................................................
b) TOFF
Flow chart:
Comment : ..............................................................................
..............................................................................
c) TON and TOFF
Flow chart:
38
DJM40082 PLC MED
Comment : ..............................................................................
..............................................................................
d) Led blinking 1
Flow chart:
Comment : ..............................................................................
..............................................................................
e) Led blinking 2
Flow chart:
Comment : ..............................................................................
..............................................................................
39
DJM40082 PLC MED
f) Running Lights
Flow chart:
Comment : ..............................................................................
..............................................................................
g) Traffic Light 1
40
DJM40082 PLC MED
Flow chart:
Comment : ..............................................................................
..............................................................................
h) Traffic Light 2
Flow chart:
Comment : ..............................................................................
..............................................................................
i) Traffic Light 3
41
DJM40082 PLC MED
Flow chart:
Comment : ..............................................................................
..............................................................................
j) Running Light
Flow chart:
Comment : ..............................................................................
..............................................................................
k) RTO
Flow chart:
42
DJM40082 PLC MED
Comment : ..............................................................................
..............................................................................
l) Running light Active Low
Flow chart:
Comment : ..............................................................................
..............................................................................
m)Running light follower 1
Flow chart:
43
DJM40082 PLC MED
Comment : ..............................................................................
..............................................................................
n) Running light follower 2
Flow chart:
Comment : ..............................................................................
..............................................................................
o)
Flow chart:
44
DJM40082 PLC MED
Comment : ..............................................................................
..............................................................................
p) Knight Rider
Flow chart:
Comment : ..............................................................................
..............................................................................
45
DJM40082 PLC MED
D) COMPARE VARIABLE
Just like other programming language, math comparison
instruction is always existed. In ladder diagram these kinds of
instruction built in symbols rather than a text.
[var ==] [var >] [1 >=]
-[ var2 ]- -[ 1 ]- -[ Ton]-
[var !=] [-4 < ] [1 <=]
-[ var2 ]- -[ vartwo]- -[ Cup]-
We have six comparison instructions list above,
1. if equal
2. if not equal
3. if greater than
4. if less than
5. if greater than or equal
6. if less than or equal
When the input to the instruction is true, it make a comparison
between two operands as set in its property. This instruction will
output true whenever the result of the comparison returns true.
46
DJM40082 PLC MED
E) COUNTER
In ladder diagram of LDMicro a counter instruction increases is
value up on the input change from false to true. LDMicro
classifies a counter into two categories - counter and circular
counter. Here, we discuss about a counter only. A counter
divides into two instructions - Counter Up (CTU) and Counter
Down (CTD) instruction.
1) Counter Up (CTU) instruction
A CTU increases its content by one whenever its input
changes from false to true. This instruction output true when
ever its content rolls over the preset value.
• Name - CTU identifier
• Start value - CTU initial value
• True if - set the value of output true condition from CTU
• Input kind - '\' for true to false and '/' for false to true.
47
DJM40082 PLC MED
2) Counter Down (CTD) instruction
Similarly, a CTD decrease its content whenever its input
change state. It output true whenever its content drop below
the preset value.
3) CIRCULAR COUNTER (CTC) Cname
--{CTC 0:3}--
A circular counter works like a normal CTU counter, except that
after reaching its upper limit, it resets its counter variable back
to 0. For example, the counter shown above would count 0, 1, 2,
4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 2,.... This is useful
incombination with conditional statements on the variable
`Cname'; you can use this like a sequencer. CTC counters clock
on the rising edge of the rung input condition condition. This
instruction must be the rightmost instruction in its rung.
48
DJM40082 PLC MED
4) CIRCULAR COUNTER REVERSIVE (CTR) Cname
--{CTR 0:-3}--
A circular counter works like a normal CTU counter, except that
after reaching its upper limit, it resets its counter variable back
to 0. For example, the counter shown above would count 0, 1, 2,
4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 2,.... This is useful
incombination with conditional statements on the variable
`Cname'; you can use this like a sequencer. CTC counters clock
on the rising edge of the rung input condition condition. This
instruction must be the rightmost instruction in its rung.
49
DJM40082 PLC MED
50