Author Topic: TIMER Contact Behavior  (Read 13677 times)

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
TIMER Contact Behavior
« on: September 13, 2017, 04:41:50 PM »
I have run into a ladder logic behavior that I can't quite figure out.
I have attached a screen shot of the entire program.

The middle rung of logic is a simple oscillator that uses a TIMER and it's inverted contact in a bit of a loop.  I am an old hardware guy and I'd describe this as either a relaxation oscillator or a ring oscillator.

My expectations are that once the TIMER runs out that it's contact would go active for 1 entire scan of the ladder logic and then go inactive for the SV period of the TIMER.

This contact behavior is consistent with both the simulator and my Nano-10 test hardware.


What I observe:
  • The TIMER counts down and reloads with a period determined by it's SV.  It oscillates as expected!
  • The TIMER's contact goes active and inactive and can be "seen" on ladder rungs before the TIMER.  As expected!
  • The contact appears to only be inactive after the rung with the TIMER.
The sample code has 2 simple pulse catches that sets RELAYs if the TIMER contact is ever active.  These RELAYs can only be cleared by restarting the PLC or via the online monitoring program.

Why can't I come up with a good story as to why the RELAY contact is not visible below the line of code with the TIMER, but is visible on the next scan to ladder rungs that come before the TIMER.

I am just getting too senile to work with this stuff???

Best regards,

Gary D*ckinson
« Last Edit: September 13, 2017, 10:12:44 PM by garysdickinson »

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:TIMER Contact Behavior
« Reply #1 on: September 13, 2017, 10:12:20 PM »
OK, thought about it and I think that I can explain how TIMERs work.

1.  TIMERs are implemented in PLC firmware.
2.  Each TIMER has a data structure managed by the PLC firmware. This data
   structure for each TIMER includes these things and probably a few other items:
      TIMER SV
      TIMER PV
      TIMER control input state
      TIMER contact state

OK, with that in mind, this is how I think a TIMER works:
1. Following reset, the input state for all TIMERs is set to zero to indicate that the TIMERs are inactive.
   The contact associated with each TIMER is set to 0 to indicate that the TIMER has not timed out.
2. On the each scan of the ladder logic the evaluates the control input state for each structure during the
   ladder logic scan.  This state is maintained in the data structure associated with the TIMER.
3. If the ladder logic scan determines that the TIMER control input is 0 or false, the contact associated with the
   TIMER is set to 0 or de-asserted.
4. If the ladder logic scan detects that the TIMER control input has transitioned to l, then the TIMER is active.
   The TIMER SV is reloaded and the TIMER's contact is set to 0 or FALSE. These changes happen when the
   rung of ladder logic that controls the TIMER is executed.  These changes are visible to all other rungs of ladder
   logic that follows the rung that controls the TIMER.
5. At the start of each ladder logic scan, each TIMER that is active at the end of the previous scan may have its PV
   decremented based on either a 0.1 second or 0.01 clock. If the TIMER's PV is now 0, then the contact associated
   with this TIMER will be set asserted (set TRUE).  Please note that contact transitions for 0..1 at the start of the
   ladder logic scan and NOT at the rung of code that controls the TIMER.

The most important points to take away from this long winded guess on TIMER behavior is that a TIMER's contact is set TRUE (1) at the beginning of the scan and the contact is set FALSE (0) the ladder logic rung that controls the TIMER is evaluated.

The fact that the TIMER contact is set and cleared at different points in the execution of the ladder logic explains why the TIMER contact can be "seen" as TRUE before the ladder rung that controls the TIMER and is never seen after this rung in my one line PLC code to implement an oscillator.

This is a bit of tricky TIMER stuff but it is not broken, but it is now documented.


Best regards,

Gary D*ckinson



« Last Edit: September 13, 2017, 10:14:50 PM by garysdickinson »

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:TIMER Contact Behavior
« Reply #2 on: September 19, 2017, 02:47:31 AM »
Thanks for helping to document the timer behavior. You are mostly right simply based on your observation! The followings describe exactly what happens during the timer updates:

1. Following reset, the input state for all TIMERs is set to zero to indicate that the TIMERs are inactive.
   The contact associated with each TIMER is set to 0 to indicate that the TIMER has not timed out.
   The timer present value are all set to -1, indicating that the timer is inactive.

2. On the each scan of the ladder logic the evaluates the control input state for each structure during the
   ladder logic scan.  This state is maintained in the data structure associated with the TIMER.

3. If the ladder logic scan determines that the TIMER control input is 0 or false, the contact associated with the
   TIMER is set to 0 or de-asserted and the timer PV is set to -1.

4. If the ladder logic scan detects that the TIMER control input has transitioned to l, then the TIMER is active.
   The TIMER PV is reloaded from SV and the TIMER's contact is set to 0 or FALSE. These changes happen when the
   rung of ladder logic that controls the TIMER is executed.  These changes are visible to all other rungs of ladder
   logic that follows the rung that controls the TIMER.

5. Every 0.01s the timer countdown task is run. If a timer's PV is >= 0 it will be processed either every time the
   timer countdown task is run (for high speed timers with 0.01s time-base) or every 10th time the timer countdown
   task is run (for regular timers with 0.1s time-base). Timer countdown task runs on interrupt and
   is independent of the PLC program scan time so that the timer can count down accurately even if the program
   scan time is > 0.01s.

   At the start of each ladder logic scan, each TIMER that is active at the end of the previous scan may have its PV
   decremented based on either a 0.1 second or 0.01 clock.
, the I/O scan cycle check and if the TIMER's PV is now 0,
   then the contact associated with this TIMER will be set asserted (set TRUE).  Please note that contact
   transitions for 0..1 at the start of the ladder logic scan and NOT at the rung of code that controls the TIMER. THAT"S RIGHT!!

The most important points to take away from this long winded guess on TIMER behavior is that a TIMER's contact is set TRUE (1) at the beginning of the scan and the contact is set FALSE (0) at the ladder logic rung that controls the TIMER is evaluated.   THAT's RIGHT!!

« Last Edit: September 19, 2017, 07:49:46 PM by support »
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:TIMER Contact Behavior
« Reply #3 on: September 19, 2017, 12:14:28 PM »
Thanks so much for adding the corrected info to the discussion.

I had forgotten about the ”-1” value for the TIMER PV. Thanks for reminding me.

Gary D*ckinson