ModSlaveSim help v3.04 - 4.4.5. Timers

Download manual: HTML

4.4.5. Timers

There are two basic methods for timing an interval using the ModSlaveSim language:

  • Save the value of TimeNow in a register at the start of the interval. At the end of the interval, subtract the saved value from the current value of TimeNow to get the length of the interval.

    The register used to store TimeNow must be a float64 for this method to work (the value of TimeNow is too large to fit in any other register type).

  • Increment a register by the value of CycleTime on each cycle during the interval.

    In this case, the register need not be a float64. However, if you want to use an integer register and want sub-second precision, you will have to scale CycleTime before adding it to the register value (e.g. multiply by 10 to work in tenths of a second).

Both these methods are illustrated in the following examples.

4.4.5.1. Retentive timer

This timer times how long its Input is non-zero. It is called a "retentive" timer because it retains its accumulated Time when the Input becomes zero (and it has stopped timing) and continues accumulating when the Input becomes non-zero again.

In order to stop accumulating successive times, a retentive timer has to be cleared explicitly, and is done here by setting Time to 0 if Reset ($101) is non-zero.

Examples of its use might be to record how long a pump or engine has been running.

Table 13. Retentive timer
Register 
Address
Register 
Name
Statement
$100 Input  
$101 Reset  
$102 Time
if $101 != 0 then 0
else if $100 != 0 then $$ + CycleTime
4.4.5.2. On-delay timer

When this timer's Input turns on (becomes non-zero), it waits for a specified Delay period and then turns on its Output. When the Input turns off, the Output is turned off immediately.

Table 14. On-delay timer
Register 
Address
Register 
 Name
Statement
$100 Input  
$101 Delay  
$102 On time if $100 == 0 then TimeNow
$103 Output
if $100 == 0 then 0
else if TimeNow > $102 + $101 then 1
  • The On time register is a sampler, which records the time at which the Input turned on.

4.4.5.3. Off-delay timer

When this timer's Input turns on (becomes non-zero), it turns on its Output immediately. However, when the Input turns off, it waits for a specified Delay period before turning off its Output.

Table 15. Off-delay timer
Register 
Address
Register 
Name
Statement
$100 Input  
$101 Delay  
$102 Off time if $100 != 0 then TimeNow
$103 Output
if $100 != 0 then 1
else if TimeNow > $102 + $101 then 0
4.4.5.4. On/off-delay timer

This timer combines the on-delay timer and off-delay timer.

Table 16. On/off-delay timer
Register Address Register Name Statement
$100 Input  
$101 On delay  
$102 Off delay  
$103 On time if $100 == 0 then TimeNow
$104 Off time if $100 != 0 then TimeNow
$105 Output
if $100 != 0 && TimeNow > $103 + $101 then 1
else if $100 == 0 && TimeNow > $104 + $102 then 0
4.4.5.5. Periodic timer

This timer repeatedly waits for a set Period and then sets its Output to 1 for one cycle.

Table 17. Periodic timer
Register Address Register Name Statement
$100 Period  
$101 Output
if $$ != 0 then 0
else if $102 >= $100 then 1
$102 Elapsed time
if $$ >= $100
then ($$ - $100) + CycleTime
else $$ + CycleTime

The Elapsed time register holds the time since the start of the current timer period. It is incremented by CycleTime on each cycle until it equals or exceeds the Period, when it is re-initialized before being incremented. The timer cycle and ModSlaveSim's cycle are not synchronized, so the Elapsed time has to be re-initialized to the partial ModSlaveSim cycle ($$ - $100) that has already occurred instead of to zero.