10 - API Course CHAPTER 5 PROGRAMMING
10 - API Course CHAPTER 5 PROGRAMMING
API Programming
API Programming
V.1. Introduction
The realization of all or part of a command section in programmed logic requires the
translation of the concerned model (grafcet, equations, ...) into an executable program by the API.
The development of such a program aims to write the output activation equations of the API and
the associated conditions in a language respond to theindustrial standards of thecommission
international electrotechnicsIEC 1131-3. This standard for programming languages of
programmable automation allows defining five usable programming languages, which
are:
Contact diagram (LD: Ladder Diagram)
Block Diagram (FBD: Function Block Diagram)
SFC (Sequential Function Chart) Language
Instruction List (IL)
Structured Text (ST).
The LD, FBD, and IL languages are available for all ranges of controllers, while the ST
and the SFC are only available for the S7-300/400, WINAC, and S7-1500 models.
To program the automation system, the automation engineer can use a programming console (portable)
or a computer equipped with programming software (for example: Step 7 for Siemens APIs or PL7 for
Schneider).
To send the program created to the memory of the automation, a serial connection is used between
the automated system and the computer or a specific cable when using a console.
In this chapter, we present the execution cycle of a paragrame by the programmable automation.
industrial, in the first place. Secondly, we define the notion of training and assignment. Then, we
describe the different programming languages of APIs. Then we will focus on the
programming of different operations in contact language. Programming in languages by
Blocks and a list of instructions are provided briefly at the end of the chapter.
46
Chapter V. API Programming
INTERVENING TREATMENT
READING INPUTS
TRAITEMENTDU PROGRAMME
WRITING OF OUTPUTS
The term scrutiny refers to the set of four operations carried out by the automaton and the time of
Scrutation is the time taken by the automaton to process the same part of the program. This time varies.
depending on the size of the program and the power of the controller, and it is on the order of ten
milliseconds for standard applications.
Scanning time
Response time
Figure V.2. Total scanning and response time.
The total response time is the time that elapses between a change of state of an input and the
Change of state of the corresponding output: The total response time is at most equal to two.
the time of scrutiny (without special treatment). This time is on the order of about twenty
milliseconds and is controlled by a timing called watchdog.
47
Chapter V. API Programming
Prefix Meaning
I (or E) Reading the state of an input.
Q (or A) Read/Write the state of an output.
M and V Lecture/Writing the state of an internal variable (memo)
SM Lecture/Writing a bit System
S Lecture/Writing a Sequential Relay Bit
C or (Z) Meters
SC Fast counters
T Timers
A or (P) Analog
% IEC 1131-3 standards
B Size of a byte or octet
W Word size: 16-bit word
D Size of a double word: double word of 32 bits
Table V.1. Meaning of the different symbols of an address.
Examples:
Input bit 4 module 0.
IW125: entry word 125.
Output bit 4 module 1.
QB17: Output octet 17.
M12.15: internal memory bit 12.15.
MD48: dual internal memory 48.
AIW 288: Analog Input 288.
AQW304: Analog Output 304.
Note 1: addressing can be absolute or symbolic. Absolute addressing involves using the
physical addresses of the automate. To bring clarity to the program, addressing is used.
symbolic. This amounts to substituting each physical address with a symbolic address that
best represents the function. The mnemonic assignment table is "the tool" used to
create a symbolic addressing.
I0.4: Q0.4:
I0.4 Q0.4 Marche Pompe Market Pump
The programming languages of APIs are of various natures given the diversity,
users who can use them. They can be classified into two families:
V.4.1. Graphic languages
They allow for as direct a graphic transcription of the patterns as possible in order to
facilitate programming tasks and reduce sources of uncertainties. We distinguish three
the following languages: Contact diagram, Block diagram, and Functional sequence diagram.
Contact diagram: LeLDest is the most used, very easy to handle, and ideal for
visualize and diagnose programs during maintenance operations. It is suitable
in combinatorial processing.
48
Chapter V. API Programming
< opérande > The contact is closed if the value of the queried bit saved in
Contact at
<operand> is 0. On the other hand, if the signal state in
opening
<operand> is 1, the contact is open. The current flows through the
contact and the operation yields a logical result (LRG) equal to 1
if and only if the contact is closed and the RLG at its input
equal 1.
49
Chapter V. API Programming
RLG Inverter NOT This operation inverts the logical result bit (LRB).
< operand > This operation is performed only if the RLG of the operations
S previous to the value 1. In this case, the N bits are set to 1,
Set to 1
N starting from the specified operand address.
50
Chapter V. API Programming
S E
E
t t
t
R Q4.0
Q4.0
t t
t
Q1.0 Q4.2
Q5.1
t t
t
M2.0
t
The symbols B, I, D, R, and S mean that the data format of inputs IN1 and IN2 is
octet, integer, double integer, real and ASCII, respectively.
-The byte comparison operations are unsigned. The others are signed.
These operations are only executed if the RLG of the previous operations has a value of 1.
-If the comparison is true, the logical output result (LOR) is 1 (the contact is closed).
51
Chapter V. API Programming
Example
I0.0 I0.1 MD0 I0.2 Q0.1 The output Q0.1 is set to 1 if the signal state is
>=R S 1 at inputs I0.0 AND I0.1, AND if MD0 >=
MD4 1 MD4 AND if the signal state I0.2 is 1 at the input.
V.5.4. Conversion operations
The conversion operations read the content of the input parameter IN and then convert it.
The result is stored in the output parameter OUT. These operations only execute if the input
The EN value is 1. The ENO output takes the value 1 if the operation is performed.
Among these operations, you have the following:
B_I: Convert byte to 16-bit integer, B-I ROUND
Convert 16-bit integer to byte, EN ENO EN ENO
BCD_I: Convert DCB number to 16-bit integer. IN OUT IN OUT
I_BCD: Convert 16-bit integer to DCB number,
I_DI: Convert 16-bit integer to 32-bit integer
ROUND: Rounding: converts a real value IN into a 32-bit integer
TRUNC: Truncate: converts a real number IN into a 32-bit integer and places the part
entire result in the variable indicated by OUT.
Example
If the signal state is 1 at input I0.0, the content
I0.0 BCD-I Q4.0 the word of memento MW10 is read as number
IN INO NOT DCB in three digits and converted to an integer
of 16 bits. The result is stored in the word of
MW10IN OUT MW12
MW12 memo. The output Q4.0 is set to 1 if the
conversion is not executed (ENO = 0).
V 5.5. Transfer operations
This operation is activated by the validation input EN. The value indicated in the input IN is
copied to the address specified in the OUT output. The signal state of ENO is identical to that of EN.
You have the following transfer operations:
MOV-B: Transfer of a byte,
MOV_B MOV_W MOV_DW
MOV-W: Transfer of a word,
IN NO EN ENO IN NO
MOV-D: Transfer of a double word,
MOV-R: Transfer of a real. IN OUT IN OUT IN OUT
Example
The operation is executed if I0.0 is at 1. The content of
I0.0 MOV_W Q4.0 MW10 is then copied into data word 12 of the
EN ENO Data block in progress. Output A 4.0 is set to 1 if
MW10 IN OUT MW12 the operation is executed.
52
Chapter V. API Programming
53
Chapter V. API Programming
The Incremental Counter operation increments starting from the current value. CU CTU
at each rising edge of the CU increment input. When the current value
R
"Cxx" is greater than or equal to the predefined value PV, the counter bit Cxx is
activated. The counter is reset to zero when the reset input R is activated. PV
The incremental counter stops counting when it reaches the maximum value.
32,767. The current value of CXX is of type 16-bit integer. CXX
Decremental counter (CTD) CD CTD
The Decremental Counter operation decrements starting from the current value.
at each rising edge of the CD decrement input. When the value in LD
If Cxx is equal to zero, the Cxx counter bit is activated. The counter resets the PV
54
Chapter V. API Programming
Set the Cxx counter bit to 0 and load the preset value PV into the current value when the input
LD loading is activated. The counter stops when it reaches zero and the counter bit Cxx
is then set to 1. The current value of CXX is of integer type.
CXX
Up-Counter/Down-Counter (CTUD)
CU CTUD
The incremental/decremental counter operation increments starting from the
values in progress at each front rising from the increment input CU and CD
decreases on each rising edge of the decrement input CD. The value
The Cxx counter contains the current count. The preset value R
PV is compared to the current value at each execution of the operation. PV
counting.
When it reaches the maximum value of 32,767, the next rising edge at the increment input
set the current value to the minimum value of -32,768. When the minimum value -32,768
Attained, the next rising front at the entry of decrementation causes the current value to take on the
maximum value of 32,767.
When the current value Cxx is greater than or equal to the predefined value PV, the counter bit
Cxx is activated. Otherwise, the counter bit is disabled. The counter is reset to zero when the input
Reset R is activated or the 'Zero Out' operation is executed. The current value of
CXX is of integer type.
Examples
Network 1
I0.0 C1
CD CTD
I0.1
LD
+3 PV
Network 2
C1 Q0.0
Réseau 1
I0.0 C48
See youCTUD
I0.1
CD
I0.2
R
+4 PV
Network 2
C48 Q0.0
55
Chapter V. API Programming
+10 PT 100ms
Network 2
T37 Q0.0
TXX
Start delay timing in the form of falling edge (TOF)
IN TOF
The Start delay operation in the form of a drop-off delay serves
to delay the deactivation of an output (the timer bit) for a PT
given time interval after the input (IN) has been deactivated.
The timer starts when a falling edge is detected at the start input IN.
The timer initializes if the signal state at input IN goes from 0 to 1 while the
Timing is being executed.
The timer continues to run until the elapsed time reaches the time.
predefined.
If current value = predefined value, the counting is stopped.
The signal state at the output is equal to 0 when the current value equals the predefined value or if the value
in progress = 0 provided that the input IN remains equal to 0. In other cases, the signal state at the
exit is 1.
Example
Network 1
I0.0 T33
IN TOF
+100 PT 10ms
Network 2
T33 Q0.0
56
Chapter V. API Programming
57
Chapter V. API Programming
The SCR operation signals the start of an SCR segment if the segment bit is equal to 1.
The sequential relay switch operation SCRT allows you to hand over from one segment
SCR active in another SCR segment, if the previous logical result equals 1. The execution of
The SCRT operation in the presence of a signal flow sets the S bit of the current segment to 0.
activates and sets bit S of the referenced segment to 1.
The Sequential Relay End Operation (SCRE) signals the end of a SCR segment if the result
previous logic equals 1.
Example
Network 1
Network 1: Activate Segment 1 during the first cycle.
SM0.1 S0.1
S
. 1
Network 2 S0.1
Network 2: Start of Segment 1.
SCR
Réseau 3 Network 3: Instructions for Segment 1.
End-of-processing operations
The Conditional End of Processing operation (END) ends the current cycle, if the result
previous logic equals 1. You can use the End of processing operation
conditional in the main program, but not in the sub-programs or in the
interruption programs.
The Conditional End of Processing operation (RET) ends the execution of the subprogram or
of the interrupt program, if the previous logical result equals 1.
The operation STOP immediately ends the execution of your program, if the result
previous logic equals 1, switching the S7-200 CPU from the 'Running' state
(RUN) in the state 'Stop' (STOP).
58
Chapter V. API Programming
Operation Comment
LD Load the operand value
LDN Load the inverse value of the operand
A ET logic between the previous result and the state of the operand
AN AND logic between the previous result and the inverse state of the operand
O OR logic between the previous result and the state of the operand
ON OR logic between the previous result and the inverse state of the operand
NOT Reverse the result
EU Detect rising front
ED Detect descending front
= Write the output
S position the operator to a
R Put the operand back to a
LDB= Equality type comparison
AB= Comparison of type equality preceded by a series contact
OB= contact in parallel with the equality type comparison
BTI, ITB Conversion of a byte to an integer, Conversion of an integer to a byte.
+I ; -I ; *I ; /I Addition, Subtraction, Multiplication, and Division of two whole numbers
INCREMENT Incrementing and decrementing a byte
INVB ; ANDB ; ORB Inversion of one byte, and logical operation of two bytes, or logical operation of two bytes
MOVB Transfer a byte
SLB Left shift of a byte, Right shift of a word
RLB ; RRD Left rotation of a byte, Right shift of a double word
Table V.4. Basic operations of the language Listed instructions.
59