SFC and ST Programming Languages
SFC and ST Programming Languages
Excerpts from : - Logix5000 Controllers Common Procedures, 1756-PM001H-EN-P - Logix5000 Controllers General Instructions Reference Manual, 1756-RM003H-EN-P
Programming Manual
Solid state equipment has operational characteristics differing from those of electromechanical equipment. Safety Guidelines for the Application, Installation and Maintenance of Solid State Controls (Publication SGI-1.1 available from your local Rockwell Automation sales office or online at http://www.ab.com/manuals/gi) describes some important differences between solid state equipment and hard-wired electromechanical devices. Because of this difference, and also because of the wide variety of uses for solid state equipment, all persons responsible for applying this equipment must satisfy themselves that each intended application of this equipment is acceptable. In no event will Rockwell Automation, Inc. be responsible or liable for indirect or consequential damages resulting from the use or application of this equipment. The examples and diagrams in this manual are included solely for illustrative purposes. Because of the many variables and requirements associated with any particular installation, Rockwell Automation, Inc. cannot assume responsibility or liability for actual use based on the examples and diagrams. No patent liability is assumed by Rockwell Automation, Inc. with respect to use of information, circuits, equipment, or software described in this manual. Reproduction of the contents of this manual, in whole or in part, without written permission of Rockwell Automation, Inc. is prohibited. Throughout this manual we use notes to make you aware of safety considerations.
WARNING
Identifies information about practices or circumstances that can cause an explosion in a hazardous environment, which may lead to personal injury or death, property damage, or economic loss.
IMPORTANT
Identifies information that is critical for successful application and understanding of the product. Identifies information about practices or circumstances that can lead to personal injury or death, property damage, or economic loss. Attentions help you: identify a hazard avoid a hazard recognize the consequence
ATTENTION
SHOCK HAZARD
Labels may be located on or inside the drive to alert people that dangerous voltage may be present.
BURN HAZARD
Labels may be located on or inside the drive to alert people that surfaces may be dangerous temperatures.
Preface
This manual contains excerpts from Logix5000 Controllers Common Procedures, publication 1756-PM001H-EN-P and Logix5000 Controllers General Instructions Reference Manual, publication 1756-RM003H-EN-P. This manual provides information on: Sequential Function Chart (SFC) programming language Structured Text (ST) programming language Jump to Subroutine (JSR), Subroutine (SBR), and Return (RET) instructions needed to access SFC and ST logic The term Logix5000 controller refers to any controller that is based on the Logix5000 operating system, such as: CompactLogix controllers ControlLogix controllers FlexLogix controllers PowerFlex 700S with DriveLogix controllers SoftLogix5800 controllers
This manual is intended for those individuals who program applications that use Logix5000 controllers, such as: software engineers control engineers application engineers instrumentation technicians
Use this manual when you perform these actions: develop the basic code for your application modify an existing application perform isolated tests of your application As you integrate your application with the I/0 devices, controllers, and networks in your system: Refer to the user manual for your specific type of controller. Use this manual as a reference, when needed.
Preface
This manual is divided into the basic tasks that you perform while programming a Logix5000 controller. Each chapter covers a task. The tasks are organized in the sequence that you will typically perform them. As you use this manual, you will see some terms that are formatted differently from the rest of the text:
Identifies:
For example:
Means: Right-click on the item that is named User-Defined. If you want additional information, refer to name in the Glossary.
the actual name of an item that you Right-click User-Defined see on your screen or in an example an entry in the Glossary Type a name
If you are viewing the PDF file of the manual, click name to jump to the glossary entry. courier information that you must supply based on your application (a variable) Right-click name_of_program Press [Enter]. You must identify the specific program in your application. Typically, it is a name or variable that you have defined. Press the Enter key.
Table of Contents
Chapter 4 Design a Sequential Function Chart
When to Use This Chapter. . . . . . . . . . . . What is a Sequential Function Chart? . . . . Define the Tasks . . . . . . . . . . . . . . . . . . . Choose How to Execute the SFC . . . . . . . Define the Steps of the Process . . . . . . . . Organize the Steps . . . . . . . . . . . . . . . . . Add Actions for Each Step . . . . . . . . . . . . Describe Each Action in Pseudocode . . . . Choose a Qualifier for an Action . . . . . . . Define the Transition Conditions . . . . . . . Transition After a Specified Time . . . . . . . Turn Off a Device at the End of a Step . . Keep Something On From Step-to-Step . . End the SFC . . . . . . . . . . . . . . . . . . . . . . Nest an SFC . . . . . . . . . . . . . . . . . . . . . . Configure When to Return to the OS/JSR . Pause or Reset an SFC . . . . . . . . . . . . . . . Execution Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-1 4-2 4-5 4-6 4-6 4-11 4-15 4-19 4-19 4-20 4-25 4-28 4-34 4-38 4-41 4-42 4-43 4-43
Table of Contents
Chapter
Use this chapter to design a sequential function chart (SFC) for your process or system. An SFC is similar to a flowchart of your process. It defines the steps or states through which your system progresses. Use an SFC to: organize the functional specification for your system program and control your system as a series of steps and transitions By using an SFC to specify your process, you gain these advantages: Since an SFC is a graphical representation of your process, it is easier to organize and read than a textual version. In addition, RSLogix 5000 software lets you: add notes that clarify steps or capture important information for use later on print the SFC to share the information with other individuals Since Logix5000 controllers support SFCs, you do not have to enter the specification a second time. You are programming your system as you specify it. By using an SFC to program your process, you gain these advantages: graphical division of processes into its major logic pieces (steps) faster repeated execution of individual pieces of your logic simpler screen display reduced time to design and debug your program faster and easier troubleshooting direct access to the point in the logic where a machine faulted easy updates and enhancements
4-2
A sequential function chart (SFC) is similar to a flowchart. It uses steps and transitions to perform specific operations or actions. This example shows the elements of an SFC:
Figure 4.1 SFC Example A step represents a major function of your process. It contains the actions that occur at a particular time, phase, or station. An action is one of the functions that a step performs.
A transition is the true or false condition that tells the SFC when to go to the next step. A qualifier determines when an action starts and stops.
4-3
Figure 4.2 SFC Example (continued from previous page) A selection branch chooses between different execution paths.
A text box lets you add descriptive text or notes to your SFC.
A wire connects one element to another element anywhere on the chart. This wire takes you to the conveyor step on Figure 4.1 on the previous page.
4-4
4-5
The first step in the development of an SFC is to separate the configuration and regulation of devices from the commands to those devices. Logix5000 controllers let you divide your project into one continuous task and multiple periodic tasks and event tasks. 1. Organize your project:
These functions: configure and regulate devices command a device to a specific state sequence the execution of your process Go here: periodic task SFC in the continuous task
2. For those functions that go in a periodic task, group the functions according to similar update rates. Create a periodic task for each update rate. For example, 2-state devices may require faster updates than PID loops. Use separate periodic tasks for each. In this example, a project uses two periodic tasks to regulate motors, valves, and temperature loops. An SFC to control the process. Define the Tasks
This task (periodic) uses function block diagrams to turn on or off motors and open or close valves. The SFC in MainTask commands the state for each device. The function block diagrams set and maintain that state.
EXAMPLE
This task (periodic) uses function block diagrams to configure and regulate temperature loops. The SFC in MainTask commands the temperatures. The function block diagrams set and maintain those temperatures.
This task (continuous) executes the sequential function chart (SFC). The SFC commands the specific state or temperature for each device or temperature loop.
4-6
To execute an SFC, either configure it as the main routine for a program or call it as a subroutine.
Then: Configure the SFC as the main routine for the program. 1. Configure another routine as the main routine for the program. 2. Use the main routine to call the SFC as a subroutine.
The SFC is the only routine in the program. The SFC calls all the other routines of the program. The program requires other routines to execute independent of the SFC. The SFC uses boolean actions.
If the SFC uses boolean actions, then other logic must run independent of the SFC and monitor status bits of the SFC.
A step represents a major function of your process. It contains the actions that occur at a particular time, phase, or station.
Step A step executes continuously until a transition tells the SFC to go to the next step.
Do this
Actions
MIX
and do this
A transition ends a step. The transition defines the physical conditions that must occur or change in order to go to the next step.
4-7
Step Guidelines
As you define the steps of your process, follow these guidelines: Start with large steps and refine the steps in several passes.
first pass Paint second pass Transfer_In third pass Transfer_In
Paint
Paint_Flow
Air_Flow
Elec_Charg
Clean Transfr_Out
Clean Transfr_Out
When you first open an SFC routine, it contains an initial step and transition. Use this step to initialize your process.
initial step
initial transition
The controller executes the initial step: after a project download when the controller goes into Run mode. if the SFC is configured to, when the controller transitions to Run mode and on power-up when the routine containing the chart is modified online and the controller transitions to or from Test mode To identify a step, look for a physical change in your system, such as new part that is in position, a temperature that is reached, a preset time that is reached, or a recipe selection that occurs. The step is the actions that take place before that change.
4-8
SFC_STEP Structure
Each step uses a tag to provide information about the step. Access this information via either the Step Properties dialog box or the Monitor Tags tab of the Tags window:
If you want to: determine how long a step has been active (milliseconds) flag when the step has been active for a specific length of time (milliseconds) Then check or set Data type: Details: this member: T DINT When a step becomes active, the Timer (T) value resets and then starts to count up in milliseconds. The timer continues to count up until the step goes inactive, regardless of the Preset (PRE) value. Enter the time in the Preset (PRE) member. When the Timer (T) reaches the Preset value, the Done (DN) bit turns on and stays on until the step becomes active again. As an option, enter a numeric expression that calculates the time at runtime. DN flag if a step did not execute long enough LimitLow BOOL DINT When the Timer (T) reaches the Preset (PRE) value, the Done (DN) bit turns on and stays on until the step becomes active again. Enter the time in the LimitLow member (milliseconds). If the step goes inactive before the Timer (T) reaches the LimitLow value, the AlarmLow bit turns on. The AlarmLow bit stays on until you reset it. To use this alarm function, turn on (check) the AlarmEnable (AlarmEn) bit. As an option, enter a numeric expression that calculates the time at runtime. AlarmEn AlarmLow BOOL BOOL To use the alarm bits, turn on (check) the AlarmEnable (AlarmEn) bit. If the step goes inactive before the Timer (T) reaches the LimitLow value, the AlarmLow bit turns on. The bit stays on until you reset it. To use this alarm function, turn on (check) the AlarmEnable (AlarmEn) bit.
PRE
DINT
4-9
Then check or set Data type: Details: this member: LimitHigh DINT Enter the time in the LimitHigh member (milliseconds). If the Timer (T) reaches the LimitHigh value, the AlarmHigh bit turns on. The AlarmHigh bit stays on until you reset it. To use this alarm function, turn on (check) the AlarmEnable (AlarmEn) bit. As an option, enter a numeric expression that calculates the time at runtime. AlarmEn AlarmHigh BOOL BOOL To use the alarm bits, turn on (check) the AlarmEnable (AlarmEn) bit. If the Timer (T) reaches the LimitHigh value, the AlarmHigh bit turns on. The bit stays on until you reset it. To use this alarm function, turn on (check) the AlarmEnable (AlarmEn) bit.
do something while the step is active (including first and last scan) do something one time when the step becomes active
BOOL
The X bit is on the entire time the step is active (executing). Typically, we recommend that you use an action with a N Non-Stored qualifier to accomplish this.
FS(1)
BOOL
The FS bit is on during the first scan of the step. Typically, we recommend that you use an action with a P1 Pulse (Rising Edge) qualifier to accomplish this.
do something while the step is active, except on the first and last scan do something one time on the last scan of the step
SA
BOOL
The SA bit is on when the step is active except during the first and last scan of the step. The LS bit is on during the last scan of the step. Use this bit only if you do the following: On the Controller Properties dialog box, SFC Execution tab, set the Last Scan of Active Step to Dont Scan or Programmatic reset. Typically, we recommend that you use an action with a P0 Pulse (Falling Edge) qualifier to accomplish this.
LS(1)
BOOL
Reset
BOOL
An SFC Reset (SFR) instruction resets the SFC to a step or stop that the instruction specifies. The Reset bit indicates to which step or stop the SFC will go to begin executing again. Once the SFC executes, the Reset bit clears.
determine the maximum time that a step has been active during any of its executions determine if the Timer (T) value rolls over to a negative value
TMax
DINT
Use this for diagnostic purposes. The controller clears this value only when you select the Restart Position of Restart at initial step and the controller changes modes or experiences a power cycle. Use this for diagnostic purposes.
OV
BOOL
4-10
If you want to: determine how many times a step has become active
Then check or set Data type: Details: this member: Count DINT This is not a count of scans of the step. The count increments each time the step becomes active. It increments again only after the step goes inactive and then active again. The count resets only if you configure the SFC to restart at the initial step. With that configuration, it resets when the controller changes from program mode to run mode.
use one tag for the various status bits of this step
Status
DINT
(1)
The FS and LS bits are only active during a steps execution. Once a step finishes executing the code within its actions, the FS and/or LS bits are reset. If you reference either of these bits in code outside of the SFC routine in a different part of the project, the bits are always cleared (0).
This diagram shows the relationship of the X, FS, SA, and LS bits.
step_name.X
step_name.FS
step_name.SA
4-11
Once you define the steps of your process, organize them into sequences, simultaneous branches, selection branches, or loops.
Use this structure: Sequence With these considerations: The SFC checks the transition at the end of the step: If true, the SFC goes to the next step. If false, the SFC repeats the step.
Choose between alternative steps or groups of steps depending on logic conditions Execute a step or steps or skip the step or steps depending on logic conditions
Selection Branch
It is OK for a path to have no steps and only a transition. This lets the SFC skip the selection branch. By default, the SFC checks from left to right the transitions that start each path. It takes the first true path. If no transitions are true, the SFC repeats the previous step. RSLogix 5000 software lets you change the order in which the SFC checks the transitions.
Execute 2 or more steps at the same time. All paths must finish before continuing the SFC
Simultaneous Branch
A single transition ends the branch. The SFC checks the ending transition after the last step in each path has executed at least once. If the transition is false, the SFC repeats the previous step.
Connect the wire to the step or simultaneous branch to which you want to go. Do not wire into, out of, or between a simultaneous branch.
4-12
45
46
Selection Branch
Drill
Polish
To simplify my programming, I want to separate communications and block transfers from other control logic. All occur at the same time.
Simultaneous Branch
Control
Comms
BTs
In a heat treating area, the temperature ramps up at a specific rate, maintains that temperature for a specific duration, and then cools at a specific rate.
Sequence
Ramp Maintain Cool
At station 12, the machine drills, taps, and bolts a part. The steps occur one after the other.
Sequence
Drill Tap Bolt
Step 12 inspects a process for the correct mix of chemicals. If OK, then continue with the remaining steps. If not OK, go to the top of the SFC and purge the system.
Wire
start of SFC Step 12
Not OK
OK
4-13
Sequence
A sequence is a group of steps that execute one after the other.
do this
THEN this
THEN this
Selection Branch
A selection branch represents a choice between one path (step or group of steps) or another path (i.e., an OR structure). Only one path executes. By default the SFC checks the transitions from left to right. The SFC takes the first true path. RSLogix 5000 software lets you change the order in which the SFC checks the transitions (see chapter 5).
single horizontal line each path has its own transition do this OR this OR this
4-14
Simultaneous Branch
A simultaneous branch represents paths (steps or group of steps) that occur at the same time (i.e., an AND structure). All paths execute. All paths must finish before continuing with the SFC. The SFC checks the transition after the last step of each path has executed at least once.
do this
AND this
AND this
4-15
For example:
go to this step
Use actions to divide a step into the different functions that the step performs, such as commanding a motor, setting the state of a valve, or placing a group of devices in a specific mode.
Step
Do this
Actions
MIX
and do this
4-16
4-17
execute your logic without additional Embed structured text. routines For example: use structured text assignments, constructs, and instructions
When the S_Complete_the_Batch step is active, the S_Open_Outlet action executes. The action sets the Outlet.ProgCommand tag equal to 1, which opens the outlet valve. re-use logic in multiple steps Call a subroutine.
use another language to program the For example: action, such as ladder logic nest an SFC
When the S_Complete_the_Batch step is active, the S_Open_Outlet action executes. The action calls the Open_Outlet routine. Open_Outlet Routine
When the Open_Outlet routine executes, the OTE instruction sets the Outlet.ProgCommand tag equal to 1, which opens the outlet valve.
You cannot reuse a non-boolean action within the same SFC except to reset a stored action. Only one instance of a specific non-boolean action is permitted per SFC.
4-18
For example:
EXAMPLE
When the S_Complete_the_Batch step is active, the S_Open_Outlet action executes. When the action is active, its Q bit turns on.
A ladder logic routine monitors the Q bit (S_Open_Outlet.Q). When the Q bit is on, the JSR instruction executes and opens the outlet valve.
You can reuse a boolean action multiple times within the same SFC.
SFC_ACTION Structure
Each action (non-boolean and boolean) uses a tag to provide information about the action. Access this information via either the Action Properties dialog or the Monitor Tags tab of the Tags window:
If you want to: determine when the action is active Then check or set this member: Q Data type: Details: BOOL The status of the Q bit depends on whether the action is a boolean action or non-boolean action: If the action is: boolean non-boolean Then the Q bit is: on (1) the entire time the action is active, including the last scan of the action on (1) while the action is active but off (0) at the last scan of the action To use a bit to determine when an action is active, use the Q bit. A determine how long an action has been active (milliseconds) T BOOL DINT The A bit is on the entire time the action is active. When an action becomes active, the Timer (T) value resets and then starts to count up in milliseconds. The timer continues to count up until the action goes inactive, regardless of the Preset (PRE) value.
4-19
If you want to: use one of these time-based qualifiers: L, SL, D, DS, SD
Data type: Details: DINT Enter the time limit or delay in the Preset (PRE) member. The action starts or stops when the Timer (T) reaches the Preset value. As an option, enter a numeric expression that calculates the time at runtime.
Count
DINT
This is not a count of scans of the action. The count increments each time the action becomes active. It increments again only after the action goes inactive and then active again. The count resets only if you configure the SFC to restart at the initial step. With that configuration, it resets when the controller changes from program mode to run mode.
use one tag for the various status bits of this action
Status
DINT
To organize the logic for an action, first describe the action in pseudocode. If you are unfamiliar with pseudocode: Use a series of short statements that describe what should happen. Use terms or symbols such as: if, then, otherwise, until, and, or, =, >, <. Sequence the statements in the order that they should execute. If necessary, name the conditions to check first (when 1st) and then the action to take second (what 2nd). Enter the pseudocode into the body of the action. After you enter the pseudocode, you can: Refine the pseudocode so it executes as structured text. Use the pseudocode to design your logic and leave the pseudocode as comments. Since all structured text comments download to the controller, your pseudocode is always available as documentation for the action.
4-20
To convert the pseudocode to structured text comments, add these comment symbols:
For a comment: on a single line that spans more than one line Use one of these formats: //comment (*start of comment . . . end of comment*)
Each action (non-boolean and boolean) uses a qualifier to determine when it starts and stops. The default qualifier is Non-Stored. The action starts when the step is activated and stops when the step is deactivated. To change when an action starts or stops, assign a different qualifier:
Table 4.1 Choose a Qualifier for an Action If you want the action to: start when the step is activated And: stop when the step is deactivated execute only once stop before the step is deactivated or when the step is deactivated stay active until a Reset action turns off this action stay active until a Reset action turns off this action or a specific time expires, even if the step is deactivated start a specific time after the step is activated and the step is still active stop when the step is deactivated stay active until a Reset action turns off this action D DS SD Time Delayed Delayed and Stored Stored and Time Delayed Then assign this Which qualifier: means: N P1 L S SL Non-Stored Pulse (Rising Edge) Time Limited Stored Stored and Time Limited
start a specific time after the step is activated, stay active until a Reset action turns off this action even if the step is deactivated before this time
4-21
Table 4.1 Choose a Qualifier for an Action If you want the action to: execute once when the step is activated start when the step is deactivated turn off (reset) a stored action: S SL DS SD Stored Stored and Time Limited Delayed and Stored Stored and Time Delayed And: execute once when the step is deactivated execute only once Then assign this Which qualifier: means: P P0 R Pulse Pulse (Falling Edge) Reset
The transition is the physical conditions that must occur or change in order to go to the next step.
The transition tells the SFC when to go to the next step.
transition
4-22
selection branch
transitions
simultaneous branch
transitions
4-23
Here are two examples of transitions: EXAMPLE You want to: a. Turn on 2 compressors. When a compressor is on, the Device1State bit is on. b. When both compressors are on, go to the next step. Solution:
Init Init_Done compressor_1.Device1State = on (1) and
EXAMPLE
You want to: a. Package the product. When the product is in the package, the package_done bit turns on. b. Pack the product either 8 per carton or 16 per carton. Solution:
Package
carton_16
To override the state of a transition, see Force Logic Elements on page 13-1.
4-24
Transition Tag
Each transition uses a BOOL tag to represent the true or false state of the transition.
If the transition is: true false The value is: 1 0 And: The SFC goes to the next step. The SFC continues to execute the current step.
bool_tag_a
dint_tag_a > 8
4-25
Call a Subroutine
To use a subroutine to control a transition, include an End Of Transition (EOT) instruction in the subroutine. The EOT instruction returns the state of the conditions to the transition, as shown below.
3 1
JSR - or -
If condition_1 & condition_2 & condition_3 then BOOL_tag := 1; Else BOOL_tag := 0; End_if; EOT(BOOL_tag);
1. Call a subroutine. 2. Check for the required conditions. When those conditions are true, turn on a BOOL tag. 3. Use an EOT instruction to set the state of the transition equal to the value of the BOOL tag. When the BOOL tag is on (true), the transition is true.
4-26
Each step of the SFC includes a millisecond timer that runs whenever the step is active. Use the timer to: signal when the step has run for the required time and the SFC should go to the next step signal when the step has run too long and the SFC should go to an error step
Figure 4.3 Action of a timer and associated bits of a step:
step_name.X
step_name.PRE
step_name.T
step_name.DN
1 Description:
1. Step becomes active. X bit turns on. Timer (T) begins to increment. 2. Timer reaches the Preset (PRE) value of the step. DN bit turns on. Timer continues to increment. 3. Step becomes inactive. X bit turns off. Timer retains its value. DN remains on. 4. Step becomes active. X bit turns on. Timer clears and then begins to increment. DN bit turns off.
4-27
Figure 4.4 Action of the low and high alarms for a step:
step_name.AlarmEn step_name.X step_name.LimitHigh step_name.LimitLow step_name.T
step_name.AlarmHigh
step_name.AlarmLow
Description: 1. AlarmEn is on. To use the low and high alarms turn this bit on. Turn the bit on via the properties dialog box or the tag for the step. 2. Step becomes active. X bit turns on. Timer (T) begins to increment. 3. Step becomes inactive. X bit turns off. Timer retains its value. Since Timer is less than LimitLow, AlarmLow bit turns on. 4. Step becomes active. X bit turns on. Timer clears and then begins to increment. AlarmLow stays on. (You have to manually turn it off.) 5. Timer reaches the LimitHigh value of the step. AlarmHigh bit turns on. Timer continues to increment. 6. Step becomes inactive. X bit turns off. Timer retains its value. AlarmHigh stays on. (You have to manually turn it off.)
4-28
Here is an example of the use of the Preset time of a step. EXAMPLE Functional specification says: a. Cook the ingredients in the tank for 20 seconds. b. Empty the tank. Solution:
Cook Cook_Done Empty_Tank Cook.PRE = 20000 ms Cook.DN = on (1)
Here is an example of the use of the high alarm of a step. Functional specification says: a. Home 8 devices. b. If all 8 devices are not home within 20 seconds, then shutdown the system. Solution:
Init Init.LimitHigh = 20000 ms
EXAMPLE
Init_OK Step_1
Init_Not_OK Shutdown
Init.AlarmHigh
4-29
When the SFC leaves a step, you have several options on how to turn off devices that the step turned on.
Programmatic Reset
Automatic Reset
Each option requires you to make these choices: 1. Choose a last scan option. 2. Based on the last scan option, develop your logic so that the last scan returns data to the desired values.
4-30
This table compares the different options for handling the last scan of a step:
Characteristic: execution actions During the last scan of a step, this option does: Dont scan Only P and P0 actions execute. They execute according to their logic. Programmatic reset All actions execute according to their logic. Automatic reset P and P0 actions execute according to their logic. All other actions execute in postscan mode. On the next scan of the routine, the P and P0 actions execute in postscan mode. retention of data values All data keeps its current values. All data keeps its current values. Data reverts to its values for postscan. Tags to the left of [:=] assignments clear to zero. method for clearing data Use P and P0 actions. Use either: status bits of the step or action to condition logic P and P0 actions reset of a nested SFC A nested SFCs remains at its current step. A nested SFCs remains at its current step. Use either: [:=] assignment (non-retentive assignment) instructions that clear their data during postscan For the Restart Position property, if you choose the Restart at initial step option, then: A nested SFC resets to its initial step. The X bit of a stop element in a nested SFC clears to zero.
Publication 1756-PM003H-EN-E (excerpt from 1756-PM001H-EN-P) - August 2005
4-31
EXAMPLE
This action turns on the conveyor. When conveyor_state turns on, the conveyor turns on. Before the SFC leaves the step, the P0 action turns off the conveyor. On the last scan of the step, conveyor_state turns off. This turns off the conveyor.
4-32
EXAMPLE
When the step is not on its last scan (conveyor_fwd.LS = 0), this statement turns on conveyor_state. When conveyor_state turns on, the conveyor turns on. On the last scan of the step (conveyor_fwd.LS =1), this statement turns off conveyor_state. When conveyor_state turns off, the conveyor turns off.
4-33
For an action that uses one of the stored qualifiers, use the Q bit of the action to condition your logic. See SFC_ACTION Structure on page 4-18. Use the Programmatic Reset Option and the Q Bit
EXAMPLE
When the action is not on its last scan (conveyor_start.Q =1), this statement turns on conveyor_state. When conveyor_state turns on, the conveyor turns on. On the last scan of the action (conveyor_start.Q =0), this statement turns off conveyor_state. When conveyor_state turns off, the conveyor turns off.
You can also use a P0 Pulse (Falling Edge) action to clear data. This example uses an action to turn on a conveyor at the start of a step. A different action turns off the conveyor at the end of the step. Use the Programmatic Reset Option and a P0 Action
EXAMPLE
This action turns on the conveyor. When conveyor_state turns on, the conveyor turns on. Before the SFC leaves the step, the P0 action turns off the conveyor. On the last scan of the step, conveyor_state turns off. This turns off the conveyor.
4-34
As a general rule, the postscan executes instructions as if all conditions are false. For example, the Output Energize (OTE) instruction clears its data during postscan. Some instructions do not follow the general rule during postscan. For a description of how a specific instruction executes during postscan, see: Logix5000 Controllers General Instructions Reference Manual, publication 1756-RM003 Logix5000 Controllers Process and Drives Instructions Reference Manual, publication 1756-RM006 Logix5000 Controllers Motion Instruction Set Reference Manual, publication 1756-RM007
4-35
Here is an example that uses a non-retentive assignment to control a conveyor. It turns on a conveyor at the start of a step and automatically turns off the conveyor when the step is done. Automatically Clear Data
EXAMPLE
This action turns on the conveyor. When conveyor_state turns on, the conveyor turns on. When the SFC leaves the step, conveyor_state turns off. This turns off the conveyor.
Option: Use a Simultaneous Branch Make a separate step that controls the device.
Example:
Transfer_In
Paint
Fan
Clean Transfr_Out
Store and Reset an Action Note the step that turns on the device and the step that turns off the device. Later, define a Stored and Reset Action pair to control the device.
Transfer_In
Paint
Clean Transfr_Out Use One Large Step Paint Use one large step that contains all the actions that occur while the device is on.
turn off the fan
4-36
EXAMPLE
Paint_Flow
Air_Flow
Elec_Charg
Fan
Clean Transfr_Out
4-37
more steps
Action_Name
same name as the stored action
When the SFC leaves the step that stores the action, RSLogix 5000 software continues to show the stored action as active. (By default, a green border displays around the action.) This lets you know that the SFC is executing the logic of that action. To use a stored action, follow these guidelines: The Reset action only turns off the stored action. It does not automatically turn off the devices of the action. To turn off the device, follow the Reset action with another action that turns off the device. Or use the Automatic reset option described on page 4-33. Before the SFC reaches a stop element, reset any stored actions that you do not want to execute at the stop. An active stored action remains active even if the SFC reaches a stop. Use caution when you jump in between a step that stores an action and a step that resets the action. Once you reset an action, it only starts when you execute the step that stores the action.
4-38
In this example, steps 1 - 4 require a fan to be on. At the end of step_4, the fan is reset (turned off). When the SFC jumps back to step_3, the fan remains off.
step_1 turn on the fan (stored)
step_2
step_4
To turn the fan back on, the SFC has to jump back to step_1.
EXAMPLE
4-39
For additional information on how to nest an SFC, see Nest an SFC on page 4-41.
Once an SFC completes its last step, it does not automatically restart at the first step. You must tell the SFC what to do when it finishes the last step.
Do this: Wire the last transition to the top of the step to which you want to go. See Wire to a Previous Stepon page 4-14.
stop and wait for a command to Use a Stop Element. restart See Use a Stop Element on page 4-38.
4-40
EXAMPLE
When the SFC reaches last_step and process_done is true, the execution of the SFC stops.
This example shows the use of the SFC Reset (SFR) instruction to restart the SFC and clear the X bit of the stop element.
4-41
EXAMPLE
Restart (Reset) the SFC If SFC_a_stop.X = on (SFC_a is at the stop) and SFC_a_reset = on (time to reset the SFC) then for one scan (ons[0] = on): Reset SFC_a to SFC_a_Step_1 SFC_a_stop.X = 0
4-42
SFC_STOP Structure
Each stop uses a tag to provide information about the stop element:
If you want to: determine when the SFC is at the stop Then check or set this member: X Data type: Details: BOOL When the SFC reaches the stop, the X bit turns on. The X bit clears if you configure the SFCs to restart at the initial step and the controller changes from program to run mode. In a nested SFC, the X bit also clears if you configure the SFCs for automatic reset and the SFC leaves the step that calls the nested SFC. determine the target of an SFC Reset (SFR) instruction Reset BOOL An SFC Reset (SFR) instruction resets the SFC to a step or stop that the instruction specifies. The Reset bit indicates to which step or stop the SFC will go to begin executing again. Once the SFC executes, the Reset bit clears. determine how many times a stop has become active Count DINT This is not a count of scans of the stop. The count increments each time the stop becomes active. It increments again only after the stop goes inactive and then active again. The count resets only if you configure the SFC to restart at the initial step. With that configuration, it resets when the controller changes from program mode to run mode. use one tag for the various status bits of this stop Status DINT For this member: Reset X Use this bit: 22 31
4-43
Nest an SFC
One method for organizing your project is to create one SFC that provides a high-level view of your process. Each step of that SFC calls another SFC that performs the detailed procedures of the step (nested SFC). This figure shows one way to nest an SFC. In this method, the last scan option of the SFC is configured for either Programmatic reset or Dont scan. If you configure the SFC for Automatic reset, then step 1 is unnecessary.
SFC_a
SFC_b
3 1. Reset the nested SFC: The SFR instruction restarts SFC_b at SFC_b_Step_1. Each time SFC_a leaves this step and then returns, you have to reset SFC_b. The action also clears the X bit of the stop element. 2. Call SFC_b. 3. Stop SFC_b. This sets the X bit of the stop element. 4. Use the X bit of the stop element to signal that SFC_b is done and it is time to go to the next step.
Publication 1756-PM003H-EN-E (excerpt from 1756-PM001H-EN-P) - August 2005
4-44
Pass Parameters
To pass parameters to or from an SFC, place a Subroutine/Return element in the SFC.
Input Parameters Return Parameters
SFC_b
By default, an SFC executes a step or group of simultaneous steps and then returns to the operating system (OS) or the calling routine (JSR).
default
You have the option of letting the SFC execute until it reaches a false transition. If several transitions are true at the same time, this option reduces the time to get to the desired step. Use the Execute until FALSE transition option only when: 1. You dont have to update JSR parameters before each step. Parameters update only when the SFC returns to the JSR. See Pass Parameters on page 4-42. 2. A false transition occurs within the watchdog timer for the task. If the time that it takes to return to a JSR and complete the rest of the task is greater than the watchdog timer, a major fault occurs.
4-45
Two optional instructions are available that give you further control over the execution of your SFC:
If you want to: pause an SFC reset an SFC to a specific step or stop Then use this instruction: Pause SFC (SFP) Reset SFC (SFR)
Both instructions are available in the ladder logic and structured text programming languages.
Execution Diagrams
The following diagrams show the execution of an SFC with different organizations of steps or different selections of execution options.
See page: 4-44 4-45 4-46 4-46 4-47
For a diagram of the: Execution of a Sequence Execution of a Simultaneous Branch Execution of a Selection Branch When parameters enter and exit an SFC Options for Execution Control
4-46
step_1
false
return to OS/JSR
last scan
step_2
false
return to OS/JSR
last scan
4-47
last scan
step_2
step_3
tran_2 true
false
step_3
false
return to OS/JSR
last scan
step_3
4-48
return to OS/JSR
last scan
false
false
last scan
false
tran_1 true
return parameters
input parameters
last scan
4-49
false
return to OS/JSR
tran_1 true
false
return to OS/JSR
step_1
step_1
last scan
step_2
false
tran_2 true
false
return to OS/JSR
step_2
last scan
4-50
Notes:
Chapter
Use this chapter to enter a sequential function chart (SFC) into RSLogix 5000 software. Enter the SFC as you design it. Or first design the SFC and then enter it. To design the SFC, see Design a Sequential Function Chart on page 4-1. To program a sequential function chart routine:
For this information: Add an SFC Element Create a Simultaneous Branch Create a Selection Branch Set the Priorities of a Selection Branch Return to a Previous Step Configure a Step Program a Transition Add an Action Configure an Action Program an Action Assign the Execution Order of Actions Document an SFC Configure the Execution of the SFC Verify the Routine See page: 5-1 5-3 5-5 5-6 5-7 5-8 5-10 5-12 5-12 5-14 5-16 5-16 5-20 5-21
5-2
green dot
3. To wire (connect) two elements together, click a pin on one of the elements A and then click the pin on the other element B . A green dot shows a valid connection point.
5-3
green dot
5-4
2. To add a path to the branch, select (click) the first step of the path that is to the left of where you want to add the new path. Click
A B
green dot
3. To wire the simultaneous branch to the preceding transition, click the bottom pin of the transition A and then click the horizontal line of the branch B . A green dot shows a valid connection point.
5-5
B A
green dot
4. To wire the simultaneous branch to the transition, click the top pin of the transition A and then click the horizontal line of the branch B . A green dot shows a valid connection point.
2. To add a path to the branch, select (click) the first transition of the path that is to the left of where you want to add the new path. Click
A B
green dot
3. To wire the selection branch to the preceding step, click the bottom pin of the step A and then click the horizontal line of the branch B . A green dot shows a valid connection point.
5-6
2. On the SFC toolbar, click 3. Add the step that follows the selection branch.
B A
green dot
4. To wire the selection branch to the step, click the top pin of the step A and then click the horizontal line of the branch B . A green dot shows a valid connection point.
By default, the SFC checks the transitions that start a selection branch from left to right. If you want to check a different transition first, assign a priority to each path of the selection branch. For example, it is a good practice to check for error conditions first. Then check for normal conditions. To assign priorities to a selection branch: 1. Right click the horizontal line that starts the branch and select Set Sequence Priorities.
5-7
2. Clear (uncheck) the Use default priorities check box and select a transition.
Use the Move buttons to raise or lower the priority of the transition. 3. When all the transitions have the desired priority, click When you clear (uncheck) the Use default priorities check box, numbers show the priority of each transition.
5-8
1.
2.
Click here.
green dot
Hide a Wire
If a wire gets in the way of other parts of your SFC, hide the wire to make the SFC easier to read. To hide a wire, right-click the wire and select Hide Wire.
To see the SFC element to which the wire goes, click the grid location on the wire.
5-9
Configure a Step
2. On the General tab, type the time for the step, in milliseconds. 3. Click When the step is active for the preset time (Timer = Preset), the DN bit of the step turns on. To calculate the preset time for a step at runtime, see Use an Expression to Calculate a Time on page 5-9.
Type the time for the high alarm and low alarm, in milliseconds. 3. Click To calculate the time for an alarm at runtime, see Use an Expression to Calculate a Time on page 5-9.
5-10
Type a numeric expression that defines the time. Use the buttons alongside the dialog box to help you complete the expression. For information on numeric expressions, see Expressions on page 6-4. 4. Click 5. To close the Step Properties dialog box, click
5-11
Program a Transition
BOOL_expression
This example shows three transitions that use a BOOL expression. EXAMPLE Enter a BOOL Expression
Call a Subroutine
1. Right-click the transition and select Set JSR. 2. Select the routine that contains the logic for the transition.
5-12
3. Click
JSR(routine_name)
Add an Action
To add an action to a step, right-click the step in which the action executes and select Add Action.
Configure an Action
If you chose a timed qualifier, type the time limit or delay for the action, in milliseconds. Timed qualifiers include: L Time Limited SL Stored and Time Limited D Time Delayed DS Delayed and Stored
5-13
Type a numeric expression that defines the preset time. Use the buttons alongside the dialog box to help you complete the expression. For information on numeric expressions, see Expressions on page 6-4. 4. Click 5. To close the Action Properties dialog box, click
5-14
Program an Action
1. Double-click the text area of the action. 2. Type the required structured text. 3. To close the text entry window, press [Ctrl] + [Enter].
//comment statement;
5-15
Call a Subroutine
Use a Jump to Subroutine (JSR) instruction to execute a subroutine when the action is active. 1. In the SFC, right-click the text entry area of the action and select Set JSR.
2. Choose the routine that you want to call. 3. To pass a parameter to the routine, click an empty Input Parameters text box. Then use the down arrow to select the tag that contains the parameter. 4. To receive a parameter from the routine, click an empty Return Parameters text box. Then use the down arrow to select the tag in which to store the parameter from the routine. 5. Click
5-16
To change the order in which an action executes, drag the action to the desired location in the sequence. A green bar shows a valid placement location.
Document an SFC
To document this: general information about the SFC step
transition
download the documentation to the controller have the option of showing or hiding the documentation position the documentation anywhere in the SFC
Add Structured Text Comments Add a Text Box -orAdd a Tag Description Add Structured Text Comments Add a Text Box -orAdd a Tag Description
5-17
//comment statement;
5-18
2. Click the Tag tab and type the description for the element.
3. Click 4. Drag the description box to the desired location on the SFC.
2. Drag the text box to a location near the element to which it applies. 3. Double-click the text box and type the note. Then press [Ctrl] + [Enter].
5-19
4. As you move the element on the SFC, what do you want the text box to do?
If you the text box to: stay in the same spot move with the element to which it applies Then: Stop. You are done. Go to step 5.
B A green dot
5. Click the pin symbol in the text box and then click the SFC element to which you want to attach the text box. A green dot shows a valid connection point.
5-20
3. Click
The SFC Execution tab of the controller properties lets you configure: what to do when a transition is true where to start after a transition to the Run mode or recovery from a power loss what to do on the last scan of a step 1. On the Online toolbar, click the controller properties button.
5-21
3. Choose: whether or not to return to the OS/JSR if a transition is true. where to restart the SFC. The restart position applies when the controller loses power or leaves the Run or Remote Run mode. Select:
If you want to restart at the: Last step that was running Initial step Select Restart at most recently executed step Select Restart at initial step
The restart position isnt for major faults. After you clear a major fault, the SFC always restarts at the initial step. what to do on the last scan of a step. 4. Click
As you program your routine, periodically verify your work: 1. In the top-most toolbar of the RSLogix 5000 window, click 2. If any errors are listed at the bottom of the window: a. To go to the first error or warning, press [F4]. b. Correct the error according to the description in the Results window. c. Go to step 1. 3. To close the Results window, press [Alt] + [1]. To check your SFC, you can: force transitions step through the SFC For more information on these debug options, see chapter 13 Force Logic Elements.
5-22
Firmware revision 13 added support for editing SFCs online. When you transition the controller to test or un-test edits, the controller resets the SFC and starts execution at the initial step. If you edit an SFC online: Time when you test or untest edits to coincide with the SFC executing the initial step. Place structured text logic in subroutines to minimize the impact of online edits. Use an SFR instruction to programmatically shift SFC execution to the desired step.
Chapter
Use this chapter to write and enter structured text for a: structured text routine action of a sequential function chart (SFC) transition of sequential function chart (SFC)
Structured text is a textual programming language that uses statements to define what to execute. Structured text is not case sensitive. Use tabs and carriage returns (separate lines) to make your structured text easier to read. They have no effect on the execution of the structured text. Structured text can contain these components:
Examples: tag := expression;
Definition: Use an assignment statement to assign values to tags. The := operator is the assignment operator. Terminate the assignment with a semi colon ;. An expression is part of a complete assignment or construct statement. An expression evaluates to a number (numerical expression) or to a true or false state (BOOL expression). An expression contains: tags immediates operators functions A named area of the memory where data is stored (BOOL, SINT,INT,DINT, REAL, string). A constant value. A symbol or mnemonic that specifies an operation within an expression. When executed, a function yields one value. Use parentheses to contain the operand of a function. Even though their syntax is similar, functions differ from instructions in that functions can only be used in expressions. Instructions cannot be used in expressions.
6-2
Definition: An instruction is a standalone statement. An instruction uses parenthesis to contain its operands. Depending on the instruction, there can be zero, one, or multiple operands. When executed, an instruction yields one or more values that are part of a data structure. Terminate the instruction with a semi colon ;. Even though their syntax is similar, instructions differ from functions in that instructions cannot be used in expressions. Functions can only be used in expressions.
A conditional statement used to trigger structured text code (i.e, other statements). Terminate the construct with a semi colon ;.
IF...THEN CASE FOR...DO WHILE...DO REPEAT...UNTIL EXIT //comment (*start of comment . . . end of comment*) /*start of comment . . . end of comment*/
Text that explains or clarifies what a section of structured text does. Use comments to make it easier to interpret the structured text. Comments do not affect the execution of the structured text. Comments can appear anywhere in structured text.
Assignments
Use an assignment to change the value stored within a tag. An assignment has this syntax: tag := expression ; where:
Component: tag := expression Description: represents the tag that is getting the new value the tag must be a BOOL, SINT, INT, DINT, or REAL is the assignment symbol represents the new value to assign to the tag If tag is this data type: BOOL SINT INT DINT REAL ; ends the assignment Use this type of expression: BOOL expression numeric expression
Thetagretainstheassignedvalueuntilanotherassignmentchangesthevalue.
6-3
The expression can be simple, such as an immediate value or another tag name,ortheexpressioncanbecomplexandincludeseveraloperatorsand/or functions. See the next section Expressionson page 6-4 for details. I/O module data updates asynchronously to the execution of logic. If you referenceaninputmultipletimesinyourlogic,theinputcouldchangestate between separate references. If you need the input to have the same state for each reference, buffer the input value and reference that buffer tag. For more information on buffering data, see page1-8.
TIP
6-4
To add or insert a string of characters to a string tag, use either of these ASCII string instructions:
To: add characters to the end of a string insert characters into a string Use this instruction: CONCAT INSERT
Expressions
Anexpressionisatagname,equation,orcomparison.Towriteanexpression, use any of these elements: tag name that stores the value (variable) number that you enter directly into the expression (immediate value) functions, such as: ABS, TRUNC operators, such as: +, -, <, >, And, Or As you write expressions, follow these general rules: Useanycombinationofupper-caseandlower-caseletter.Forexample, these three variations of AND are acceptable: AND, And, and. Formorecomplexrequirements,useparenthesestogroupexpressions within expressions. This makes the whole expression easier to read and ensures that the expression executes in the desired sequence. See Determine the order of executionon page 6-10.
6-5
In structured text, you use two types of expressions: BOOL expression: An expression that produces either the BOOL value of 1 (true) or 0 (false). A bool expression uses bool tags, relational operators, and logical operators to compare values or check if conditions are true or false. For example, tag1>65. A simple bool expression can be a single BOOL tag. Typically,youuseboolexpressionstoconditiontheexecutionofother logic. Numeric expression: An expression that calculates an integer or floating-point value. A numeric expression uses arithmeticoperators, arithmetic functions, and bitwise operators. For example, tag1+5. Often, you nest a numeric expression within a bool expression. For example, (tag1+5)>65. Use the following table to choose operators for your expressions:
If you want to: Calculate an arithmetic value Compare two values or strings Check if conditions are true or false Compare the bits within values Then: Use arithmetic operators and functionson page 6-6. Use relational operatorson page 6-7. Use logical operatorson page 6-9. Use bitwise operatorson page 6-10.
6-6
Arithmetic functions perform math operations. Specify a constant, a non-boolean tag, or an expression for the function.
For: absolute value arc cosine arc sine arc tangent cosine radians to degrees natural log log base 10 degrees to radians sine square root tangent truncate Use this function: ABS (numeric_expression) ACOS (numeric_expression) ASIN (numeric_expression) ATAN (numeric_expression) COS (numeric_expression) DEG (numeric_expression) LN (numeric_expression) LOG (numeric_expression) RAD (numeric_expression) SIN (numeric_expression) SQRT (numeric_expression) TAN (numeric_expression) TRUNC (numeric_expression) Optimal data type: DINT, REAL REAL REAL REAL REAL DINT, REAL REAL REAL DINT, REAL REAL DINT, REAL REAL DINT, REAL
6-7
For example:
Use this format: value1 operator value2 Example: For this situation: If gain_4 and gain_4_adj are DINT tags and your specification says: "Add 15 to gain_4 and store the result in gain_4_adj." If alarm and high_alarm are DINT tags and your specification says: Negate high_alarm and store the result in alarm. If overtravel and overtravel_POS are DINT tags and your specification says: Calculate the absolute value of overtravel and store the result in overtravel_POS. If adjustment and position are DINT tags and sensor1 and sensor2 are REAL tags and your specification says: Find the absolute value of the average of sensor1 and sensor2, add the adjustment, and store the result in position. Youd write: gain_4_adj := gain_4+15; alarm:= -high_alarm; overtravel_POS := ABS(overtravel);
operator value1
function(numeric_expression)
6-8
For example:
Use this format: value1 operator value2 stringtag1 operator stringtag2 char1 operator char2 To enter an ASCII character directly into the expression, enter the decimal value of the character. bool_tag := bool_expressions Example: For this situation: If temp is a DINT tag and your specification says: If temp is less than 100 then If bar_code and dest are string tags and your specification says: If bar_code equals dest then If bar_code is a string tag and your specification says: If bar_code.DATA[0] equals A then Youd write: IF temp<100 THEN... IF bar_code=dest THEN...
IF bar_code.DATA[0]=65 THEN...
If count and length are DINT tags, done is a BOOL tag, and your specification says If count is greater than or equal to length, you are done counting.
Hex Codes $31$61$62 $31$62 $41 $41$42 $42 $61 $61$62 a>B AB < B
1b A AB B a ab
Strings are equal if their characters match. Characters are case sensitive. Upper case A ($41) is not equal to lower case a ($61).
6-9
For example:
Use this format:
BOOLtag NOT BOOLtag expression1 & expression2
Example: For this situation: Youd write: If photoeye is a BOOL tag and your specification IF photoeye THEN... says: If photoeye_1 is on then If photoeye is a BOOL tag and your specification IF NOT photoeye THEN... says: If photoeye is off then If photoeye is a BOOL tag, temp is a DINT tag, and your specification says: If photoeye is on and temp is less than 100 then. If photoeye is a BOOL tag, temp is a DINT tag, and your specification says: If photoeye is on or temp is less than 100 then. If photoeye1 and photoeye2 are BOOL tags and your specification says: If: photoeye1 is on while photoeye2 is off or photoeye1 is off while photoeye2 is on then" IF photoeye & (temp<100) THEN... IF photoeye OR (temp<100) THEN... IF photoeye1 XOR photoeye2 THEN...
expression1 OR expression2
open := photoeye1 & If photoeye1 and photoeye2 are BOOL tags, open is a BOOL tag, and your specification says: photoeye2; If photoeye1 and photoeye2 are both on, set open to true.
6-10
For example:
Use this format: value1 operator value2 Example: For this situation: If input1, input2, and result1 are DINT tags and your specification says: Calculate the bitwise result of input1 and input2. Store the result in result1. Youd write: result1 := input1 AND input2;
6-11
Instructions
Structured text statements can also be instructions. See the Locator Table at thebeginningofthismanualforalistoftheinstructionsavailableinstructured text. A structured text instruction executes each time it is scanned. A structured text instruction within a construct executes every time the conditions of the construct are true. If the conditions of the construct are false, the statements within the construct are not scanned. There is no rung-condition or state transition that triggers execution. This differs from function block instructions that use EnableIn to trigger execution. Structured text instructions execute as if EnableIn is always set. This also differs from relay ladder instructions that use rung-condition-in to trigger execution. Some relay ladder instructions only execute when rung-condition-intogglesfromfalsetotrue.Thesearetransitionalrelayladder instructions. In structured text, instructions will execute each time they are scanned unless you pre-condition the execution of the structured text instruction. Forexample,theABLinstructionisatransitionalinstructioninrelayladder.In this example, the ABL instruction only executes on a scan when tag_xic transitions from cleared to set. The ABL instruction does not execute when tag_xic stays set or when tag_xic is cleared.
In structured text, if you write this example as: IF tag_xic THEN ABL(0,serial_control); END_IF; the ABL instruction will execute every scan that tag_xic is set, not just when tag_xic transitions from cleared to set.
6-12
If you want the ABL instruction to execute only when tag_xic transitions from cleared to set, you have to condition the structured text instruction. Use a one shot to trigger execution.
osri_1.InputBit := tag_xic; OSRI(osri_1); IF (osri_1.OutputBit) THEN ABL(0,serial_control); END_IF;
Constructs
If you want to: do something if or when specific conditions occur select what to do based on a numerical value
do something a specific number of times before doing anything else keep doing something as long as certain conditions are true keep doing something until a condition is true
6-13
IF...THEN
Operands:
Structured Text
IF bool_expression THEN <statement>; END_IF;
Type: BOOL
Enter: BOOL tag or expression that evaluates to a BOOL value (BOOL expression)
. . .
optional
. . .
optional
ELSE <statement>;
. . .
END_IF;
To use ELSIF or ELSE, follow these guidelines: 1. To select from several possible groups of statements, add one or more ELSIF statements. Each ELSIF represents an alternative path. Specify as many ELSIF paths as you need. The controller executes the first true IF or ELSIF and skips the rest of the ELSIFs and the ELSE. 2. To do something when all of the IF or ELSIF conditions are false, add an ELSE statement.
6-14
Arithmetic Status Flags: not affected Fault Conditions: none Example 1: IFTHEN
If you want this: IF rejects > 3 then conveyor = off (0) alarm = on (1) Enter this structured text: IF rejects > 3 THEN conveyor := 0; alarm := 1; END_IF;
Example 2: IFTHENELSE
If you want this: If conveyor direction contact = forward (1) then light = off Otherwise light = on Enter this structured text: IF conveyor_direction THEN light := 0; ELSE light [:=] 1; END_IF;
The [:=] tells the controller to clear light whenever the controller: enters the RUN mode leaves the step of an SFC if you configure the SFC for Automatic reset (This applies only if you embed the assignment in the action of the step or use the action to call a structured text routine via a JSR instruction.)
6-15
Example 3: IFTHENELSIF
If you want this: If sugar low limit switch = low (on) and sugar high limit switch = not high (on) then inlet valve = open (on) Until sugar high limit switch = high (off) Enter this structured text: IF Sugar.Low & Sugar.High THEN Sugar.Inlet [:=] 1; ELSIF NOT(Sugar.High) THEN Sugar.Inlet := 0; END_IF;
The [:=] tells the controller to clear Sugar.Inlet whenever the controller: enters the RUN mode leaves the step of an SFC if you configure the SFC for Automatic reset (This applies only if you embed the assignment in the action of the step or use the action to call a structured text routine via a JSR instruction.)
Example 4: IFTHENELSIFELSE
If you want this: If tank temperature > 100 then pump = slow If tank temperature > 200 then pump = fast otherwise pump = off ELSE pump.fast :=0; pump.slow :=0; pump.off :=1; END_IF; Enter this structured text: IF tank.temp > 200 THEN pump.fast :=1; pump.slow :=0; pump.off :=0; ELSIF tank.temp > 100 THEN pump.fast :=0; pump.slow :=1; pump.off :=0;
6-16
CASE...OF
Operands:
Structured Text
CASE numeric_expression OF selector1: statement; selectorN: statement; ELSE statement; END_CASE; selector
Operand:
numeric_ expression
immediate
IMPORTANT
If you use REAL values, use a range of values for a selector because a REAL value is more likely to be within a range of values than an exact match of one, specific value.
selector2 :
. . .
selector3 : <statement>;
. . .
ELSE <statement>;
optional
. . .
END_CASE;
See the table on the next page for valid selector values.
Publication 1756-PM003H-EN-E (excerpt from 1756-PM001H-EN-P) - August 2005
6-17
The CASE construct is similar to a switch statement in the C or C++ programming languages. However, with the CASE construct the controller executes only the statements that are associated with the first matching selector value. Execution always breaks after the statements of that selector and goes to the END_CASE statement.
6-18
Example
If you want this: If recipe number = 1 then Ingredient A outlet 1 = open (1) Ingredient B outlet 4 = open (1) If recipe number = 2 or 3 then Ingredient A outlet 4 = open (1) Ingredient B outlet 2 = open (1) If recipe number = 4, 5, 6, or 7 then Ingredient A outlet 4 = open (1) Ingredient B outlet 2 = open (1) If recipe number = 8, 11, 12, or 13 then Ingredient A outlet 1 = open (1) Ingredient B outlet 4 = open (1) Otherwise all outlets = closed (0)
ELSE Ingredient_A.Outlet_1 [:=]0; Ingredient_A.Outlet_4 [:=]0; Ingredient_B.Outlet_2 [:=]0; Ingredient_B.Outlet_4 [:=]0; END_CASE; 8,11..13 Ingredient_A.Outlet_1 :=1; Ingredient_B.Outlet_4 :=1; 4..7: Ingredient_A.Outlet_4 :=1; Ingredient_B.Outlet_2 :=1; 2,3:
The [:=] tells the controller to also clear the outlet tags whenever the controller: enters the RUN mode leaves the step of an SFC if you configure the SFC for Automatic reset (This applies only if you embed the assignment in the action of the step or use the action to call a structured text routine via a JSR instruction.)
6-19
FORDO
Operands:
Use the FORDO loop to do something a specific number of times before doing anything else.
Structured Text
FOR count:= initial_value TO final_value BY increment DO <statement>; END_FOR;
Operand: count
Type: SINT INT DINT SINT INT DINT SINT INT DINT SINT INT DINT
Format: tag
Description: tag to store count position as the FORDO executes must evaluate to a number specifies initial value for count specifies final value for count, which determines when to exit the loop (optional) amount to increment count each time through the loop If you dont specify an increment, the count increments by 1.
IMPORTANT
Make sure that you do not iterate within the loop too many times in a single scan. Thecontrollerdoesnotexecuteanyotherstatementsintheroutineuntil it completes the loop. If the time that it takes to complete the loop is greater than the watchdog timer for the task, a major fault occurs. Consider using a different construct, such as IF...THEN.
optional
If there are conditions when you want to exit the loop early, use other statements, such as an IF...THEN construct, to condition an EXIT statement.
6-20
These diagrams show how a FOR...DO loop executes and how an EXIT statement leaves the loop early.
Done x number of times? no statement 1 statement 2 statement 3 statement 4 rest of the routine yes Done x number of times? no statement 1 statement 2 statement 3 statement 4 Exit ? no rest of the routine yes
yes
To stop the loop before the count reaches the last value, use an EXIT statement.
Example 1:
If you want this: Clear bits 0 - 31 in an array of BOOLs: 1. Initialize the subscript tag to 0. 2. Clear array[ subscript ] . For example, when subscript = 5, clear array[5]. 3. Add 1 to subscript. 4. If subscript is to 31, repeat 2 and 3. Otherwise, stop. Enter this structured text: For subscript:=0 to 31 by 1 do array[subscript] := 0; End_for;
6-21
Example 2:
If you want this: Enter this structured text:
SIZE(Inventory,0,Inventory_Items); A user-defined data type (structure) stores this information about an item in your inventory: For position:=0 to Inventory_Items - 1 do Barcode ID of the item (string data type) Quantity in stock of the item (DINT data type) If Barcode = Inventory[position].ID then An array of the above structure contains an element for each Quantity := Inventory[position].Qty; different item in your inventory. You want to search the array for a specific product (use its bar code) and determine the Exit; quantity that is in stock. End_if; 1. Get the size (number of items) of the Inventory array and store the result in Inventory_Items (DINT tag). End_for; 2. Initialize the position tag to 0. 3. If Barcode matches the ID of an item in the array, then: a. Set the Quantity tag = Inventory[position].Qty. This produces the quantity in stock of the item. b. Stop. Barcode is a string tag that stores the bar code of the item for which you are searching. For example, when position = 5, compare Barcode to Inventory[5].ID. 4. Add 1 to position. 5. If position is to (Inventory_Items -1), repeat 3 and 4. Since element numbers start at 0, the last element is 1 less than the number of elements in the array. Otherwise, stop.
6-22
WHILEDO
Operands:
Use the WHILEDO loop to keep doing something as long as certain conditions are true.
Structured Text
WHILE bool_expression DO <statement>; END_WHILE;
Type: BOOL
IMPORTANT
Make sure that you do not iterate within the loop too many times in a single scan. Thecontrollerdoesnotexecuteanyotherstatementsintheroutineuntil it completes the loop. If the time that it takes to complete the loop is greater than the watchdog timer for the task, a major fault occurs. Consider using a different construct, such as IF...THEN.
If there are conditions when you want to exit the loop early, use other statements, such as an IF...THEN construct, to condition an EXIT statement.
6-23
These diagrams show how a WHILE...DO loop executes and how an EXIT statement leaves the loop early.
BOOL expression true statement 1 statement 2 statement 3 statement 4 rest of the routine false BOOL expression true statement 1 statement 2 statement 3 statement 4 Exit ? no rest of the routine false
yes
While the bool_expression is true, the controller executes only the statements within the WHILEDO loop.
To stop the loop before the conditions are true, use an EXIT statement.
Example 1:
If you want this: The WHILE...DO loop evaluates its conditions first. If the conditions are true, the controller then executes the statements within the loop. This differs from the REPEAT...UNTIL loop because the REPEAT...UNTIL loop executes the statements in the construct and then determines if the conditions are true before executing the statements again. The statements in a REPEAT...UNTIL loop are always executed at least once. The statements in a WHILE...DO loop might never be executed. Enter this structured text: pos := 0; While ((pos <= 100) & structarray[pos].value <> targetvalue)) do pos := pos + 2; String_tag.DATA[pos] := SINT_array[pos]; end_while;
6-24
Example 2:
If you want this: Move ASCII characters from a SINT array into a string tag. (In a SINT array, each element holds one character.) Stop when you reach the carriage return. 1. Initialize Element_number to 0. 2. Count the number of elements in SINT_array (array that contains the ASCII characters) and store the result in SINT_array_size (DINT tag). 3. If the character at SINT_array[element_number] = 13 (decimal value of the carriage return), then stop. 4. Set String_tag[element_number] = the character at SINT_array[element_number]. 5. Add 1 to element_number. This lets the controller check the next character in SINT_array. 6. Set the Length member of String_tag = element_number. (This records the number of characters in String_tag so far.) 7. If element_number = SINT_array_size, then stop. (You are at the end of the array and it does not contain a carriage return.) 8. Go to 3. Enter this structured text: element_number := 0; SIZE(SINT_array, 0, SINT_array_size); While SINT_array[element_number] <> 13 do String_tag.DATA[element_number] := SINT_array[element_number]; element_number := element_number + 1; String_tag.LEN := element_number; If element_number = SINT_array_size then exit; end_if; end_while;
6-25
REPEATUNTIL
Operands:
Use the REPEATUNTIL loop to keep doing something until conditions are true.
Structured Text
REPEAT <statement>; UNTIL bool_expression END_REPEAT;
Type: BOOL
Enter: BOOL tag or expression that evaluates to a BOOL value (BOOL expression)
IMPORTANT
Make sure that you do not iterate within the loop too many times in a single scan. Thecontrollerdoesnotexecuteanyotherstatementsintheroutineuntil it completes the loop. If the time that it takes to complete the loop is greater than the watchdog timer for the task, a major fault occurs. Consider using a different construct, such as IF...THEN.
If there are conditions when you want to exit the loop early, use other statements, such as an IF...THEN construct, to condition an EXIT statement.
6-26
These diagrams show how a REPEAT...UNTIL loop executes and how an EXIT statement leaves the loop early.
statement 1 statement 2 statement 3 statement 4 BOOL expression false rest of the routine false rest of the routine statement 1 statement 2 statement 3 statement 4 Exit ? no BOOL expression true
true
yes
While the bool_expression is false, the controller executes only the statements within the REPEATUNTIL loop.
To stop the loop before the conditions are false, use an EXIT statement.
Example 1:
If you want this: Enter this structured text:
pos := -1; The REPEAT...UNTIL loop executes the statements in the construct and then determines if the conditions are true before REPEAT executing the statements again. pos := pos + 2; This differs from the WHILE...DO loop because the WHILE...DO The WHILE...DO loop evaluates its conditions first. If the conditions are true, the controller then executes the statements within the loop. The statements in a REPEAT...UNTIL loop are always executed at least once. The statements in a WHILE...DO loop might never be executed. UNTIL ((pos = 101) OR (structarray[pos].value = targetvalue)) end_repeat;
6-27
Example 2:
If you want this: Move ASCII characters from a SINT array into a string tag. (In a SINT array, each element holds one character.) Stop when you reach the carriage return. 1. Initialize Element_number to 0. 2. Count the number of elements in SINT_array (array that contains the ASCII characters) and store the result in SINT_array_size (DINT tag). 3. Set String_tag[element_number] = the character at SINT_array[element_number]. 4. Add 1 to element_number. This lets the controller check the next character in SINT_array. 5. Set the Length member of String_tag = element_number. (This records the number of characters in String_tag so far.) 6. If element_number = SINT_array_size, then stop. (You are at the end of the array and it does not contain a carriage return.) 7. If the character at SINT_array[element_number] = 13 (decimal value of the carriage return), then stop. Otherwise, go to 3. Enter this structured text: element_number := 0; SIZE(SINT_array, 0, SINT_array_size); Repeat String_tag.DATA[element_number] := SINT_array[element_number]; element_number := element_number + 1; String_tag.LEN := element_number; If element_number = SINT_array_size then exit; end_if; Until SINT_array[element_number] = 13 end_repeat;
6-28
Comments
To make your structured text easier to interpret, add comments to it. Comments let you use plain language to describe how your structured text works. Comments do not affect the execution of the structured text. Structured text comments are downloaded into controller memory and are available for upload. To add comments to your structured text:
To add a comment: on a single line at the end of a line of structured text within a line of structured text Use one of these formats: //comment (*comment*) /*comment*/ (*comment*) /*comment*/ that spans more than one line (*start of comment . . . end of comment*) /*start of comment . . . end of comment*/
For example:
Format: //comment Example: At the beginning of a line //Check conveyor belt direction IF conveyor_direction THEN... At the end of a line ELSE //If conveyor isnt moving, set alarm light light := 1; END_IF; (*comment*) Sugar.Inlet[:=]1;(*open the inlet*) IF Sugar.Low (*low level LS*)& Sugar.High (*high level LS*)THEN... (*Controls the speed of the recirculation pump. The speed depends on the temperature in the tank.*) IF tank.temp > 200 THEN... /*comment*/ Sugar.Inlet:=0;/*close the inlet*/ IF bar_code=65 /*A*/ THEN... /*Gets the number of elements in the Inventory array and stores the value in the Inventory_Items tag*/ SIZE(Inventory,0,Inventory_Items);
Chapter
13
Use a force to override data that your logic either uses or produces. For example, use forces in the following situations: test and debug your logic check wiring to an output device temporarily keep your process functioning when an input device has failed Use forces only as a temporary measure. They are not intended to be a permanent part of your application.
For this information: Precautions Check Force Status What to Force When to Use an I/O Force Add an I/O Force When to Use Step Through Step Through a Transition or a Force of a Path When to Use an SFC Force Add an SFC Force Remove or Disable Forces See page: 13-2 13-3 13-6 13-6 13-7 13-8 13-8 13-9 13-11 13-12
13-2
Precautions
ATTENTION
Forcingcancauseunexpectedmachinemotionthatcouldinjure personnel. Before you use a force, determine how the force will effect your machine or process and keep personnel away from the machine area. EnablingI/Oforcescausesinput,output,produced,orconsumedvalues to change. Enabling SFC forces causes your machine or process to go to a different state or phase. Removing forces may still leave forces in the enabled state. If forces are enabled and you install a force, the new force immediately takes effect.
Enable Forces
For a force to take effect, you enable forces. You can only enable and disable forces at the controller level. You can enable I/O forces and SFC forces separately or at the same time. You cannot enable or disable forces for a specific module, tag collection, or tag element. IMPORTANT If you download a project that has forces enabled, the programming software prompts you to enable or disable forces after the download completes. appears next to the forced
13-3
!
Check Force Status
Before you use a force, determine the status of forces for the controller. You can check force status:
To determine the status of: I/O forces Use any of the following: online toolbar FORCE LED GSV instruction SFC forces Online Toolbar
The Online toolbar shows the status of forces. It shows the status of I/O forces and SFC forces separately.
Forces tab
13-4
This: Enabled
Means: If the project contains any forces of this type, they are overriding your logic. If you add a force of this type, the new force immediately takes effect
Forces of this type are inactive. If the project contains any forces of this type, they are not overriding your logic. At least one force of this type exists in the project. No forces of this type exist in the project.
13-5
FORCE LED
If your controller has a FORCE LED, use the LED to determine the status of any I/O forces. IMPORTANT The FORCE LED shows only the status of I/O forces. It does not show that status of SFC forces.
Then: No tags contain force values. I/O forces are inactive (disabled). flashing solid At least one tag contains a force value. I/O forces are inactive (disabled). I/O forces are active (enabled). Force values may or may not exist.
GSV Instruction
IMPORTANT The ForceStatus attribute shows only the status of I/O forces. It does not show the status of SFC forces.
This example shows how to use a GSV instruction to get the status of forces.
13-6
What to Force
If you want to:
override an input value, output value, produced tag, or consumed tag override the conditions of a transition one time to go from an active step to the next step override one time the force of a simultaneous path and execute the steps of the path override the conditions of a transition in a sequential function chart execute some but not all the paths of a simultaneous branch of a sequential function chart
Use an I/O force to: override an input value from another controller (i.e., a consumed tag) override an input value from an input device override your logic and specify an output value for another controller (i.e., a produced tag) override your logic and specify the state of an output device
IMPORTANT
Forcing increases logic execution time. The more values you force, the longer it takes to execute the logic.
IMPORTANT
I/O forces are held by the controller and not by the programming workstation. Forces remain even if the programming workstation is disconnected. When you force an I/O value: You can force all I/O data, except for configuration data. If the tag is an array or structure, such as an I/O tag, force a BOOL, SINT, INT, DINT, or REAL element or member. If the data value is a SINT, INT, or DINT, you can force the entire value or you can force individual bits within the value. Individual bits can have a force status of: no force force on force off You can also force an alias to an I/O structure member, produced tag, or consumed tag. An alias tag shares the same data value as its base tag, so forcing an alias tag also forces the associated base tag.
13-7
Removing a force from an alias tag removes the force from the associated base tag.
To override an input value, output value, produced tag, or consumed tag, use an I/O force: Forcingcancauseunexpectedmachinemotionthatcouldinjure personnel. Before you use a force, determine how the force will effect your machine or process and keep personnel away from the machine area. EnablingI/Oforcescausesinput,output,produced,orconsumedvalues to change. If forces are enabled and you install a force, the new force immediately takes effect. 1. What is the state of the I/O Forces indicator?
If: off flashing Then note: No I/O forces currently exist. No I/O forces are active. But at least one force already exists in your project. When you enable I/O forces, all existing I/O forces will also take effect. I/O forces are enabled (active). When you install (add) a force, it immediately takes effect.
solid
2. Open the routine that contains the tag that you want to force.
13-8
3. Right-click the tag and choose Monitor If necessary, expand the tag to show the value that you want to force (e.g., BOOL value of a DINT tag). 4. Install the force value:
To force a: BOOL value non-BOOL value Do this: Right-click the tag and choose Force ON or Force OFF. In the Force Mask column for the tag, type the value to which you want to force the tag. Then press the Enter key.
To override a false transition one time and go from an active step to the next step, use the Step Through option. With the Step Through option: You do not have to add, enable, disable, or remove forces. The next time the SFC reaches the transition, it executes according to the conditions of the transition. This option also lets you override one time the false force of a simultaneous path. When you step through the force, the SFC executes the steps of the path.
To step through the transition of an active step or a force of a simultaneous path: 1. Open the SFC routine. 2. Right-click the transition or the path that is forced and choose Step Through.
13-9
Force a Transition
To override the conditions of a transition through repeated executions of an SFC, force the transition. The force remains until you remove it or disable forces.
If you want to: prevent the SFC from going to the next step cause the SFC go to the next step regardless of transition conditions Then: force the transition false force the transition true
If you force a transition within a simultaneous branch to be false, the SFC stays in the simultaneous branch as long as the force is active (installed and enabled). To leave a simultaneous branch, the last step of each path must execute at least one time and the transition below the branch must be true. Forcing a transition false prevents the SFC from reaching the last step of a path. When you remove or disable the force, the SFC can execute the rest of the steps in the path.
13-10
For example, to exit this branch, the SFC must be able to: execute Step_011 at least once get past Tran_011 and execute Step_012 at least once determine that Tran_012 is true
If you force a path of a simultaneous branch to be false, the SFC stays in the simultaneous branch as long as the force is active (installed and enabled).
13-11
To leave a simultaneous branch, the last step of each path must execute at least one time and the transition below the branch must be true. Forcing a path false prevents the SFC from entering a path and executing its steps. When you remove or disable the force, the SFC can execute the steps in the path.
Forcingcancauseunexpectedmachinemotionthatcouldinjure personnel. Before you use a force, determine how the force will effect your machine or process and keep personnel away from the machine area. Enabling SFC forces causes your machine or process to go to a different state or phase. If forces are enabled and you install a force, the new force immediately takes effect. 1. What is the state of the SFC Forces indicator?
If: off flashing Then note: No SFC forces currently exist. No SFC forces are active. But at least one force already exists in your project. When you enable SFC forces, all existing SFC forces will also take effect. SFC forces are enabled (active). When you install (add) a force, it immediately takes effect.
solid
2. Open the SFC routine. 3. Right-click the transition or start of a simultaneous path that you want to force, and choose either Force TRUE (only for a transition) or Force FALSE. 4. Are SFC forces enabled? (See step 1.)
If: no yes Then: From the Logic menu, choose SFC Forcing > Enable All SFC Forces. Then choose Yes to confirm. Stop.
13-12
!
If you want to: stop an individual force
And: leave other forces enabled and in effect leave the I/O forces in the project remove the I/O forces from the project leave the SFC forces in the project remove the SFC forces from the project
Then: Remove an Individual Force Disable All I/O Forces Remove All I/O Forces Disable All SFC Forces Remove All SFC Forces
stop all I/O forces but leave all SFC forces active stop all SFC forces but leave all I/O forces active
1. Open the routine that contains the force that you want to remove. 2. What is the language of the routine?
If: SFC ladder logic function block structured text Then: Go to step 4. Go to step 4. Go to step 3. Go to step 3.
3. Right-click the tag that has the force and choose Monitor If necessary, expand the tag to show the value that is forced (e.g., BOOL value of a DINT tag). 4. Right-click the tag or element that has the force and choose Remove Force.
Publication 1756-PM003H-EN-E (excerpt from 1756-PM001H-EN-P) - August 2005
13-13
13-14
Notes:
Chapter
10
Introduction
If you want to:
Jump to a separate routine, pass data to the routine, execute the routine, and return results.
10-2
The JSR instruction jumps execution to a different routine. The SBR and RET instructions are optional instructions that exchange data with the JSR instruction.
Relay Ladder
Operand: Routine name Input parameter Type: ROUTINE BOOL SINT INT DINT REAL structure BOOL SINT INT DINT REAL structure Format: name immediate tag array tag Description: routine to execute (i.e., subroutine) data from this routine that you want to copy to a tag in the subroutine Input parameters are optional. Enter multiple input parameters, if needed. tag in this routine to which you want to copy a result of the subroutine Return parameters are optional. Enter multiple return parameters, if needed.
Return parameter
Structured Text
JSR(RoutineName,InputCount, InputPar,ReturnPar);
Type: ROUTINE SINT INT DINT REAL BOOL SINT INT DINT REAL structure BOOL SINT INT DINT REAL structure
Input parameter
data from this routine that you want to copy to a tag in the subroutine Input parameters are optional. Enter multiple input parameters, if needed. tag in this routine to which you want to copy a result of the subroutine Return parameters are optional. Enter multiple return parameters, if needed.
Return parameter
10-3
The operands are the same as those for the relay ladder JSR instruction.
ATTENTION
For each parameter in a SBR or RET instruction, use the same data type (including any array dimensions) as the corresponding parameter in the JSR instruction. Using different data types may produce unexpected results.
10-4
SBR Operands: The SBR instruction must be the first instruction in a relay ladder or
structured text routine.
Relay Ladder
Operand: Input parameter Type: BOOL SINT INT DINT REAL structure Format: tag array tag Description: tag in this routine into which you want to copy the corresponding input parameter from the JSR instruction
SBR(InputPar);
Structured Text
The operands are the same as those for the relay ladder SBR instruction.
Function Block
Parameters
The operands are the same as those for the relay ladder SBR instruction.
10-5
RET(ReturnPar);
Structured Text
The operands are the same as those for the relay ladder RET instruction.
Function Block
Parameters
The operands are the same as those for the relay ladder RET instruction.
Description: The JSR instruction initiates the execution of the specified routine,
which is referred to as a subroutine: The subroutine executes one time. After the subroutine executes, logic execution returns to the routine that contains the JSR instruction. To program a jump to a subroutine, follow these guidelines: IMPORTANT Do not use a JSR instruction to call (execute) the main routine. You can put a JSR instruction in the main routine or any other routine. If you use a JSR instruction to call the main routine and then put a RET instruction in the main routine, a major fault occurs (type 4, code 31).
10-6
SBR JSR 1. If you want to copy data to a tag in the subroutine, enter an input parameter. 2. If you want to copy a result of the subroutine to a tag in this routine, enter a return parameter. 3. Enter as many input and return parameters as you need. JSR
RET
42974
RET 1. If the JSR instruction has a return parameter, enter an RET instruction. 2. Place the RET instruction as the last instruction in the routine. 3. For each return parameter in the JSR instruction, enter a return parameter to send to the JSR instruction. 4. In a ladder routine, place additional RET instructions to exit the subroutine based on different input conditions, if required. (Function block routines only permit one RET instruction.)
There are no restrictions, other than controller memory, on the number of nested routines you can have or the number of parameters you pass or return.
level 1 subroutine action_1 main routine action_1 JSR action_2 JSR action_3 JSR SBR SBR SBR level 2 subroutine action_2 level 3 subroutine action_3
RET
RET
RET
10-7
Arithmetic Status Flags: Arithmetic status flags are affected. Fault Conditions:
A major fault will occur if: JSR instruction has fewer input parameters than SBR instruction JSR instruction jumps to a fault routine RET instruction has fewer return parameters than JSR instruction main routine contains a RET instruction Fault type: 4 4 or user-supplied 4 4 Fault code: 31 0 or user-supplied 31 31
The controller executes all subroutines regardless of rung condition. To ensure that all rungs in the subroutine are prescanned, the controller ignores RET instructions. (I.e., RET instructions do not exit the subroutine.) Release 6.x and earlier, input and return parameters are passed. Release 7.x and later, input and return parameters are not passed. If recursive calls exist to the same subroutine, the subroutine is prescanned only the first time. If multiple calls exist (non-recursive) to the same subroutine, the subroutine is prescanned each time. The rung-condition-out is set to false (relay ladder only). na
rung-condition-in is false to The subroutine does not execute. the JSR instruction Outputs in the subroutine remain in their last state. The rung-condition-out is set to false. rung-condition-in is true EnableIn is set The instruction executes. The rung-condition-out is set to true. na
10-8
input parameters
yes
no
yes
no
no
no rung-condition-out is set to false continue executing routine rung-condition-out is set to true logic execution returns to JSR
end
postscan
Function Block
Condition: prescan instruction first scan instruction first run normal execution Action: No action taken. No action taken. No action taken. 1. If the routine contains an SBR instruction, the controller first executes the SBR instruction. 2. The controller latches all data values in IREFs. 3. The controller executes the other function blocks in the order that is determined by their wiring. This includes other JSR instructions. 4. The controller writes outputs in OREFs. 5. If the routine contains an RET instruction, the controller executes the RET instruction last. The subroutine is called. If the routine is an SFC routine, the routine in initialized the same as it is during prescan.
postscan
10-9
Relay Ladder
Routine: Main routine Program:
Subroutine
Structured Text
Routine: Main routine Subroutine Program: JSR(routine_1,2,value_1,value_2,float_value_1); SBR(value_a,value_b); <statements>; RET(float_a);
10-10
Adds the value in cookies_1 to cookies_2 and stores the result in total_cookies.
Subroutine_1 When def is on, the RET instruction returns value_1 to the JSR cookies_1 parameter and the rest of the subroutine is not scanned.
When def is off (previous rung) and ghi is on, the RET instruction returns value_2 to the JSR cookies_1 parameter and the rest of the subroutine is not scanned.
When both def and ghi are off (previous rungs), the RET instruction returns value_3 to the JSR cookies_1 parameter.
10-11
42972
1. The values in Add_Input_1, Add_Input_2, and Add_Input_3 are copied to Input_A, Input_B, and Input_C, respectively.
42973
2. The ADD instructions add Input_A, Input_B, and Input_C and place the result in Sum_A_B_C.
10-12
Notes:
vr
Pub. Title/Type SFC and ST Programming Languages Programming Manual Cat. No.
1756 ControlLogix, 1769 CompactLogix, 1789 SoftLogix, 1794 FlexLogix, PowerFlex 700S with DriveLogix
Pub. No.
1756-PM003H-EN-E
Part No.
957974-51
Please complete the sections below. Where applicable, rank the feature (1=needs improvement, 2=satisfactory, and 3=outstanding).
Overall Usefulness 1 2 3 How can we make this publication more useful for you?
Can we add more information to help you? procedure/step example explanation illustration guideline definition feature other
Other Comments
Location/Phone Would you like us to contact you regarding your comments? ___No, there is no need to contact me ___Yes, please call me ___Yes, please email me at __________________________ ___Yes, please contact me via ________________________
Allen-Bradley Marketing Communications, 1 Allen-Bradley Dr., Mayfield Hts., OH 44124-9705 Phone: 440-646-3176 Fax: 440-646-3525 Email: [email protected]
PN 955107-82
Other Comments
PLEASE REMOVE
[ctrl-@] NUL 0
Rockwell Automation provides technical information on the web to assist you in using our products. At http://support.rockwellautomation.com, you can find technical manuals, a knowledge base of FAQs, technical and application notes, sample code and links to software service packs, and a MySupport feature that you can customize to make the best use of these tools. For an additional level of technical phone support for installation, configuration and troubleshooting, we offer TechConnect Support programs. For more information, contact your local distributor or Rockwell Automation representative, or visit http://support.rockwellautomation.com.
Installation Assistance
If you experience a problem with a hardware module within the first 24 hours of installation, please review the information that's contained in this manual. You can also contact a special Customer Support number for initial help in getting your module up and running: United States Outside United States 1.440.646.3223 Monday Friday, 8am 5pm EST Please contact your local Rockwell Automation representative for any technical support issues.
ControlNet is a trademark of ControlNet International, Ltd. DeviceNet is a trademark of the Open DeviceNet Vendor Association.
PN 957974-51
Copyright 2005 Rockwell Automation, Inc. All rights reserved. Printed in the U.S.A.
Programming Manual