Simple Timer in ST?

nfaust
2012-02-03
2012-03-03
  • nfaust - 2012-02-03

    Hi all-

    Relatively new here to PLC's and the Codesys environment. I have a small task where I need to start/stop a simple timer depending on digital/analog inputs (think starting and stopping a stopwatch). For example:

    VAR
       trigger1: F_TRIG;
       trigger2: F_TRIG;
       trigger1Count: CTU;
       trigger2Count: CTU;
       delayTimer: TON;
    END_VAR
    trigger1(CLK := digital1);
    trigger2(CLK := digital2);
    trigger1Count(CU := trigger1.Q);
    trigger2Count(CU := trigger2.Q);
    IF trigger1Count.CV = 1 AND trigger2Count.CV = 0 AND analog1 < 100 THEN
       (* Start Timer 1 *)
       (* Start Timer 2 *)
       delayTimer(IN := TRUE, PT := T#5s);
    ELSE trigger2Count.CV = 1 THEN
       (* End Timer 1 *)
       IF delayTimer.Q AND analog1 < 100 THEN
          (* End Timer 2 *)
       END_IF
    END_IF
    

    This does not seem conducive to the pre-built functions (IE - TON/TOF/TP). Can anyone recommend a technique for how to build one? Note that the output of the timers would need to be passed to another variable (to be held in memory) and then reset. Ideally, I'm shooting for a resolution of ~10ms.

    Thanks,
    Nick

    Edit to add:
    Thus far I have tried two approaches to this. The first, just a simple counter (IE- i:= i+1), but I was getting different results, I assume dependent on the cycle time. The second, I tried just using a TON, but could not figure out how to correctly stop the TON (it would continue running until it reached PT), nor how to correctly reset it.

    Additionally, can anyone recommend a good reference/instruction manual for ST (or IEC 61131-3 in general)? Preferably one that is available in the US.

     
  • shooter - 2012-02-05

    start simpler by using a timer:
    your IF statements are not working.
    No comments are used.
    program is not complete
    If above send it again and we will have a look at it.

    time1(IN:=bstart , PT:=tprocestime , Q=>btimeexpired , ET=>tmssincestart );
    yes you can define them elsewhere like time1.in:= true;
    You should set the PT outside your program as whenever you run over this command the time will change.
    there is an internal timer running better to use that one.

    if new please download w www.oscat.de w the txt version of it. you will understand a lot in ST.
    if you download the lib too you can make whatever you like.

     
  • nfaust - 2012-02-06

    shooter hat geschrieben:
    start simpler by using a timer:
    your IF statements are not working.
    No comments are used.
    program is not complete
    If above send it again and we will have a look at it.

    The comments were merely to indicate functionality. IE - I'd like to start two timers at the same time, but stop them at different times.

    shooter hat geschrieben:
    time1(IN:=bstart , PT:=tprocestime , Q=>btimeexpired , ET=>tmssincestart );
    yes you can define them elsewhere like time1.in:= true;
    You should set the PT outside your program as whenever you run over this command the time will change.
    there is an internal timer running better to use that one.

    I haven't had luck using a TON, because I cannot stop it (even by setting time1.IN := FALSE, it continues to run). I do not have a use for PT, as I do not want to set an "upper bound" on when the timer stops (nor do I know when it needs to stop).

    shooter hat geschrieben:
    if new please download http://www.oscat.de the txt version of it. you will understand a lot in ST.
    if you download the lib too you can make whatever you like.

    Is there an english version?

     
  • shooter - 2012-02-06

    oscat is in english written.
    there is even a PDF file in english now.

    A timer is used to give a signal after a PT time.
    so you must set a time in it.
    You can otherwise use a internal timer in mseconds.
    in oscat it is called now().
    If you like to know the expired time you can calculate the ET and use it in a formula.
    please be a little more clearer what you are looking for.

     
  • spfeif - 2012-03-03

    One thing to be careful about is you are trapping the Timer function block call in an IF statement. You need to reset the timer some where else by calling it's instance with IN := FALSE other wise next time you enter the IF statement the timer will have saved the state and will not reset.

     

Log in to post a comment.