<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to TON declared as PERSISTENT</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/0b0c35e833/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/0b0c35e833/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/0b0c35e833/</id><updated>2022-02-18T13:41:38.453000Z</updated><subtitle>Recent posts to TON declared as PERSISTENT</subtitle><entry><title>TON declared as PERSISTENT</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/0b0c35e833/?limit=25#9c21" rel="alternate"/><published>2022-02-18T13:41:38.453000Z</published><updated>2022-02-18T13:41:38.453000Z</updated><author><name>ldetzler</name><uri>https://forge.codesys.com/u/ldetzler/</uri></author><id>https://forge.codesys.com01fe843f9209c334999b374450e82ef4e929728b</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;I think I've answered my own question. &lt;/p&gt;
&lt;p&gt;I believe that TON is using the TIME() function or something similar internally. If it does, the TON function would look something like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;TIME&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="nt"&gt;function&lt;/span&gt;
&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;This&lt;/span&gt; &lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="nt"&gt;yields&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;time&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;in&lt;/span&gt; &lt;span class="nt"&gt;milliseconds&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nt"&gt;that&lt;/span&gt; &lt;span class="nt"&gt;has&lt;/span&gt; &lt;span class="nt"&gt;elapsed&lt;/span&gt; &lt;span class="nt"&gt;since&lt;/span&gt; &lt;span class="nt"&gt;system&lt;/span&gt; &lt;span class="nt"&gt;boot&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;

&lt;span class="nt"&gt;StartONS&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;CLK&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="nt"&gt;IN&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nt"&gt;IF&lt;/span&gt; &lt;span class="nt"&gt;StartONS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Q&lt;/span&gt; &lt;span class="nt"&gt;THEN&lt;/span&gt;
    &lt;span class="nt"&gt;StartTime&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nt"&gt;TIME&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nt"&gt;END_IF&lt;/span&gt;

&lt;span class="nt"&gt;IF&lt;/span&gt; &lt;span class="nt"&gt;NOT&lt;/span&gt; &lt;span class="nt"&gt;IN&lt;/span&gt; &lt;span class="nt"&gt;THEN&lt;/span&gt;
    &lt;span class="nt"&gt;StartTime&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="nt"&gt;TIME&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="nt"&gt;Q&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="nt"&gt;FALSE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;END_IF&lt;/span&gt;

&lt;span class="nt"&gt;ET&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nt"&gt;TIME&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="nt"&gt;-StartTime&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;        
&lt;span class="nt"&gt;IF&lt;/span&gt; &lt;span class="nt"&gt;ET&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt;&lt;span class="nt"&gt;PT&lt;/span&gt; &lt;span class="nt"&gt;THEN&lt;/span&gt;
    &lt;span class="nt"&gt;Q&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="nt"&gt;TRUE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;END_IF&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Because the TON is stored in PERSISTENT RETAIN Memory, StartTime is retained through a power cycle. &lt;br/&gt;
The first time that the TON code is scanned after the PLC boots, TIME() will return a very small value, since it has just booted.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="err"&gt;ET := TIME()-StartTime;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Results in an overflow causing ET to be large. When we get to this line of code, ET is greater than PT and Q is set TRUE.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="err"&gt;IF ET&amp;gt;=PT THEN&lt;/span&gt;
&lt;span class="err"&gt;    Q:=TRUE;&lt;/span&gt;
&lt;span class="err"&gt;END_IF&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;img src="./attachment/Time.png" width="50%"/&gt;&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>TON declared as PERSISTENT</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/0b0c35e833/?limit=25#400e" rel="alternate"/><published>2022-02-17T20:08:56.218000Z</published><updated>2022-02-17T20:08:56.218000Z</updated><author><name>ldetzler</name><uri>https://forge.codesys.com/u/ldetzler/</uri></author><id>https://forge.codesys.com28904a404cb31f4047f8757e8297e0fbe7a763ee</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Can someone help me understand what is happening internally for a TON that is declared as PERSISTENT RETAIN?&lt;/p&gt;
&lt;p&gt;VAR_GLOBAL PERSISTENT RETAIN&lt;br/&gt;
    PersistTON: TON;&lt;br/&gt;
    Enable: BOOL :=TRUE;&lt;br/&gt;
END_VAR&lt;/p&gt;
&lt;p&gt;PROGRAM Main&lt;br/&gt;
VAR&lt;br/&gt;
    Test: BOOL;&lt;br/&gt;
END_VAR&lt;/p&gt;
&lt;p&gt;PersistTON(IN:= Enable, PT:=T#10M);&lt;br/&gt;
IF PersistTON.Q THEN&lt;br/&gt;
    Test:=TRUE;&lt;br/&gt;
END_IF&lt;/p&gt;
&lt;p&gt;If I power cycle the PLC when the timer is timing (well before ET=PT), PersistTON.Q is TRUE on the first scan and PersistTON.ET is set to PersistTON.PT. &lt;/p&gt;
&lt;p&gt;If I power cycle when ET=T#30s, as soon as the logic is scanned again ET:=T#10M and Q is True.&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>