Time and number counter in one?

395 views Asked by At

I want to check how long a motor is working since the last check up and how many times the motor startet up since then.

#xSekundenzaehler := "Clock_1Hz";


REGION Allgemein
  #Betriebszeit_Anlage(CU:="eHauptschuetz" AND #xSekundenzaehler,
                       R:="Lebensdauer".Allgemein.xResetBetriebszeit,
                       PV:=0,
                       CV=>#rSecondsAnlage);
  
  "Lebensdauer".Allgemein.rLaufzeitAnlage := #rSecondsAnlage / 3600;
END_REGION

This is the code i used. Do i have to use another "CTU" again or is there a smarter way to do this in the already existing one that i don't know of?

1

There are 1 answers

0
Julian Maza On

Usually storing important values on CTU is not recommendable because you have no control on whether TiaPortal may restore the CTU instance.

It is preferable to build a counter by yourself so its value is kept on a non volatile db var:

"R_TRIG_1Hz"(CLK := "Clock_1Hz");
// Condition to count up running time
IF "eHauptschuetz" THEN
    // Only at rising edges of seconds
    IF "R_TRIG_1Hz".Q THEN
        "DBZeit".LebensdauerSeconds := "DBZeit".LebensdauerSeconds + 1;
    END_IF;
END_IF;

// Reset prevails over increment
IF #xReset THEN
    #xReset := false;
    "DBZeit".LebensdauerSeconds := 0;
END_IF;

// Hour total
"DBZeit".LebensdauerHours := "DBZeit".LebensdauerSeconds / 3600;

// Hour total with a decimal place
"DBZeit".LebensdauerDeciHours := "DBZeit".LebensdauerSeconds / 360;

This is the definition of the DB

Definition of DB

Please note that only seconds are remanent, as hours and decihours are calculated on every cycle.

It would be better to store hours and decihours in other DB, so this one is dedicated to remanent values. When you modify the DB variables their values are reset to the start values. Please remember to make a snapshot and copy them to start values prior to any changes.