Operation of the timers
Timer |
Timing diagram |
|---|---|
TP: Generate pulse The TP timer generates a pulse with a preset width time. |
|
TON: Generate ON-delay The TON timer sets output Q to ON after a preset time delay. |
|
TOF: Generate OFF-delay The TOF timer resets output Q to OFF after a preset time delay. |
|
TONR: Time accumulator The TONR timer sets output Q to ON after a preset time delay. Elapsed time is accumulated over multiple timing periods until the R input is used to reset the elapsed time. |
In the CPU, no dedicated resource is allocated to any specific timer instruction. Instead, each timer utilizes its own timer structure in DB memory and a continuously-running internal CPU timer to perform timing.
When a timer is started due to an edge change on the input of a TP, TON, TOF, or TONR instruction, the value of the continuously-running internal CPU timer is copied into the START member of the DB structure allocated for this timer instruction. This start value remains unchanged while the timer continues to run, and is used later each time the timer is updated. Each time the timer is started, a new start value is loaded into the timer structure from the internal CPU timer.
When a timer is updated, the start value described above is subtracted from the current value of the internal CPU timer to determine the elapsed time. The elapsed time is then compared with the preset to determine the state of the timer Q bit. The ELAPSED and Q members are then updated in the DB structure allocated for this timer. Note that the elapsed time is clamped at the preset value (the timer does not continue to accumulate elapsed time after the preset is reached).
A timer update is performed when and only when:
A timer instruction (TP, TON, TOF, or TONR) is executed
The "ELAPSED" member of the timer structure in DB is referenced directly by an instruction
The "Q" member of the timer structure in DB is referenced directly by an instruction
Timer programming
The following consequences of timer operation should be considered when planning and creating your user program:
You can have multiple updates of a timer in the same scan. The timer is updated each time the timer instruction (TP, TON, TOF, TONR) is executed and each time the ELAPSED or Q member of the timer structure is used as a parameter of another executed instruction. This is an advantage if you want the latest time data (essentially an immediate read of the timer). However, if you desire to have consistent values throughout a program scan, then place your timer instruction prior to all other instructions that need these values, and use tags from the Q and ET outputs of the timer instruction instead of the ELAPSED and Q members of the timer DB structure.
You can have scans during which no update of a timer occurs. It is possible to start your timer in a function, and then cease to call that function again for one or more scans. If no other instructions are executed which reference the ELAPSED or Q members of the timer structure, then the timer will not be updated. A new update will not occur until either the timer instruction is executed again or some other instruction is executed using ELAPSED or Q from the timer structure as a parameter.
Although not typical, you can assign the same DB timer structure to multiple timer instructions. In general, to avoid unexpected interaction, you should only use one timer instruction (TP, TON, TOF, TONR) per DB timer structure.
Self-resetting timers are useful to trigger actions that need to occur periodically. Typically, self-resetting timers are created by placing a normally-closed contact which references the timer bit in front of the timer instruction. This timer network is typically located above one or more dependent networks that use the timer bit to trigger actions. When the timer expires (elapsed time reaches preset value), the timer bit is ON for one scan, allowing the dependent network logic controlled by the timer bit to execute. Upon the next execution of the timer network, the normally closed contact is OFF, thus resetting the timer and clearing the timer bit. The next scan, the normally closed contact is ON, thus restarting the timer. When creating self-resetting timers such as this, do not use the "Q" member of the timer DB structure as the parameter for the normally-closed contact in front of the timer instruction. Instead, use the tag connected to the "Q" output of the timer instruction for this purpose. The reason to avoid accessing the Q member of the timer DB structure is because this causes an update to the timer and if the timer is updated due to the normally closed contact, then the contact will reset the timer instruction immediately. The Q output of the timer instruction will not be ON for the one scan and the dependent networks will not execute.
Time data retention after a RUN-STOP-RUN transition or a CPU power cycle
If a run mode session is ended with stop mode or a CPU power cycle and a new run mode session is started, then the timer data stored in the previous run mode session is lost, unless the timer data structure is specified as retentive (TP, TON, TOF, and TONR timers).
When you accept the defaults in the call options dialog after you place a timer instruction in the program editor, you are automatically assigned an instance DB which cannot be made retentive. To make your timer data retentive, you must either use a global DB or a Multi-instance DB.
Assign a global DB to store timer data as retentive data
This option works regardless of where the timer is placed (OB, FC, or FB).
Create a global DB:
Double-click "Add new block" from the Project tree
Click the data block (DB) icon
For the Type, choose global DB
If you want to be able to select individual data elements in this DB as retentive, be sure the DB type "Optimized" box is checked. The other DB type option "Standard - compatible with S7-300/400" only allows setting all DB data elements retentive or none retentive.
Click OK
Add timer structure(s) to the DB:
In the new global DB, add a new static tag using data type IEC_Timer.
In the "Retain" column, check the box so that this structure will be retentive.
Repeat this process to create structures for all the timers that you want to store in this DB. You can either place each timer structure in a unique global DB, or you can place multiple timer structures into the same global DB. You can also place other static tags besides timers in this global DB. Placing multiple timer structures into the same global DB allows you to reduce your overall number of blocks.
Rename the timer structures if desired.
Open the program block for editing where you want to place a retentive timer (OB, FC, or FB).
Place the timer instruction at the desired location.
When the call options dialog appears, click the cancel button.
On the top of the new timer instruction, type the name (do not use the helper to browse) of the global DB and timer structure that you created above (example: "Data_block_3.Static_1").
Assign a multi-instance DB to store timer data as retentive data
This option only works if you place the timer in an FB.
This option depends upon whether the FB properties specify "Optimized block access" (allows symbolic access only). To verify how the access attribute is configured for an existing FB, right-click on the FB in the Project tree, choose properties, and then choose Attributes.
If the FB specifies "Optimized block access" (allows symbolic access only):
Open the FB for edit.
Place the timer instruction at the desired location in the FB.
When the Call options dialog appears, click the Multi instance icon. The Multi Instance option is only available if the instruction is being placed into an FB.
In the Call options dialog, rename the timer if desired.
Click OK. The timer instruction appears in the editor, and the IEC_TIMER structure appears in the FB Interface under Static.
If necessary, open the FB interface editor (may have to click on the small arrow to expand the view).
Under Static, locate the timer structure that was just created for you.
In the Retain column for this timer structure, change the selection to "Retain". Whenever this FB is called later from another program block, an instance DB will be created with this interface definition which contains the timer structure marked as retentive.
If the FB does not specify "Optimized block access", then the block access type is standard, which is compatible with S7-300/400 classic configurations and allows symbolic and direct access. To assign a multi-instance to a standard block access FB, follow these steps:
Open the FB for edit.
Place the timer instruction at the desired location in the FB.
When the Call options dialog appears, click on the multi instance icon. The multi instance option is only available if the instruction is being placed into an FB.
In the Call options dialog, rename the timer if desired.
Click OK. The timer instruction appears in the editor, and the IEC_TIMER structure appears in the FB Interface under Static.
Open the block that will use this FB.
Place this FB at the desired location. Doing so results in the creation of an instance data block for this FB.
Open the instance data block created when you placed the FB in the editor.
Under Static, locate the timer structure of interest. In the Retain column for this timer structure, check the box to make this structure retentive.