TON declared as PERSISTENT

ldetzler
2022-02-17
2022-02-18
  • ldetzler - 2022-02-17

    Can someone help me understand what is happening internally for a TON that is declared as PERSISTENT RETAIN?

    VAR_GLOBAL PERSISTENT RETAIN
        PersistTON: TON;
        Enable: BOOL :=TRUE;
    END_VAR
    
    PROGRAM Main
    VAR
        Test: BOOL;
    END_VAR
    
    PersistTON(IN:= Enable, PT:=T#10M);
    IF PersistTON.Q THEN
        Test:=TRUE;
    END_IF
    

    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.

    If I power cycle when ET=T#30s, as soon as the logic is scanned again ET:=T#10M and Q is True.

     

    Last edit: ldetzler 2022-02-17
  • ldetzler - 2022-02-18

    I think I've answered my own question.

    I believe that TON is using the TIME() function or something similar internally. If it does, the TON function would look something like this:

    //TIME() function
    //This function yields the time (in milliseconds) that has elapsed since system boot.
    
    StartONS(CLK:=IN);
    IF StartONS.Q THEN
        StartTime := TIME();
    END_IF
    
    IF NOT IN THEN
        StartTime:=TIME();
        Q:=FALSE;
    END_IF
    
    ET := TIME()-StartTime;        
    IF ET>=PT THEN
        Q:=TRUE;
    END_IF
    

    Because the TON is stored in PERSISTENT RETAIN Memory, StartTime is retained through a power cycle.
    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.

    ET := TIME()-StartTime;
    

    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.

    IF ET>=PT THEN
        Q:=TRUE;
    END_IF
    

     

Log in to post a comment.