03 Instruction
03 Instruction
And Operation:
AND operation is nothing but one or more than one NO/NC contact connected in series. AND operation
checks whether the state of the addressed bit is "1"or “0” and Ands the test result with the RLO. You can
use AND (A) instruction to check the signal state for a scan for zero or one. If the normally open relay
contact (I 0.0 = 0) is open, the check result is ‘0’, if it is closed the result is ‘1’.In AND operation all the
inputs has to be high to activate the output coil.
Format of AND Instruction: A (op-code) I (operand identifier) 0.0 (address)
The below figure shows an example with all three representation (i.e. LAD / FBD / STL) that uses an AND
instruction to program two normally open contacts in series.
= Q 4.0 Q 4.0
The AND instruction can also be used directly to check the status word bits for 0 and 1 value directly by
use of their addresses:
Example : A OV, A >0, A <>0 , A OS, A UO , A BR , A ==0 etc.
OR Operation:
OR operation is nothing but one or more than one NO/NC contact connected in parallel. OR operation
checks whether the state of the addressed bit is "1"or “0” and ORs the test result with the RLO. You can
use an OR ( O ) instruction to check the signal state of a normally open contact. If the normally open relay
SITRAIN_INDIA
Course folder, V1.0 3-1
Course Name: TIA Basic with STEP7
contact (I 0.0 =0)is open the check result is ‘0’, if it is closed the result is ‘1’. In OR operation any one of the
input will activate the output.
Format of OR Instruction: O (op-code) I (operand identifier) 0.0 (address)
The below figure shows an example with all three representation (i.e. LAD / FBD / STL) that uses an OR
instruction to program two normally open contacts in parallel.
The OR instruction can also be used directly to check the following status word bits by their addresses:
Example: O OV, O >0, O <>0, O OS, O UO , O BR , O ==0 etc.
I 0.1
I 0.0 1
Q 4.0
I 0.0 Q 4.0 ON I 0.0
= = Q 4.0 Q 4.0
OR OR
AN I 0.0
I 0.0 & = Q 4.0
Q 4.0
The OR Not, And Not instructions can also be used directly to check the following status word bits by their
addresses:
Example : 1. ON OV, ON >0, ON <>0, ON OS, ON UO, ON BR, ON ==0 etc.
2. AN OV, AN >0, AN <>0 , AN OS, AN UO , AN BR , AN ==0 etc
SITRAIN-INDIA
3-2 Course folder, V1.0
Course Name: TIA Basic with STEP7
L a d d er P r o g ra m S T L P r o g ra m R e l a y L o g i c c ir c u i t .
I 0.0 Q 4.0 X I 0 .0
I 0.0
X I 0 .1
I 0 .1
= Q 4 .0
I 0 .1
O R O R
I 0.0 Q 4.0 X N I 0 .0
Q 4 .0
X N I 0 .1
I 0 .1
= Q 4 .0
The X and XN instructions can also be used directly to check the following status word bits by their
addresses.
Example: 1. X OV, X >0, X <>0 , X OS, X UO , X BR , X ==0 etc.
2. XN OV, XN >0, XN <>0 , XN OS, XN UO , XN BR , XN ==0 etc
SITRAIN_INDIA
Course folder, V1.0 3-3
Course Name: TIA Basic with STEP7
The O function performs a logical OR instruction on AND functions according to the rule: AND before OR.
Or Before And:
Example:
Assign:
Example:
SITRAIN-INDIA
3-4 Course folder, V1.0
Course Name: TIA Basic with STEP7
= ---( ) (Output Coil) works like a coil in a relay logic diagram. If there is power flow to the coil (RLO = 1),
the bit at location <address> is set to "1". If there is no power flow to the coil (RLO = 0), the bit at location
<address> is set to "0". An output coil can only be placed at the right end of a ladder rung. Multiple output
elements (max. 16) are possible (see example). A negated output can be created by using the ---|NOT|---
(invert power flow) element.
If the signal state is "1" at input I0.0 and "0" at I0.1, memory bit M0.0 is reset and output Q4.0 is "0".
Otherwise, if the signal state at input I0.0 is "0" and at I0.1 is "1", memory bit M0.0 is set and output Q4.0 is
"1". If both signal states are "0", nothing is changed. If both signal states are "1", the set instruction
dominates because of the order; M0.0 is set and Q4.0 is "1".
If the signal state is "1" at input I0.0 and "0" at I0.1, memory bit M0.0 is set and output Q4.0 is "1".
Otherwise, if the signal state at input I0.0 is "0" and at I0.1 is "1", memory bit M0.0 is reset and output Q4.0
is "0". If both signal states are "0", nothing is changed. If both signal states are "1", the reset instruction
dominates because of the order; M0.0 is reset and Q4.0 is "0".
SITRAIN_INDIA
Course folder, V1.0 3-5
Course Name: TIA Basic with STEP7
The signal state of output Q4.0 is "1" if one of the following conditions exists: The signal state is "1" at
inputs I0.0 and I0.1 Or the signal state is "0" at input I0.2. If the RLO is "0", the signal state of output Q4.0
remains unchanged.
SITRAIN-INDIA
3-6 Course folder, V1.0
Course Name: TIA Basic with STEP7
Example: If the programmable logic controller detects a positive edge at contact I 1.0, it energizes the coil
at Q 4.0 for one OB1 scans cycle.
SITRAIN_INDIA
Course folder, V1.0 3-7
Course Name: TIA Basic with STEP7
| | | | | | (S A V E )
BR Q 4 .2 Q 5 .0
| | | | ( )
Change RLO: With STEP7 there are a number of instructions that affect the RLO. With SET you set the
result of logic operation to "1", with CLR to "0". Parallel to this, the status bit STA is also set to "1" or to
"0". Both instructions are executed independent of conditions. SET and CLR also reset the status bits OR
and /FC, that is, a new check string begins afterwards. The NOT negates the RLO.
BR Bit: The BR bit represents an internal bit memory, in which the RLO can be saved before an RLO
changing instruction. This is so that the RLO is then available for the resumption of the
interrupted bit string. If you write a function block or a function and want to call it in LAD, you have
to manage the BR bit. The BR bit corresponds to the enable output (ENO) for the LAD box.
Setting and Resetting BR: With SAVE you save the RLO in the binary result (register). SAVE transmits
the signal state from the RLO to the status bit BR. SAVE is executed independent of any conditions and
does not affect any further status bits.
RLO Concept:
Result of Check
Result of Check
Result of Logic
Result of Logic
Result of Logic
Signal State
Signal State
Signal State
First Check
First Check
First Check
Operation
Operation
Operation
:
:
= M 3.4
A I 1.0 0 0 0 1 1 1 1 1 1 1 1 1
AN I 1.1 0 1 0 1 1 0 0 1 0 1 1 1
A M 4.0 0 0 0 1 1 1 0 1 1 1 1 1
= Q 8.0 0 0 0 0 1 0
= Q 8.1 0 0 0 0 1 0
A I 2.0 0 0 0 1 1 1 1 1 0 0 0 1
SITRAIN-INDIA
3-8 Course folder, V1.0
Course Name: TIA Basic with STEP7
Signal State: A logic operation is made up of a series of instructions to check the states of signals (inputs
(I), outputs (Q), bit memories (M), timers (T), counters (C) or data bits (D) ) and instructions to set Q,M,T,C
or D.
Result of Check: When the program is executed, the result of check is obtained. If the check condition is
fulfilled, the result of check is “1”. If it is not fulfilled, the result of check is “0”.
First Check: The result of the first check is stored as the result of logic operation (RLO).
Result of Logic: When the next check instructions are executed, the result of logic operation is gated with
the result of check and a new RLO is obtained. When the last check instruction in a logic operation has
been executed, the RLO remains the same. A number of instructions using the same RLO can follow.
NO activated
contact 1 LAD: “Yes“ LAD: “No”
Yes
1 0
“NO contact” “NC contact”
not No
activated 0 “No” “Yes”
0 1
FBD: FBD:
Process: The use of normally open or normally closed contacts for the sensors in a controlled process
depends on the safety regulations for the process.Normally closed contacts are always used for limit
switches and safety switches, so that dangerous conditions do not arise if a wire break occurs in the
sensor circuit. Normally closed contacts are also used for switching off machinery for the same reason.
Symbols: In LAD, a symbol with the name "NO contact" is used for checking for signal state "1" and a
symbol with the name "NC contact" to check for signal state "0". It makes no difference whether the
process signal “1” is supplied by an activated NO contact or a non-activated NC contact.
Example: The result of check for the "NO contact" symbol is "1" if an NC contact in the machine is not
activated.
SITRAIN_INDIA
Course folder, V1.0 3-9
Course Name: TIA Basic with STEP7
Task : In all three examples the light should be on when S1 is activated and S2 is not activated!
Hardware
S1 S2 S1 S2 S1 S2
II1.0
1.0 II1.1
1.1 II1.0
1.0 II1.1
1.1. II1.0
1.0 II1.1
1.1
Programmable controller Programmable controller Programmable controller
Q 4.0 Q 4.0 Q 4.0
Software
I 1.0 I 1.1 Q 4.0 I 1.0 I 1.1 Q 4.0 I 1.0 I 1.1 Q 4.0
LAD
FDB
I 1.1 Q 4.0 I 1.1 Q 4.0 I 1.1 Q 4.0
Note ! The terms "NO contact" and "NC contact" have different meanings depending on whether they are
used in the process hardware context or as symbols in the software.
SITRAIN-INDIA
3 - 10 Course folder, V1.0
Course Name: TIA Basic with STEP7
BOOL, BYTE, WORD, D WORD, CHAR: Variables of the data type BOOL consist of one bit, variables of
data types Byte, Word, DWord are sequences of 8, 16 & 32 bits respectively. The individual bits are not
evaluted in these data types. Special forms of these data types are the BCD numbers and the count value
used in conjunction with the count function & the data type CHAR, which represents a character in ASCII.
S5TIME: Variables of the data type S5TIME are required for specifying timer values in timer functions (S5
Timer functions). You specify the time in hours, minutes, seconds or milliseconds. You can enter the timer
values with an underline (1h_4m) or without an underline (1h4m). Functions FC 33 and FC40 from the
library convert S5TIME to TIME format and TIME to S5TIME format.
SITRAIN_INDIA
Course folder, V1.0 3 - 11
Course Name: TIA Basic with STEP7
Data Type INT (16-Bit Integers): An integer has a sign that indicates whether it is a positive or negative
integer. The space that an integer (16 bits) occupies in the memory is one word. The following table shows
the range of an integer (16 bits).
Format: Integer (16 bits ), Range: -32768 to 32768
The following figure shows the integer +44 as a binary number:
15 12 11 8 7 4 3 0
0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 0
Data Type DINT (32-Bit Integers): A double integer has a sign that indicates whether it is a positive or
negative integer. The space that a double integer occupies in the memory is two words. The following
table shows the range of a double integer.
Format: Integer (32 bits), Range: –2 147 483 648 to +2 147 483 647
The following figure shows the integer –500 000 as a binary number. In the binary system, the negative
form of an integer is represented as the twos complement of the positive integer. You obtain the twos
complement of an integer by reversing the signal states of all bits and then adding +1 to the result.
31 24 23 16 15 8 7 0
11111111 11111000 01011110 11100000
Sign
Decim al Value
Data Type REAL (Floating-Point Numbers): This type of number representation has the advantage of
being able to represent both very large and very small values within a limited space. With the limited
number of bits for the mantissa and exponent, a wide range of numbers can be covered. The
disadvantage is in the limited accuracy of calculations. For example, when forming the sum of two
numbers, the exponents must be matched by shifting the mantissa (hence floating decimal point) since
only numbers with the same exponent can be added. Floating-point numbers in STEP 7 conform to the
basic format, single width, described in the ANSI/IEEE standard 754–1985, IEEE Standard for Binary
Floating-Point Arithmetic.They consist of the following components:
The sign S
The exponent e = E + bias, increased by a constant (bias = +127)
The fractional part of the mantissa m. The whole number part of the mantissa is not stored with the
rest, because it is always equal to 1 within the valid number range.
SITRAIN-INDIA
3 - 12 Course folder, V1.0
Course Name: TIA Basic with STEP7
The three components together occupy one double word (32 bits):
0 1 0 0 0 0 1 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2-1 2-23
S Exponent: e = 8 Bit
Mantissa: f (23 Bits)
Using the three components S, e, and f, the value of a number represented in this form is defined by the
formula:
Examples :
SITRAIN_INDIA
Course folder, V1.0 3 - 13
Course Name: TIA Basic with STEP7
Load Operations :
Accumulator 1
Load Function
L Stack
Load operation brings the contents of different memory areas (as shown in the above figure) into the
accumulator 1.
SITRAIN-INDIA
3 - 14 Course folder, V1.0
Course Name: TIA Basic with STEP7
Transfer Operations :
Accumulator 1
Transfer Function
L Stack Address
Registers
Transfer operation transfers the contents of accumulator 1 to the different memory areas (as shown in the
above figure).
PII
7 6 5 4 3 2 1 0
Reading Bit by Bit into the “RLO” ( A I 0.2 ) I
Byte 0
0
0
Higher – Order Word Lower – Order Word L IB 10 Byte10
Word
00 00 Word
00 10
Higher Byte Lower Byte Higher Byte 0
Lower Byte
0 0 0 Byte16
L IW 16
Byte Byte Byte Byte Byte17
00 00 16 6
7
0 0
L ID 20 Byte20
20 Byte21
0Byte22
1Byte23
2
31 16 15 0 3
1 6 5
Format Of Load Operation: L <address>
Address can be PII, PIQ, Periphery Input, DB Area, Bit Memory Area, Constants, Timers, Counters With
data type as Byte, Word or Double Word. Example: L IW 4, L MW 12, L DB1.DBW0 etc.
SITRAIN_INDIA
Course folder, V1.0 3 - 15
Course Name: TIA Basic with STEP7
PIQ
7 6 5 4 3 2 1 0
Assignment of the “RLO” ( = Q 0.2 )
Byte 0
0
0
Higher – Order Word Lower – Order Word T QB10 Byte10
Word
00 00 Word00
Higher Byte Lower Byte Higher Byte 0
Lower Byte
0 0 0 Byte16
T QW16
Byte Byte Byte Byte Byte17
00 00 6
7
0 0
T QD20 Byte20
Byte21
0Byte22
1Byte23
2
31 16 15 0 3
1 6 5
Format Of Transfer Operation: T <address>
Address can be PII, PIQ, Periphery Output, DB Area, and Bit Memory Area with data type as Byte, Word
or Double Word. Example: T QW 4, T MW 12, T DB1.DBW0 etc.
DI PII PIQ DQ
L IW 0 T QW 0
I 0.0 Q 0.0
I 0.1 Q 0.1
I 0.2 Q 0.2
I 0.3 Q 0.3
I 0.4 Q 0.4
I 0.5
Accumulator 1 Q 0.5
I 0.6 Q 0.6
I 0.7 Q 0.7
Previous
I 1.0
Contents Of Q 1.0
I 1.1
Accumulator 1 Q 1.1
I 1.2 Q 1.2
I 1.3 Q 1.3
I 1.4
Accumulator 2 Q 1.4
I 1.5 Q 1.5
I 1.6 Q 1.6
I 1.7 Q 1.7
LOST
Load operation brings the memory area into the accumulator 1, previous contents of accumulator 1 gets
transferred to the accumulator 2. And the accumulator 2 gets lost.
SITRAIN-INDIA
3 - 16 Course folder, V1.0
Course Name: TIA Basic with STEP7
Points to Note:
The Load & Transfer functions are not dependent on “RLO” or the status bits
When a byte is loaded, its contents are written right-justified into Accumulator 1.
The remaining bytes in Accumulators are padded with “0”.
The multiple load and transfer operations are possible.
Examples on Load instuctions:
STL Explanation
L DBB12 Load data byte DBB12 into ACCU 1-L-L.
L DIW15 Load instance data word DIW15 into ACCU 1-L.
L LD252 Load local data double word LD252 ACCU 1.
L P# I 8.7 Load the pointer into ACCU 1.
L “OTTO” Load the parameter "OTTO" into ACCU 1.
L P# ANNA Load the pointer to the specified parameter in ACCU 1.
(This instruction loads the relative address offset of the specified parameter. To calculate the absolute
offset in the instance data block in multiple instance FBs, the contents of the AR2 register must be added
to this value)
SITRAIN_INDIA
Course folder, V1.0 3 - 17
Course Name: TIA Basic with STEP7
SITRAIN-INDIA
3 - 18 Course folder, V1.0
Course Name: TIA Basic with STEP7
indicates that the result of the comparison is true; RLO = 0 indicates that the result of the comparison is
false. The status word bits CC 1 and CC 0 indicate the relations ‘’less,’’ ‘’equal,’’or ‘’greater.”
The values at inputs IN1 and IN2 are compared for conformity with the specified condition:
== D IN1 is equal to IN2
<> D IN1 is not equal to IN2
>D IN1 is greater than IN2
<D IN1 is less than IN2
>= D IN1 is greater than or equal to IN2
<= D IN1 is less than or equal to IN2.
Example (STL)
L MD10 //Load contents of MD10 (double integer, 32 bits).
L ID24 //Load contents of ID24 (double integer, 32 bits).
>D //Compare if ACCU 2 (MD10) is greater (>) than ACCU 1 (ID24).
= M 2.0 //RLO = 1 if MD10 > ID24
SITRAIN_INDIA
Course folder, V1.0 3 - 19
Course Name: TIA Basic with STEP7
Comparision Operation:
LAD FBD STL
SITRAIN-INDIA
3 - 20 Course folder, V1.0
Course Name: TIA Basic with STEP7
EN ENO
IN1
IN2 O UT
SITRAIN_INDIA
Course folder, V1.0 3 - 21
Course Name: TIA Basic with STEP7
-I //Subtract ACCU 1-L from ACCU 2-L; store the result in ACCU 1-L.
T DB1.DBW25 //The contents of ACCU 1-L(result) are transferred to DBW25 of DB1.
Example 2:
L ID10 //Load the value of ID10 into ACCU 1.
L MD14 // Load the contents of ACCU1 into ACCU2. Load the value of MD14 into ACCU1.
-D // Subtract ACCU 1 from ACCU 2; store the result in ACCU 1.
T DB1.DBD25 //The contents of ACCU 1 (result) are transferred to DBD25 of DB1.
Divide : ( /I and /D ):
/I (divide 16-bit integer numbers) divide the contents of ACCU2-L by the contents of ACCU1-L. The
contents of ACCU1-L and ACCU2-L are interpreted as 16-bit integer numbers. The result is stored in
ACCU1 and consists of two 16-bit integer numbers, the quotient & the remainder. The quotient is stored in
ACCU1-L & the remainder in ACCU1-H. The instruction is executed without regard to, and without
affecting, the RLO.
/D (divide 32-bit integer numbers) divides the contents of ACCU2 by the contents of ACCU1. The contents
of ACCU1 and ACCU2 are interpreted as 32-bit integer numbers. The result of the instruction is stored in
ACCU1. The result gives only the quotient and not the remainder. (The instruction MOD can be used to
get the remainder). The instruction is executed without regard to, and without affecting, the RLO.
Example 1:
L IW10 // Load the value of IW10 into ACCU 1-L.
L MW14 // Load contents of ACCU1-L into ACCU2-L & value of MW14 into ACCU 1-L.
SITRAIN-INDIA
3 - 22 Course folder, V1.0
Course Name: TIA Basic with STEP7
/I //Divide ACCU 2-L by ACCU 1-L; store the result in ACCU 1: ACCU 1-L: quotient,
ACCU 1-H: remainder
T MD20 // The contents of ACCU 1 (result) are transferred to MD20.
Example (13 divided by 4)
Contents of ACCU 2-L before instruction (IW10): "13"
Contents of ACCU 1-L before instruction (MW14): "4"
Instruction /I (ACCU 2-L / ACCU 1-L): "13/4"
Contents of ACCU 1-L after instruction (quotient): "3"
Contents of ACCU 1-H after instruction (remainder): "1"
Example 2:
L ID10 // Load the value of ID10 into ACCU 1.
L MD14 //Load the contents of ACCU 1 into ACCU 2. Load the value of MD14 into ACCU
1.
/D //Divide ACCU 2 by ACCU 1; store the result (quotient) in ACCU 1.
T MD20 //The contents of ACCU 1 (result) are transferred to MD20.
Example (13 divided by 4)
Contents of ACCU 2 before instruction (ID10): "13"
Contents of ACCU 1 before instruction (MD14): "4"
Instruction /D (ACCU 2 / ACCU 1): "13/4"
Contents of ACCU 1 after instruction (quotient): "3"
Using floating-point math, you can carry out the following operations with one 32- bit IEEE floating-point
number:
ABS Absolute Value
SQR Generate the Square
SQRT Generate the Square Root
EXP Generate the Exponential Value
LN Generate the Natural Logarithm
SIN Generate the Sine of Angles
SITRAIN-INDIA
3 - 24 Course folder, V1.0
Course Name: TIA Basic with STEP7
Addition: (+R):
+R (add 32-bit IEEE floating-point numbers) adds the contents of accumulator 1 to the contents of
accumulator 2 and stores the result in accumulator 1. The contents of accumulator 1 and accumulator 2
are interpreted as 32-bit IEEE floating-point numbers. The instruction is executed without regard to, and
without affecting, the RLO. The contents of accumulator 2 remain unchanged for CPUs with two ACCUs.
Example 1:
OPN DB10
L ID10 //Load the value of ID10 into ACCU 1.
L MD14 //Load the value of ACCU 1 into ACCU 2. Load the value of MD14 into ACCU 1.
+R //Add ACCU 2 and ACCU 1; store the result in ACCU 1.
T DBD25 //The content of ACCU 1 (result) is transferred to DBD25 in DB10.
Subtraction: (-R):
-R (subtract 32-bit IEEE floating-point numbers) subtracts the contents of accumulator 1 from the contents
of accumulator 2 and stores the result in accumulator 1. The contents of accumulator 1 and accumulator 2
are interpreted as 32-bit IEEE floating-point numbers. The result is stored in accumulator 1. The instruction
is executed without regard to, and without affecting, the RLO.
Example 1:
OPN DB10
L ID10 // Load the value of ID10 into ACCU 1.
L MD14 //Load the value of ACCU 1 into ACCU 2. Load the value of MD14 into ACCU 1.
-R //Subtract ACCU 1 from ACCU 2; store the result in ACCU 1.
T DBD25 //The content of ACCU 1 (result) is transferred to DBD25 in DB10.
Multiply: (*R):
*R (multiply 32-bit IEEE floating-point numbers) multiplies the contents of accumulator 2 by the contents of
accumulator 1. The contents of accumulator 1and accumulator 2 are interpreted as 32-bit IEEE floating-
SITRAIN_INDIA
Course folder, V1.0 3 - 25
Course Name: TIA Basic with STEP7
point numbers. The result is stored in accumulator 1 as a 32-bit IEEE floating-point number. The
instruction is executed without regard to, and without affecting, the RLO.
Example 1:
OPN DB10
L ID10 //Load the value of ID10 into ACCU 1.
L MD14 //Load the value of ACCU 1 into ACCU 2. Load the value of MD14 into ACCU 1.
*R //Multiply ACCU 2 and ACCU 1; store the result in ACCU 1.
T DBD25 //The content of ACCU 1 (result) is transferred to DBD25 in DB10.
Divide: ( /R ):
/R (divide 32-bit IEEE floating-point numbers) divides the contents of accumulator 2 by the contents of
accumulator 1. The contents of accumulator 1 and accumulator 2 are interpreted as 32-bit IEEE floating-
point numbers. The instruction is executed without regard to, and without affecting, the RLO.
Example 1:
OPN DB10
L ID10 // Load the value of ID10 into ACCU 1.
L MD14 // Load the contents of ACCU 1 into ACCU2. Load the value of MD14 into ACCU1.
/R //Divide ACCU 2 by ACCU 1; store the result in ACCU 1.
T DBD20 // The content of ACCU 1 (result) is transferred to DBD20 in DB10.
SITRAIN-INDIA
3 - 26 Course folder, V1.0
Course Name: TIA Basic with STEP7
jump label.
... (The reaction to an error that occurred happens here.)
OK: T DBD4 // Transfer the result from ACCU 1 to data double word DBD4.
SITRAIN_INDIA
Course folder, V1.0 3 - 27
Course Name: TIA Basic with STEP7
Example 1:
L MD10 // The value from memory double word MD10 is loaded into ACCU 1.
(This value must be in the floating-point format.)
LN //Calculate the natural logarithm of the floating-point number(32-bit, IEEE-FP) in
ACCU 1. Store the result in ACCU 1.
AN OV //Scan the OV bit in the status word for "0."
JC OK // If no error occurred during the instruction, jump to the OK jump label.
... //(The reaction to an error that occurred happens here.)
OK: T MD20 //Transfer the result from ACCU 1 to memory double word MD20.
Arc Functions
The arc functions are the inverse functions of the respective trigonometric functions. They expect a REAL
number in a specific value range in ACCU1 and return an angle in radian measure:
Function Permitted definition range Value range
ASIN -1 to +1 - pi/2 to + pi/2
ACOS -1 to +1 0 to pi
ATAN entire range - pi/2 to + pi/2
With an overrange of the permitted definition range, the arc functions return an invalid REAL
Number and set the status bits CC0, CC1, OV and OS to "1".
SITRAIN-INDIA
3 - 28 Course folder, V1.0
Course Name: TIA Basic with STEP7
N um ber C o n v e rs io n
e n te re d in B C D 0 8 1 5 B C D -> In te g e r
U s e r p ro g ra m w ith
in te g e r m a th
o p e ra tio n s
N um ber C o n v e rs io n
d is p la y e d 0 2 4 8 B C D < -In te g e r
in B C D
Example: A user program is to perform mathematical functions using values entered with push wheel
buttons and display the result on a digital display. Mathematical functions cannot be performed in BCD
format, so the format must be changed.
Conversion Instructions:
The instruction set of the S7-300/400 supports a multitude of conversion facilities. The instructions all have
the same format:
EN, ENO: If RLO is =1 at Enable input EN, the conversion is performed. Enable output ENO always has
the same signal state as EN. If this is not the case, it is clearly indicated in the corresponding
instructions.
IN: When EN=1, the value at IN is read into the conversion instruction.
OUT: The result of the conversion is stored at the address at the OUT output.
SITRAIN_INDIA
Course folder, V1.0 3 - 29
Course Name: TIA Basic with STEP7
SITRAIN-INDIA
3 - 30 Course folder, V1.0
Course Name: TIA Basic with STEP7
accumulator 1. Bit 0 to bit 27 contain the value of the BCD number. Bit 28 to bit 31 are set to the state of
the sign of the BCD number (0000 = positive, 1111 = negative). Accumulator 2 remains unchanged.
The BCD number can be in the range of "-9,999,999" to "+9,999,999". If the number is out of the
permissible range, then the status bits OV and OS are set to 1.
Example 1:
L MD10 // Load the 32-bit integer into ACCU 1.
DTB //Convert from integer (32-bit) to BCD, store result in ACCU 1.
T MD20 //Transfer result (BCD number) to MD20.
SITRAIN-INDIA
3 - 32 Course folder, V1.0
Course Name: TIA Basic with STEP7
SITRAIN_INDIA
Course folder, V1.0 3 - 33
Course Name: TIA Basic with STEP7
Example 1:
L ID8 // Load value into ACCU 1.
NEGD //Generate twos complement (32-bit).
T MD10 //Transfer result to MD10.
SITRAIN-INDIA
3 - 34 Course folder, V1.0
Course Name: TIA Basic with STEP7
Round (RND):
RND (conversion of a 32-bit IEEE floating-point number to 32-bit integer) interprets the contents of ACCU
1 as a 32-bit IEEE floating-point number (32-bit, IEEE-FP). The instruction converts the 32-bit IEEE
floating-point number to a 32-bit integer (double integer) and rounds the result to the nearest whole
number. If the fractional part of the converted number is midway between an even and an odd result, the
instruction chooses the even result. If the number is out of the permissible range, then the status bits OV
and OS are set to 1. The result is stored in accumulator 1. Conversion is not performed and an overflow
indicated in the event of a fault (utilization of a NaN or a floating-point number that cannot be represented
as a 32-bit integer number).
Example 1:
L MD10 //Load the floating-point number into ACCU 1-L.
RND //Convert the floating-point number (32-bit, IEEE-FP) into an integer (32-bit) and
round off the result.
T MD20 //Transfer result (double integer number) to MD20.
Truncate (TRUNC):
TRUNC (conversion of a 32-bit IEEE floating-point number to 32-bit integer) inter-prets the contents of
ACCU1 as a 32-bit IEEE floating-point number. The instruction converts the 32- bit IEEE floating-point
number to a 32-bit integer (double integer). The result is the whole number part of the floating-point
number to be converted (IEEE rounding mode "round to zero"). If the number is out of the permissible
range, then the status bits OV and OS are set to 1. The result is stored in accumulator 1. Conversion is not
performed and an overflow indicated in the event of a fault (utilization of a NaN or a floating-point number
that cannot be represented as a 32-bit integer number).
Example 1:
L MD10 // Load the floating-point number into ACCU 1-L.
TRUNC //Convert the floating-point number (32-bit, IEEE-FP) to an integer (32-bit) and
round result. Store the result in ACCU 1.
T MD20 // Transfer result (double integer number) to MD20.
SITRAIN_INDIA
Course folder, V1.0 3 - 35
Course Name: TIA Basic with STEP7
SITRAIN-INDIA
3 - 36 Course folder, V1.0
Course Name: TIA Basic with STEP7
Time Value: Bits 0 through 9 of the timer word contain the time value in binary code. The time value
specifies a number of units. Time updating decrements the time value by one unit at an interval designated
by the time base. Decrementing continues until the time value is equal to zero. You can load a time value
into the low word of accumulator 1 in binary, hexadecimal, or binary coded decimal (BCD) format.
You can pre-load a time value using either of the following formats:
W#16#wxyz
Where w = the time base (that is, the time interval or resolution)
Where xyz = the time value in binary coded decimal format
S5T#aH_bM_cS_dMS
Where H = hours, M = minutes, S = seconds, and MS = milliseconds; a, b, c, d are defined
by the user.
The time base is selected automatically, and the value is rounded to the next lower number
with that time base.
The maximum time value that you can enter is 9,990 seconds, or 2H_46M_30S.
SITRAIN_INDIA
Course folder, V1.0 3 - 37
Course Name: TIA Basic with STEP7
Time Base: Bits 12 and 13 of the timer word contain the time base in binary code. The time base
defines the interval at which the time value is decrement by one unit. The smallest time base is 10 ms;
the largest is 10 s.
Time Base Binary Code for the Time Base
10 ms 00
100 ms 01
1s 10
10 s 11
Values that exceed 2h46m30s are not accepted. A value whose resolution is too high for the range
limits (for example, 2h10ms) is truncated down to a valid resolution. The general format for S5TIME has
limits to range and resolution as shown below:
Resolution Range
0.01 second 10MS to 9S_990MS
0.1 second 100MS to 1M_39S_900MS
1 second 1S to 16M_39S
10 seconds 10S to 2H_46M_30S
Bit Configuration in the ACCU1: When a timer is started, the contents of ACCU1 are used as the time
value. Bits 0 through 11 of the ACCU1-L hold the time value in binary coded decimal format (BCD formats:
each set of four bits contains the binary code for one decimal value). Bits 12 and 13 hold the time base in
binary code. The following figure shows the contents of the timer cell loaded with timer value 127 and a
time base of 1 second:
Choosing the right Timer: This overview is intended to help you choose the right timer for your timing
job.
SITRAIN-INDIA
3 - 38 Course folder, V1.0
Course Name: TIA Basic with STEP7
SITRAIN_INDIA
Course folder, V1.0 3 - 39
Course Name: TIA Basic with STEP7
10 2 10 1 10 0
Time Specifications:
Fixed time values specified as time constants (e.g.: S5T#100ms, S5T#35s, S5T#5m2s200ms,
S5T#2h2m2s50ms).
Time values to be changed by the machine operator using pushwheel buttons.
Process or recipe-dependent time values in memory words or data words.
Timer Cell: A special area of memory is reserved for timers in your CPU. This area contains a 16-bit word
for each timer address. bits 0 to 9 of the timer word contain the time value in binary code. When the timer
is updated, the time value is decremented by one unit at the interval defined by the time base.
L / BI: The address at the “BI” output contains the time value in the form of a 10-digit binary number
(without time base!).
LC / BCD: The address at the “BCD” output contains both the time value as a 3-digit BCD number (12bits)
and the time format (bits 12 and 13).
Note: IEC-compliant timers can also be implemented in STEP7. The system function blocks need to be
used for implementing IEC timers.
SITRAIN-INDIA
3 - 40 Course folder, V1.0
Course Name: TIA Basic with STEP7
Pulse Timer: S_PULSE (Pulse S5 Timer) starts the specified timer if there is a positive edge at the start
(S) input. A signal change is always necessary in order to enable a timer. The timer runs as long as the
signal state at input S is "1", the longest period, however, is the time value specified by input TV. The
signal state at output Q is "1" as long as the timer is running. If there is a change from "1" to "0" at the S
input before the time interval has elapsed the timer will be stopped. In this case the signal state at output Q
is "0". The timer is reset when the timer reset (R) input changes from "0" to "1" while the timer is running.
The current time and the time base are also set to zero. Logic "1" at the timer's R input has no effect if the
timer is not running. The current time value can be scanned at the outputs BI and BCD. The time value at
BI is binary coded, at BCD it is BCD coded. The current time value is the initial TV value minus the time
elapsed since the timer was started.
T4 T4 A I 0.7
I 0.7 S_PULSE Q8.5 S_PULSE L S5T#35s
S Q I 0.7 S BI MW0 SP T4
S5T#35s BI MW0 A I 0.5
TV S5T#35s TV BCD QW12
I 0.5 Q8.5 R T4
R BCD QW12 I 0.5 R = L T4
Q
T MW0
LC T4
T QW12
A T4
= Q8.5
If the signal state of input I0.7 changes from "0" to "1" (positive edge in RLO), the timer T4 will be
started. The timer will continue to run for the specified time of thirty five seconds (35 s) as long as I0.7
is "1". If the Signal State of I0.7 changes from "1" to "0" before the timer has expired the timer will be
stopped. If the Signal State of input I0.5 changes from "0" to "1" while the timer is running, the time is
reset. The output Q8.5 is logic "1" as long as the timer is running and "0" if the time has elapsed or
was reset.
SITRAIN_INDIA
Course folder, V1.0 3 - 41
Course Name: TIA Basic with STEP7
Extended Pulse Timer: S_PEXT (Extended Pulse Timer) starts the specified timer if there is a positive
edge at the start (S) input. A signal change is always necessary in order to enable a timer. The timer
runs for the preset time interval specified at input TV even if the signal state at the S input changes to
"0" before the time interval has elapsed. The signal state at output Q is "1" as long as the timer is
running. The timer will be restarted ("re-triggered") with the preset time value if the signal state at input
S changes from "0" to "1" while the timer is running. The timer is reset if the reset (R) input changes
from "0" to "1" while the timer is running. The current time and the time base are set to zero. The
current time value can be scanned at the outputs BI and BCD. The time value at BI is binary coded, at
BCD is BCD coded. The current time value is the initial TV value minus the time elapsed since the timer
was started.
T4 T4 A I 0.7
I 0.7 S_PEXT Q8.5 S_PEXT L S5T#35s
S Q MW0 SE T4
I 0.7 S BI
S5T#35s TV BI MW0 A I 0.5
S5T#35s TV BCD QW12
I 0.5 A8.5 R T4
R BCD QW12 I 0.5 R L T4
Q =
T MW0
LC T4
T QW12
A T4
= Q8.5
If the signal state of input I0.7 changes from "0" to "1" (positive edge in RLO), the timer T4 will be
started. The timer will continue to run for the specified time of thirty five seconds (35 s) even if I0.7 is
"0". If the Signal State of input I0.5 changes from "0" to "1" while the timer is running, the timer is reset.
The output Q8.5 is logic "1" as long as the timer is running and "0" if the time has elapsed or was
reset. Also the output Q8.5 does not depend on the Input I0.7.
SITRAIN-INDIA
3 - 42 Course folder, V1.0
Course Name: TIA Basic with STEP7
On-Delay Timer: S_ODT (On-Delay S5 Timer) starts the specified timer if there is a positive edge at
the start (S) input. A signal change is always necessary in order to enable a timer. The timer runs for
the time interval specified at input TV as long as the Signal State at input S is positive. The signal state
at output Q is "1" when the timer has elapsed without error and the signal state at the S input is still "1".
When the Signal State at input S changes from "1" to "0" while the timer is running, the timer is stopped.
In this case the signal state of output Q is "0". The timer is reset if the reset (R) input changes from "0"
to "1" while the timer is running. The current time and the time base are set to zero. The signal state at
output Q is then "0". The timer is also reset if there is a logic "1" at the R input while the timer is not
running and the RLO at input S is "1". The current time value can be scanned at the outputs BI and
BCD. The time value at BI is binary coded, at BCD is BCD coded. The current time value is the initial
TV value minus the time elapsed since the timer was started.
T4 T4
I 0.7 S_ODT Q8.5 S_ODT A I 0.7
S Q I 0.7 S BI MW0 L S5T#35s
S5T#35s TV BI MW0 S5T#35s QW12
SD T4
TV BCD
I 0.5 Q8.5 A I 0.5
R BCD QW12 I 0.5 R Q = R T4
L T4
T MW0
LC T4
T QW12
A T4
= Q8.5
If the signal state of input I0.7 changes from "0" to "1" (positive edge in RLO), the timer T4 will be
started. The timer will continue to run for the specified time of thirty five seconds (35 s) as long as if I0.7
is "1". If the Signal State of input I0.5 changes from "0" to "1" while the timer is running, the timer is
reset. The output Q8.5 is logic "1" only when timer is elasped and "0" if the input I0.7 is “0”.
SITRAIN_INDIA
Course folder, V1.0 3 - 43
Course Name: TIA Basic with STEP7
Retentive On-Delay S5 Timer: S_ODTS (Retentive On-Delay S5 Timer) starts the specified timer if
there is a positive edge at the start (S) input. A signal change is always necessary in order to enable a
timer. The timer runs for the time interval specified at input TV even if the signal state at input S changes
to "0" before the time interval has elapsed. The signal state at output Q is "1" when the timer has
elapsed without regard to the signal state at input S. The timer will be restarted (re-triggered) with the
specified time if the signal state at input S changes from "0" to "1" while the timer is running. The timer is
reset if the reset (R) input changes from "0" to "1" without regard to the RLO at the S input. The signal
state at output Q is then "0". The current time value can be scanned at the outputs BI and BCD. The
time value at BI is binary coded, at BCD it is BCD coded. The current time value is the initial TV value
minus the time elapsed since the timer was started.
T4 T4 A I 0.7
I 0.7 S_ODTS Q8.5 S_ODTS L S5T#35s
S Q I 0.7 S BI MW0 SS T4
S5T#35s BI MW0 A I 0.5
TV S5T#35s TV BCD QW12
I 0.5 Q8.5 R T4
R BCD QW12 I 0.5 R Q = L T4
T MW0
LC T4
T QW12
A T4
= Q8.5
If the signal state of I0.7 changes from "0" to "1" (positive edge in RLO), the timer T4 will be started.
The timer runs without regard to a signal change at I0.7 from "1" to "0". If the signal state at I0.7
changes from "0" to "1" before the timer has expired, the timer will be re-triggered. The output Q8.5 will
be "1" if the timer elapsed. If the signal state of input I0.5 changes from "0" to "1", the time will be reset
irrespective of the RLO at S.
SITRAIN-INDIA
3 - 44 Course folder, V1.0
Course Name: TIA Basic with STEP7
Off-Delay Timer: S_OFFDT (Off-Delay S5 Timer) starts the specified timer if there is a negative edge
at the start (S) input. A signal change is always necessary in order to enable a timer. The signal state at
output Q is "1" if the signal state at the S input is "1" or while the timer is running. The timer is reset
when the signal state at input S goes from "0" to "1" while the timer is running. The timer is not restarted
until the signal state at input S changes again from "1" to "0".The timer is reset when the reset (R) input
changes from "0" to "1" while the timer is running.The current time value can be scanned at the outputs
BI and BCD. The time value at BI is binary coded, at BCD it is BCD coded. The current time value is the
initial TV value minus the time elapsed since the timer was started.
T4 T4 A I 0.7
I 0.7 S_OFFDT Q8.5 S_OFFDT L S5T#35s
S Q I 0.7 S BI MW0 SF T4
S5T#35s BI MW0 A I 0.5
TV S5T#35s TV BCD QW12
I 0.5 Q8.5 R T4
R BCD QW12 I 0.5 R Q = L T4
T MW0
LC T4
T QW12
A T4
= Q8.5
If the signal state of I0.7 changes from "1" to "0" (negative edge in RLO), the timer T4 will be started.
The timer runs without regard to a signal change at I0.7. If the signal state at I0.7 changes from "0" to
"1" before the timer has expired, the timer will be re-triggered. The output Q8.5 will be "1" as soon as
I0.7 is “1” & resets only when timer is elapsed. If the signal state of input I0.5 changes from "0" to "1",
the time will be reset & output becomes zero.
SITRAIN_INDIA
Course folder, V1.0 3 - 45
Course Name: TIA Basic with STEP7
Bit Instruction for Timer: All timer functions can also be started with simple bit instructions. The
similarities and differences between this method and the timer functions discussed so far are as follows:
Similarities:
Start conditions at the "S" input
Specification of the time value
Reset conditions at the “R” input
Signal response at output “Q”
Differences (for LAD and FBD):
It is not possible to check the current time value (there are no BI and BCD outputs).
Example:
Network 1:
T4
I 0.0 T4 I 0.0 & SD A I 0.0
SD L S5T#5s
S5T#5s TV SD T4
S5T#5s
Network 2:
T4 Q 8.0 Q 8.0
A T4
T4 & =
= Q 8.0
Network 3:
T4
I 0.1 T4 A I 0.1
R I 0.1 & R R T4
SITRAIN-INDIA
3 - 46 Course folder, V1.0
Course Name: TIA Basic with STEP7
Count Value: Bits 0 through 9 of the counter word contain the count value in binary code. The count value
is moved to the counter word when a counter is set. The range of the count value is 0 to 999. You can vary
the count value within this range by using the following counter instructions:
S_CUD Up-Down Counter
S_CD Down Counter
S_CU Up Counter
---( SC ) Set Counter Coil
---( CU ) Up Counter Coil
---( CD ) Down Counter Coil
Counters in STEP 7:
LA D FBD STL
A I0 .4
C5 C5
CU C5
S _C U D S _C U D
I 0.4 Q 8.3 A I0 .5
CU Q
Q I 0.4 CU CD C5
A I0 .3
I 0.5
L C #2 0
CD I 0.5 CD
S C5
I 0.3 A I0 .7
S I 0.3 S CV MW 4 R C5
L C5
C #2 0 PV CV MW 4 C #2 0 PV C V_ BC D QW 12 T MW4
Q 8 .3 LC C5
I 0.7
R CV _B C D QW 12 I 0.7 R Q
Q = T Q W 12
A C5
= Q 8.3
Counter Value: A 16-bit word is reserved for each counter in the system data memory. This is used for
storing the counter value for the counter (0...999) in binary code.
Count Up: When the RLO at the "CU" input changes from "0" to "1" the current counter reading is
incremented by 1 (upper limit = 999).
Count Down: When the RLO at the "CD" input changes from "0" to "1" the current counter reading is
SITRAIN_INDIA
Course folder, V1.0 3 - 47
Course Name: TIA Basic with STEP7
Set Counter: When the RLO at the "S" input changes from "0" to "1" the counter is set to the value
at the "CV" input.
Reset Counter:When RLO = 1 the counter is set to zero. If the Reset condition is fulfilled, the counter
cannot be set and counting is not possible.
PV:The preset value (0...999) is specified in BCD at the "PV" input. It is as a constant (C#...) in BCD
format via a data interface.
CV / CV_BCD: The counter value can be loaded as a binary number or BCD number into the accumulator
and transferred from there to other addresses.
Types of Counter:
S_CU = Up counter (counts up only)
S_CD = Down counter (counts down only)
S_CUD = Up/Down counter.
CU
CD
5
4
3
Count
2
1
0
SITRAIN-INDIA
3 - 48 Course folder, V1.0
Course Name: TIA Basic with STEP7
Notes: If the counter reaches the value 999 during up counting, or the value 0 during down counting, then
the count remains unchanged even in the case of further counting pulses.If up counting and down
counting is lined up at the same time, the count remains the same.
LA D FBD STL
N etw ork 1: C5
I 0 .0 SC
I 0.0 C5
A I 0 .0
SC L C#20
C # 20 CV
C#20 S C5
N etw ork 2:
C5
I 0.1 C5
I 0 .1 CU A I 0 .1
CU
CU C5
N etw ork 3:
I 0.2 C5 C5
A I 0 .2
CD I 0 .2 CD CD C5
N etw ork 4:
C5 Q 4.0 Q 4.0
A C5
C5 = = Q 4 .0
Bit Instructions: All counter functions can also operate with simple bit instructions. The similarities and
differences between this method and the counter functions discussed so far are as follows:
Similarities:
Setting conditions at the "SC" input
Specification of the counter value
RLO change at the "CU" input
RLO change at the "CD" input
Differences:
It is not possible to check the current counter value (there are no BI and BCD outputs).
There is no binary output Q in the graphical representation.
Note: IEC-compliant counters can also be implemented in STEP 7. The use of system function blocks for
implementing IEC counters is dealt with in an advanced programming course.
SITRAIN_INDIA
Course folder, V1.0 3 - 49