1.2.3. Programming¶
Download project Exercises.zip
1.2.3.1. Basic operations¶
The following images show the basics operations used in Ladder languagge. Mainly contacts, coils, timers and edge detection are often used. In the images are missing variables.
1.2.3.1.1. Contact and Coils¶
A contact and a coil are the most basic operations in PLC. They correspond to an assignment operation.
Written as IF statement:
IF pushButtonOn = TRUE THEN
lamp := TRUE;
ELSE
lamp := FALSE;
END_IF
1.2.3.1.2. Trigger¶
Sometimes we need to detect the change in a signal. When detection from 0 to 1 is needed an R_TRIG (rising edge) is used. The output Q of R_TRIG is one only in the cycle where the signal CLK go from 0 to 1, otherwise is always 0.
The falling egde detector, F_TRIG, detect the transtion from 1 to 0. The output Q is 1 only in the cycle where the CLK go from 1 to 0, otherwise is always 0.
The follwing animation show how to use the edge detectors. You will never see the output Q go to 1 while debugging. The cylce time of the PLC is too short, between 1ms and 10 ms. But we can see its result.
1.2.3.1.3. Timers¶
There are different types of timers. The TON, Time On delay, is used to delay its input. It have two input variables and two outputs. The IN is the variable or signal we want to delay of a certain time PT. The output Q is the result of the delay. When IN go to 1, Q will go to 1 after a time PT.
The TOF timer, is a Time Off delay. The output Q go to zeroa after PT time from when IN go to zero.
The following animation show how to call timers in SCL language.
1.2.3.1.4. Set Reset¶
While the normal coil corrispond to an assignment. The set and reset coils corrispond to IF conditions.
When the condition is true, the variable is written to true:
IF pushButtonOn = TRUE THEN
lamp := TRUE;
END_IF
When the condition is true, the variable is written to false:
IF pushButtonOn = TRUE THEN
lamp := FALSE;
END_IF
Note that there is no ELSE statement. While in a normal coil there is one.
The following animation show how the set reset coils works.
The follwing animation show a common error made by beginners.
In Ladder language you can use the set coil a lot of times with the same variable. But you can’t use a normal coil with that same variable. The reason is the set coil is assigned only if the consition are true otherwise keep its old value. A normal coil update always the value of the variable to true or false.
The oLamp variable in the set coil is assigned the value true if the output of the trigger is true, otherwise it keep its old value. The oLamp varible in the normal coil is assigned the value true if the varible of the contact is true, and the value of false if the value of the contact is false.
1.2.3.2. SCL¶
1.2.3.2.1. If statement¶
Think about the if
statement as you think in daily life.
For example:
- If today is raining I take umbrella
- If it is cold I put a coat
- I you find orange then buy, otherwise buy apple.
1.2.3.2.2. Case statement¶
Case is like if, it check if the numerical value of the variable is present in the list, and execute the instruction corresponding to that value.
For example let create a variable day
of type int
. The first day of the week is one the last day is seven.
So If I want to make a decision tree, I list in the case
statement days from 1 to 7, and for every value I do something:
- If day is 1 (Monday), I go to work
- If day is 2, I do something else
- …
- If day is 6, I stay at home.
Remember that a case can be written also as an if.
The Case statement is more suitable in state machine
. In Siemens there is no enumeration
data type. In Tia portal siemens introduce CONSTANTS, so we can emulate an enumeration
. It is more clear to have name than numbers. For example, is more clear to say Monday than day 1. And if Day 1 for me is Sunday? So is better to create a set of CONSTANTS with unique value and use them.
int today;
const int MONDAY := 1;
const int TUESDAY := 2;
const int WEDNESDAY := 3;
const int THURSDAY := 4;
const int FRIDAY := 5;
const int SATURDAY := 6;
const int SUNDAY := 7;
CASE today OF
MONDAY:
I go to work;
SATURDAY:
I sleep more;
ELSE:
Error day is not recognized;
END_CASE;
1.2.3.2.3. Loop¶
Try to avoid for
and while
in PLC programming if you don’t know what are you doing. Infinite loops stop the plc.