Anda di halaman 1dari 42

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.

Chapter 4,

page 1 of 14

(please turn off mobile phones)

Chapter 4.

More About Ladder Logic

4.1. Memory Areas


How many inputs and outputs can a PLC have? The PLCs we use have 12 inputs (bits 00.00 to 00.11) and 8 outputs (100.00 to 100.08). However CX-programmer allocates The whole of words 00 to 99 (100 words or 1600 bits) to inputs and The whole of words 100 to 199 (100 words or 1600 bits) to outputs.

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 4,

page 2 of 14

(please turn off mobile phones)

What about when we want to store the output from one rung for use elsewhere in the program? CX-programmer has a memory area called the work area for this. This area is addressed by placing a W before the memory address, and has 512 words (8192 bits): words W0 to W511 (bits W0.00 to W511.15).

When the power to the PLC is interrupted, the values in all these memory addresses are lost. However there are situations where we want the PLC to maintain its memory values even after the power is interrupted or the PLC is shut down. CXprogrammer has a separate memory area called the holding area that achieves this. This area is addressed by placing a H before the memory address, and has 1535 words (24,560 bits): words H0 to H1535 (bits H0.00 to H1535.15).
2

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 4,

page 3 of 14

(please turn off mobile phones)

4.2.

Program Cycling and the Order of Execution of Rungs.

The same logic could be obtained by using the rung of the ladder diagram as an electrical circuit diagram and: holding its left hand rail at a non-zero potential and its right hand rail at a zero potential replacing the inputs by normally open switches. replacing the negated inputs by normally closed switches. replacing the outputs by solenoids taking the normally open switches to represent true when pressed and false when not pressed taking an energised solenoid to represent true.

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 4,

page 4 of 14

(please turn off mobile phones)

An exception to the above analogy between ladder logic and electrical circuits becomes apparent in ladder diagrams containing more than one rung (as ladder diagrams almost always do). In an electrical circuit, all rungs are energised, and therefore perform their logic, simultaneously. But ladder logic is a computer language executed by a microprocessor in the PLC, so it cant analyse every rung simultaneously. In order to make ladder logic more closely resemble this simultaneous operation, the execution proceeds in a continuous cycle as shown in this figure.

Read the PLC Input Signals Execute the first rung


0001 1101 0001 0002 1102

0001 1103

0002

0010

0003 1104

1103 1105

Execute the last rung Activate the PLC Output Signals

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 4,

page 5 of 14

(please turn off mobile phones)

Only one rung is executed at a time. The program cycles continuously from the moment the PLC is put into run mode (even when nothing on the rig has been touched) A PLC does not wait for a rung to go true before the following rung is executed. If the rung is false, the output is set low and the PLC moves onto the following rung The PLC input signals are read only at the start of each cycle. The PLC output signals are activated only after the last rung has been executed Each rung takes a time in the order of milliseconds to execute. Each rung happens immediately after the preceding rung.

Read the PLC Input Signals Execute the first rung


0001 1101 0001 0002 1102

0001 1103

0002

0010

0003 1104

1103 1105

Execute the last rung Activate the PLC Output Signals

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 4,

page 6 of 14

(please turn off mobile phones)

The whole cycle is executed in a very short time, giving the impression of the instantaneous and continuous execution of a hardwired electric circuit. Unlike an electric circuit, the order in which the rungs are arranged is important (as will be shown in coming weeks) . You should never use the addresses of the PLC inputs as the output of a rung. Since the PLC outputs are activated only after the last rung is executed, if a PLC output is used as the output to more than one rung, only its last value will ever be sent to the plant.

Read the PLC Input Signals Execute the first rung


0001 1101 0001 0002 1102

0001 1103

0002

0010

0003 1104

1103 1105

Execute the last rung Activate the PLC Output Signals

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 4,

page 7 of 14

(please turn off mobile phones)

4.3. Some more Examples


Suppose that we want to extend cylinder 1 (memory address 100.02) when either the red button or the black button is pressed. One possibility program is:
RED 100.0

BLACK

100.0

Looking at the logic of this program: RED BLACK 100.02 State of Cylinder 1 1 0 0 1 0 1 1 0

We see that the red button has no influence on the program Every time the red button controls 100.02, it is immediately changed by the following rung. Clearly this program does not achieve the required output.
7

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 4,

page 8 of 14

(please turn off mobile phones)

A better solution is: (extend cylinder 1 when either the red button or the black button is pressed)

RED BLACK

100.0

Looking at the logic of this program: RED BLACK 100.02 State of Cylinder 1 1 0 0 1 0 1 1 0

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 4,

page 9 of 14

(please turn off mobile phones)

This is also true for cylinder 2 and cylinder 4, despite being driven by two memory addresses. Suppose that we want to extend cylinder 2 when the red button is pressed, but retract it whenever the black button is pressed. Since we know that cylinder 2 extends when 100.04 is high and 100.03 is low, it is tempting to try the following program: Looking at the logic of this program:
RED 100.04

BLACK

100.03

RED BLACK 100.03 100.04 State of Cylinder 2 1 0 0 1 1 0

Clearly this program does not achieve the required output.

1 0
9

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 4,

page 10 of 14

(please turn off mobile phones)

A better solution is: (extend cylinder 2 when the red button is pressed, but retract it whenever the black button is pressed)
100.04

RED

BLACK

100.03

RED BLACK 100.03 100.04 State of Cylinder 2 1 0 0 1 0 1 1 0

10

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 4,

page 11 of 14

(please turn off mobile phones)

4.4. Using a Time Chart to Simulate a PLCs behaviour


In writing and debugging a PLC program, it is useful to be able to simulate how the PLC executes the rungs of a Ladder Diagram. We can do this by constructing a Time Chart. We start with a rectangular grid as shown. For each bit listed in the Each column represents a program scan. A single bit line from left to right is used for each bit. The bit line jumps between the high line to the low line whenever the bit changes its logical value. GB RB Ext-C1 Ext-C2 Ret-C2

11

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 4,

page 12 of 14

(please turn off mobile phones)

For the first program scan we work our way through the program. We determine the value of the output/coil of each rung, based on the current values of the rungs input/contacts. If the value of a bit changes from low to high or visa versa, its bit line is drawn vertically. After the first program scan, each bit line is extended horizontally to the vertical grid line corresponding to the second program scan. We repeat the process for the second program scan. GB RB Ext-C1 Ext-C2 Ret-C2

12

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 4,

page 13 of 14

(please turn off mobile phones)

If there are no changes during a program scan, and not changes to the PLC inputs, subsequent scans will be exactly the same. Such a situation is called a steady state and is represented by a -//- in each bit line. No further scans are recorded until a PLC input is changed from outside. Once a PLC input is changed, the above steps are repeated. GB RB Ext-C1 Ext-C2 RetC2

13

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 4,

page 14 of 14

(please turn off mobile phones)

Example

Black Green Cyl 1 Cyl 2

14

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 1 of 17

(please turn off mobile phones)

Chapter 5.

CX-Programmer II

5.1. More Help with CX-Programmer


CX-Programmer has a good online help make use of it. However, it sometimes crashes the computer, so open it from the start menu: Start | Programs | OMRON | CX-One | CX-Programmer | CX-Programmer | Help.

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 2 of 17

(please turn off mobile phones)

5.2. Opening an existing Project


Existing programs can be opened and run on any of our PLCs. However, if ever you want to run a program on a different model of Omron PLC, you must change the PLC type. To do this, double click on the PLC line in the Project Workspace. A dialogue opens which allows you to change o The PLCs name, o the PLC type o the Network Type You can choose the PLCs name. For the Device Type, select CP1L. The Network Type should be set to USB.
2

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 3 of 17

(please turn off mobile phones)

5.3. Special Instructions:


In addition to the logic achievable by arranging a rungs contacts in different AND and OR arrangements, most PLCs have a variety of special built in instructions or functions.

To access one of these, click on the New PLC Instruction icon The New Instruction dialogue opens.

Enter the name or number of the instruction in the top space.

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 4 of 17

(please turn off mobile phones)

The corresponding bit address or name is entered in the space labelled Operands Some instructions require more than one operand. This is indicated below the Operands box. The operands are placed one below the other.

If the small instruction dialogue opens, either expand it by clicking on Details, or enter the instructions name and operands separated by spaces.
4

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 5 of 17

(please turn off mobile phones)

If you want help with an instruction, click on Instruction Help in the expanded New Instruction dialogue. This opens a help file specific to the instructions supported by the PLCs we use.

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 6 of 17

(please turn off mobile phones)

One important special instruction is the END(01) instruction. Every ladder program in CX-Programmer has to end with an END(01) instruction. When you open a new program, this is inserted for you in a special section called END.

Make sure that you dont delete this.

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 7 of 17

(please turn off mobile phones)

5.4.

Some more CX-Programmer functions

The KEEP Instruction In the programs you wrote last week, the cylinders all returned to their retracted positions as soon as the input button(s) were released. In practice we dont want to have to hold down the start button all the time to keep our factory running. This can be achieved by using the output/coil as a rung input/contact, so that once the output/coil is set high (by pressing the green button), it keeps itself high. It then stays high until the red button is pressed.
7

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 8 of 17

(please turn off mobile phones)

CX-Programmer has a special instruction that achieves the same result. This is the KEEP(11) instruction we saw before. This instruction is used as the output/coil to a rung. It requires two input branches. The top Branch sets the output/coil high, and it stays high until it is reset (cancelled) by the bottom branch going high. Set Reset

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 9 of 17

(please turn off mobile phones)

Differentiate up DIFU(13)
00.09
DIFU(13)

Using the buttons as inputs as we do, the input goes high when the button is pressed and stays high until it is released. However, sometimes we want a bit to go high for only one program scan. CX-Programmer has a built-in instruction (DIFU) that achieves this.

GB 00.11 RB W0.03 DU

W0.03

W0.01

The argument of this instruction goes high for one program scan after a truth path has been established to the GB instruction and then goes low again. RB DU
9

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 10 of 17

(please turn off mobile phones)

Differentiate down DIFD(14)

00.09

The argument of this instruction goes high for one GB program scan after a truth path that did exist is 00.11 broken. Its time chart looks as follows:
RB W0.03 DD

DIFD(14 )

W0.03 00.08 BB

W0.01

GB RB BB DD
10

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 11 of 17

(please turn off mobile phones)

5.5. Timers
Timers are PLC bits which go high after predetermined time. They are used as a rung output and go high after a truth path has been maintained for a set period.

A timer is inserted as for other special functions clicking on the and selecting the instruction name TIM.

icon

It requires two arguments: An identification number in the range 0-4095 A set value (the time to count down from) in tenths of a second and preceded by a #.

11

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 12 of 17

(please turn off mobile phones)

A timer can then drive other PLC bits, by being used as an input/contact to another rung.
00.09 TIM

This is achieved by using the bit address Tn In this program, T25 goes high 7.5 seconds after the green button is pressed. So its time chart looks as follows: GB T25 Ext-C3
7.5d

GB

025 #0075

T25 7.5 sec

100.0 Ext-C3

The timer goes low again as soon as the truth path activating it goes low.

12

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 13 of 17

(please turn off mobile phones)

If the truth path goes low before the timer has gone high, the timer wont go high at all, and any time that been accumulated is lost.

00.09 TIM GB 025 #0075 T25 7.5 sec 100.0 Ext-C3

GB T25 Ext-C3
7.5d 7.5d

13

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 14 of 17

(please turn off mobile phones)

5.6.

Counters (CNT)

A counter (CNT) is a special instruction which, when used as a rung output, goes high after a truth path pulses (is established) a predetermined number of times. A counter is selected by entering the special function CNT, it requires two arguments: An identification number A Set Point - The number to count down from, (preceded by a #).
00.09

A counter has two input sub-rungs, the upper sub-rung is the pulse that gets counted, the lower rung resets the counter, back to its set point.

CNT GB
00.08

027 #003

BB

14

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 15 of 17

(please turn off mobile phones)

When used as a rung input/contact, the counter is identified by Cn where n is its identification number.
00.09

For example,
GB
00.08

CNT 027 #003

In the above program, when the black button is pressed, the value of the counter becomes equal to the set point. Each time the green button is pressed, the value of the counter decreases by one.

BB
C27 100.05

3 times

Ext-C3

When the counter value equals zero (after 3 presses of the green button), the counter bit C27 goes high. When the black button is pressed again, the counter is reset the counter value returns to the set point and the counter bit goes low.
15

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 16 of 17

(please turn off mobile phones)

00.09

CNT GB
00.08

027 #003

BB
C27 100.05

So its time chart looks as follows:


3 times ExtRB

GB BB C27
Counter value
16

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 5,

page 17 of 17

(please turn off mobile phones)

If the black button is pressed before the counter value reaches zero, the counter value returns to the set point again. GB BB C27
Counter value

To ensure that the counter counts down from the correct value, it is best to reset the counter before each time it is used, particularly the first time. Note: Timers and counters use the same addresses, so if timer T3 has been used in a program, counter C3 cant be used.

17

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 6,

page 1 of 9

(please turn off mobile phones)

Chapter 6.

CX-Programmer III

6.1. Naming Bits


It is often difficult to remember memory addresses. We therefore attach a name to each bit.

This makes programs easier to write and, easier to follow (particularly for someone else).

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 6,

page 2 of 9

(please turn off mobile phones)

To create names; Go offline ( PLC | WORK ONLINE, Ctrl+W, or )

Double click on symbols in the Project Workspace. This will open the symbol table.

Right click in the table and then click on Insert Symbol

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 6,

page 3 of 9

(please turn off mobile phones)

This will open the New Symbol dialogue. Enter the bits memory address, and the name that you want to use. Leave data type as BOOL. Click OK. When you want to continue writing your program, double click on Section 1 in the project workspace.

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 6,

page 4 of 9

(please turn off mobile phones)

The rules for a bits name are: 1. The name cannot begin with a number, 2. The name cannot contain punctuation marks (except underscore), 3. The name cannot contain spaces, 4. The name cannot be similar to an address, 5. Names are case sensitive. Whenever a Contact Dialogue or Coil Dialogue opens, you can now enter the bits name instead of its memory address.

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 6,

page 5 of 9

(please turn off mobile phones)

Bits can also have comments. These are inserted in the same dialogue used to give the bit a name.

The first few words of each comment are also displayed in the ladder diagram.

To reduce overcrowding, the comments can be turned off, by clicking on View | Show Symbol Comments, Alt-Y, or

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 6,

page 6 of 9

(please turn off mobile phones)

6.2.

Exporting and Importing Symbols

It would be good if we could copy some symbols or an entire symbol table from one program to another. This would save us from having to re-enter commonly used bit names like BlackPB, GreenPB, extendCyl_3. This can be done as follows: 1. Select one or more symbols in the symbol table or the entire table by clicking on Symbols in the Project Workspace. 2. Copy and paste it to a new document in Word or Excel 3. This file can now be saved. To import the symbols: 1. Open the different file in CX-Programmer 2. Open the Symbol table 3. Copy and paste the Symbols from Word or Excel into the Symbol table. More details about the required formats are in the online help.
6

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 6,

page 7 of 9

(please turn off mobile phones)

6.3. PLC Monitoring


CX-Programmer is able to display the current status of the PLC bits and the TRUTH PATHS.

To do this, click on PLC | Monitor | Monitoring , Crtl+M, or Whenever you are working online, all bits that are high and truth paths get highlighted in green. Note that it may also be necessary to turn on window monitoring View | Monitoring or to get monitoring to work.

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 6,

page 8 of 9

(please turn off mobile phones)

When you have PLC Monitoring active, the current value of timers and counters is displyed. While the truth path is active, you can see the current value counting down from the set point to zero .
Timer Address

Current Value

Set point

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 6,

page 9 of 9

(please turn off mobile phones)

6.4.

Differential Monitoring

Sometimes a bit is high for only a very brief moment but, when debugging the program, we need to determine whether it went high at all. This can be done by using differential monitoring. When online and in monitor mode or run mode, click on a bit in the PLC program. Click on PLC | Monitor | Differential Monitor or dialogue box. . This opens a special

As the PLC runs, the dialogue indicates every time the selected bit goes high (or low), by flashing a coloured square, keeping a count, and making a sound. The Differential Monitor tool is useful for checking whether a bit being driven by a DIFU is working properly. See section 5.4.

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 7,

page 1 of 2

(please turn off mobile phones)

Chapter 7.

More about time charts

7.1. Modelling a more complex program.

GB DU_GB DUGB_C3ext Cylinder 3

We see that this program performs a toggle function.


1

300044 Microcontrollers and PLCs 2011 Written by Dr Jonathan Vincent (Robotic and Mechatronic Eng.)

Chapter 7,

page 2 of 2

(please turn off mobile phones)

7.2.

The Importance of Rung Order

GB GB(L-H-Cext) Ext-C2 OldGB

In an electrical circuit, every rung is executed simultaneously and continuously. In a PLC ladder program, however, the order in which rungs appear is very important. Looking at the preceding ladder diagram with the last rung moved to the top:
2