View Full Version : infinite 1minute timer for Siemens S7-300 PLC

June 19th, 2002, 04:55 PM
Anyone know how to write a looping 1 minute timer routine? where it counts to one minute and reset and counts to 1 minutes over and over again?


Terry Woods
June 19th, 2002, 05:23 PM
I think almost all of us do... What have you tried?

June 19th, 2002, 05:34 PM
I give this a shot. Look at pages 52-53 of manual.

Let Q set an internal bit and replace I0.2 for the reset with an XIO representing the internal bit for Q To make it be repetitive the EN input would need to be TRUE/ON all the time. This will allow your timer to run for 1 minute (if PV is set to 1 minute) than reset because Q only goes to 1 (true) when ET is equal to or greater than PV.

June 19th, 2002, 11:32 PM
for beejus sake guys,,,say something, even if its another rsdoran is an idiot thing. I havent (but will) tested my logic but some of you can read and simulate in your head.

I will gladly accept explanations and insults if my logic is wrong.

June 20th, 2002, 02:11 AM
RSdoran - unfortunately your link is not correct all that appears is 'page cannot be found'. Can I call you an Idiot now! (joking Phil!)

As I have the manuals, I had a quick look at the timer you refer to, if I was any good at this I would have posted a diagram of what was in the manual. Anyway, I think that what you suggested would work, if I can wangle a spare S7 314 out of our stores I will give it a test and post back to let you know the results

Eric Nelson
June 20th, 2002, 03:18 AM
Those loooooong URLs don't work well. It seems to work better when you use the <input type="button" value="http://" title="Insert Hyperlink"> button in the posting editor.

Here's a working link to Ron's file: -->CLICK HERE (http://www4.ad.siemens.de/csinfo/livelink.exe/fetch/1214904/S7SFC2_e.pdf?func=doc.Fetch&nodeid=1214906)<--

I quickly read through it, but does Siemens ONLY have retentative timers? (Forgive my ignorance... I haven't played with Siemen's stuff since the S5) If so, I can see why poor lostpatrol is lost! Can't he just do something simple like this?....

TIM X +--------+
----]/[----------| TIM X |
| |
| 1 MIN |

This is very generic.... Add your own "brand specific" flavoring... :D


Steve Bailey
June 20th, 2002, 06:33 AM
Eric's method is the easiest way to create a self-resetting timer, but be careful how you apply it.

If your need is to make something happen approximately once per minute or to keep track of running time to signal somebody to come and do PM, it's an excellent tool.

It should not be used to roll your own time-of-day clock.

There are two cumulative sources of error in the self-resetting timer.

The first is the fact that for one PLC scan, the timer is held in a reset condition. Those few milliseconds are lost, but they are cumulative. If your PLC scan is 10 milliseconds, it adds up to six tenths of a second per hour when you use a one minute time period.

The second is a little more subtle. PLC timers store their acculmulated time internally. They turn on their 'Done' bit when the accumulated time is greater than or equal to the preset time. When you reset a timer, you drive the accumulated value to zero. Consider this case. During a PLC scan, the timer is evaluated and the accumulated time is 59.999 seconds, so the 'Done' bit remains false. In the next scan, 10 milliseconds later, the accumulated value is 60.009 seconds so the 'Done' bit gets set. In the next scan, when the timer gets reset, you lose those nine milliseconds.

In addition to all of the above, the internal timing functions in the PLC are not as accurate as the timer chips used to maintain a real-time clock.

June 20th, 2002, 09:49 AM
Got it working..thanks all

Terry Woods
June 20th, 2002, 06:51 PM

Yes, Siemens does have Non-Retentive Timers... at least, in the S7-2XX series.

With respect to your solution, Siemens reccommends against it. The Figure below is from their S7-2XX manual.


How about this though... (Sorta, Kinda Pseudo-code)

Run a 1-mSec Timer.
Every scan check the value in the Timer Accumulator.
If the value is Greater Than or Equal to 1000 then, flip the 1-minute bit.
Then, reset the Timer to the difference.

If the scan sees that the accumulator is 1009, then flip the bit and subtract 1000 from the current accumulator value and write the difference back into the timer accumulator.

At that point, the bit has been flipped and the current value = 0.009-Sec.

Then, if something is supposed to happen on a 1-Sec basis, it becomes a matter of watching for the bit-transition; either OFF-to-ON or ON-to-OFF.

Another method that will help to minimize the "in-house" error is to run the timer for the maximun time but reset at 32.000 seconds. Watch for every case of the timer exceeding XX.000. Then you will only have an accumulating error occurring every 32 seconds (or so). Again, resetting the accumulator to the difference.

If it ain't that critical then use a 10-mSec Timer and only have an accumulating error occurring every 320-seconds (5.3 minutes). Worst case: 1-Scan Time.

If it's even less critical than that, then use a 100-mSec Timer and only have an accumulating error occurring every 3200-seconds (53.3 minutes). Worst case: 1-Scan Time.

He didn't ask for just a single line of code.

Allen Nelson
June 20th, 2002, 08:49 PM
Originally posted by Terry Woods


Yes, Siemens does have Non-Retentive Timers... at least, in the S7-2XX series.

Who, me? I think you mean Eric. (I know, all us Nelsons look alike :p )

If you follow Ron's link, you'll see the concern - it's not the Ladder Timer, but the Function Block Timer:

+--- TONR ---+
---|EN ENO|---
| |
---|TMR_EN Q|---
| |
---|RESET |
| |
---|PV |
---|DELTA_T |
---|ET |

which might be a little daunting if you don't know FBD. (Although just as I have trouble telling the difference between IL and STL, I don't see the big difference between LD and FBD. Except for how the data gets into the block (with LD it's by registers internal to the block; with FBD the registers are external (and sometimes it's more graphical)) and except for the EN/ENO thing, they're pretty much the same. Really, isn't a ladder PID block just a FBD, with encapsulated code (that we don't get to see)?

But when you think about it, lostpatrol's requirement was "When the timer is done, it needs to reset itself", and since you know that Q is the done, and RESET is the reset, the <del>ladder</del> code kinda writes itself.

But then, I don't know the first thing about Siemens PLC. Been achin' to, but so far, no customer has spec'ed them (on any project that I've gotten close to, anyway). It's all been Blue Octagon for years.

Terry, one way I might approach what you're trying to accomplish in an AB PLC would be to have a 0.01 sec timer going to the max (32000 csec), but have a rung that checks the ACC for greater than 10000 csec. If it is, then subtract 10000 from the ACC and increment a counter. The timer will never get to ACC=PRE, thus never be done. The timer ACC will always contain it's scan error, so that effect will be minimized. And since the timer is never reset, you don't lose that one scan time.

But I can't tell if you can manipulate the timers registers like that in Seimens.

June 20th, 2002, 09:24 PM
Allen my link was bad but Eric fixed that. Anyway I have in 12 machines 2 S7-300 for each in a Master/slave configuration. I am not adept at them at all but YES they can manipulate registers....the scenario for their ladder is different but I cant see any functions in AB that the S7-300 cant match...that excludes the 5000 line because I havent used it. If I was going to match the S7-300 with an AB product it would probably be the 5000 because I think it exceeds the SLC and PLC5. ITs just looks so different to me that its hard to get use too. That and the fact the machines were made in Germany so I spend more time translating the documentation than actually working with the plc's.

If I can ever get the company to send me to Johnson City for training (more training) on the S7-300 then it may become my plc of choice. So far their support has been great, there is a local rep in our area and we had a good relationship with local distributor. Alas that is going to change, the local distributor took over the Square D (Scheider Group) line.

I agree with your assesment though, they state it uses ladder but I think it is more of FBD. Whatever it is I like it and would like to learn more of it. What is good about that is I have Simatic S7 and spare S7-300's. I should have done what you did, show the diagram for the TONR along with the explanation.

June 25th, 2002, 11:50 AM
One of the things that sets the Siemens product apart from others is the ability to write your own functions or function blocks. In the case of timers the stock blocks are ok but you can use the system clock byte in the 3XX cpu's to make the timing more accurate and write your own logic for timming. Like set the clock byte to mb100 in the hardware config. Then use M100.5 as a 1 second pulse. The following STL becomes a X second timer. This example assumes the Clk input is one scan long so you have to put M100.5 through a one shot. You cound write a one shot into the FC if you wanted to. Once written you can use the function over and over. Our group keeps a bunch of "stock" functions around.

In: In (enable count bit)
In: Clk (one second pulse)
In: count (number of seconds to count)
In/out: Accu (count accumulator)
In/out: Out (finished pulse)

An In //If there is no input
Jc Clr //do clear...else

An Clk //If there is no second pulse
BEC //end here....else
L +1 //get a one
L accu //get the count
+I //add it up
T accu //save as new count
L count //get the end count
< I //check for done
BEC //if not done end...else
An Out //if out is off
S Out //turn it on
beu //and end

Clr: A Out //if out is on
R Out //turn it off
L +0 //get a zero
T accu //clear the accumulator

Now there are as many different ways to do this as there are folks writing code so be creative. With S5 controllers and sometimes in S7 I have done this sort of thing in the interupt OB's to get real accurate events. This is great for run hour meters and the like.

Ya gotta love that statement list stuff.