Hide Transcript
View Transcript
Here’s a Main program with just a single
rung of ladder code. Let’s create a task, Call it Shift Report,
and let’s tell it to NEVER yield. In that Task, we’ll create a For Loop that
increments D0 from 1 to whatever is in D100. Normally we would log data or something like
that inside this loop, but that’s a subject for a different video. So for our example we’ll just use a math
instruction with some trig functions just to kill time. Back in Main, we’ll enable the task and
make it edge triggered so it only runs once, which is usually what you would do, because
normally you would trigger an end of shift report on some event or during a certain time
each day. But for this demo, we’ll add a control bit
so we can force it whenever we want to. Great, so we have a main scan loop that has
one rung of ladder code and one rung that enables our task when C0 is set. Accept that. Write it out to the PLC. Put the PLC in run mode. Let’s create a dataview with our control
bit, the loop counter and the value the loop counts to. Let’s make that 250 loop iterations. Let’s also monitor the actual scan time…
the average scan time… and the max scan time. These numbers are all in microseconds. Looks like it takes on average this many microseconds
to execute the program without the task enabled. If we enable the task - holy cow – during
the one scan that ran the task it maxed our scan time out at 40 times the average scan
time! Why is that? Well, because we told it to NEVER yield. On every scan we’re executing this entire
loop. I we increase the number of loops, the scan
time goes up. Again, we are running through the entire loop
on each scan – that is, we are never yielding. Let’s re-configure the task to Always Yield,
and write that back out to the PLC … Let’s clear that max scan time number. With the Task disabled we get this scan time,
and when we enable the task it only bumps up the scan time just a little bit. Why? Because since we setup the Task to Always
Yield, so the PLC does one pass through the loop and exits the task. On the next scan it does another pass through
the loop. Next Scan, another pass. Etc. So we are only executing one iteration of
the loop per scan. What if we change the loop count to 1000? Look, no change in the max scan time! Why? Well, since we told the task to only do one
pass of the loop on each scan it doesn’t matter how many passes through the loop we
need! Let’s take a look at the last Yielding option. Let’s tell the task it can take up to 1000
microseconds but then it has to yield. Write it out to the PLC. With the task enabled; our scan time maxes
out at a little over 1000 microseconds. Exactly what we expect. We call this time we allotted to the task
a time slice. And look at this. You can change the time slice while the task
or program is running. It’s just one of the structure members of
the program or the task. If I change the time slice, and re-execute
the task, then sure enough, the scan time changes accordingly. Can you use Yielding with a Program? Sure – look it’s the exact same thing. Just like a Task, whenever a Program hits
a Yielding instruction, it reacts however you setup Yielding for that Program. So, once again Do-more gives YOU total control
over how your PLC operates. That’s so cool. One Caution when using NEVER Yield: There
is a Watchdog in the PLC. If your code takes too long to execute, then
it’s going to assume something is wrong and it’s going to reset your PLC. Or, it may shutdown depending on how you have
your watchdog setup. We’ll cover the Watchdog in another video. Just be careful your code doesn’t exceed
that watchdog time limit when using never yield or when setting up time slices. That ought to give you a pretty good feel
for how all of this fits together. The main things to remember are: Tasks are great for executing things that
only require a one pass through the code – even it takes multiple scans. Programs can do everything tasks can do but
they can also do anything that might require multiple passes through the code where you
have to keep coming back to check on something like Stage or Asynchronous Communications. And finally, Yielding is an awesome way to
reduce your scan time. If you need any help with Do-more Tasks and
Programs, please contact AutomationDirect’s free, award winning support team during regular
business hours. They will be happy to help. And don’t forget the forums! There are lots of automation professionals
there that love to share their years of experience. Just don’t post any questions directed at
AutomationDirect’s support team there, they don’t monitor the forums on a regular basis.