2.3. Programming

Download project Exercises.zip

2.3.1. Basic operations

../../../_images/mainLogic.png

2.3.1.1. Contact and Coils

../../../_images/contact.gif

Contact-Coil in ladder and its equivalent in SCL

2.3.1.2. Trigger

../../../_images/r_trig.jpg
../../../_images/f_trig.jpg
../../../_images/trigLD.gif

R_TRIG positive signal edge in ladder

../../../_images/trigSCL.gif

R_TRIG positive signal edge in SCL

2.3.1.3. Timers

../../../_images/ton.png
../../../_images/tonLD.gif

TON (On delay) in ladder

../../../_images/tof.png
../../../_images/tofLD.gif

TOF (Off delay) in ladder

../../../_images/tonSCL.gif

TON (On delay) in SCL

2.3.1.4. Set Reset

../../../_images/set_reset.gif

Set Reset a signal

../../../_images/set_coil_err.gif

Why the output didn’t change value?

../../../_images/set_coil_err.png

What is wrong in this code ????

2.3.2. SCL

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.
../../../_images/if.png

If statement

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.
../../../_images/switch.png

Switch Case statement

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;

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.