Ron and George have answered for me already, but yes, it works differently to the way you thought.
A quick tip for you as you delve more and more into Rockwell PLC's. These few "rules" have helped me understand and write code better, if you remember these and think about the code in these terms, you might find it becoming more intuitive:
1. Forget "on" and "off"; instead think about things in terms of "rung in condition" and "rung out condition"
2. Every single instruction on every single rung is executed exactly once per scan, from left to right and top to bottom, no matter what is happening on the rung before it*
3. PLC's never look backwards**
*There are exceptions to this rule of course: if the subroutine is not being called, or if JMP/LBL (jump and label) instructions or an MCR instruction are involved. These programming methods should (in general) only ever be used as a weapon of last resort, and very sparingly. So, for what I'd consider "well written" code, rule 2 is correct enough for most applications
**Again, there are exceptions, like JMP/LBL, but again, as a general rule...
Read on for a little more detail on each of my "rules" and why they're helpful (to me, at least)
1. Forget "on" and "off"; instead think about things in terms of "rung in condition" and "rung out condition"
Imagine a rung of ladder logic with a range of XIC, XIO, TON, MOV, OTE, OTL, OTU, or any other instructions on it. Each instruction on the rung has two different ways of operating; one way when it is scanned with the rung-in condition TRUE, and once when it is scanned with the rung-in condition FALSE. In addition, each instruction also sets a
rung-out condition, which may be based on the rung-in condition, the status of any tag associated with that instruction, internal instruction parameters, or a combination of several of those things.
For example, an XIC instruction operates as follows:
- With rung-in condition TRUE: if associated tag is also true, set rung-out condition to TRUE; if associated tag is FALSE, set rung-out condition to FALSE
- With rung-in condition FALSE: set rung-out condition to FALSE, regardless of the state of the associated tag
- Then, the rung-out condition of that instruction becomes the rung-in condition of the next instruction
Now consider the OTE and OTL instructions:
- An
OTE scanned when the rung-in condition is
TRUE will set a bit to 1, and set the rung-out condition to
TRUE
- An
OTE scanned when the rung-in condition is
FALSE will set a bit to 0, and set the rung-out condition to
FALSE
- An
OTL scanned when the rung-in condition is
TRUE will set a bit to 1, and set the rung-out condition to
TRUE
- An
OTL scanned when the rung-in condition is
FALSE will do nothing to the bit, and set the rung-out condition to
FALSE
If you look at the help files for each instruction, you'll see that it gives detailed descriptions of how each and every instruction operates with both a true rung-in condition, and a false rung-in condition. It will also tell you how the rung-out condition behaves. As a general rule, most (if not all)
output instructions (any instruction that acts on a tag - OTE's, MOV's, math instructions, timers...) have a very straightforward method: rung-out condition = rung-in condition. They just pass the rung status straight through to the next instruction, which is why it is possible to put multiple output instructions in series.
2. Every single instruction on every single rung is executed exactly once per scan, from left to right and top to bottom, no matter what is happening on the rung before it.
Each and every instruction on a rung is scanned and executed, even if you put an AFI right at the start of the rung. When you think about it, it has to be this way, because when you "break the circuit" to a timer, the timer still has to do certain things (like turning off the .DN bit, and resetting the accumulated time to zero). As per rule one, think about it as "rung-in condition" and "rung out condition". No matter what happens earlier in the rung, each instruction is scanned and will perform its specified action - that action, as we've seen, is dependent on the rung-in condition to the instruction. Remember: putting an "always off" at the start of the rung
does not disable the rung. It merely ensures that every instruction on the rung is scanned and executed with a false rung-in condition. OTE's will still turn off bits. Timers will still turn off .DN bits. And PLC's will still crash if you feed a MOV instruction with an out-of-range array pointer while you're overseas in a third world country trying to troubleshoot remotely with terrible internet access (three guesses as to how I learned
this lesson).
This rule is also relevant when considering its exceptions: if you scan an OTE with a true rung-in condition, and then stop scanning the rung altogether (e.g. stop calling the subroutine), the bit associated with that OTE
will not turn off. It doesn't turn off just because it's not being "powered" any more; the bit is only ever turned off by an instruction commanding it to do so - which could be an OTE scanned with a false rung-in condition, or an OTU scanned with a true rung-in condition. If the bit is an output controlling a pump, and you stop calling its subroutine, that pump will never stop. This is one of may reasons that conditionally calling subroutines is (in general) very bad programming practice.
3. PLC's never look backwards
Consider the following rung:
Code:
| Bit_1 Bit_1 Bit_2 |
|-----| |----------------( U )-----( )-----|
|
To look at it simplistically, you might say "bit 2 will never turn on, because as soon as bit 1 comes on it turns itself back off". But remember: PLC's never look backward. Again, read the rung left to right with a rung-in and rung-out condition mindset. To start with, let's assume Bit 1 is false.
1. XIC is scanned with rung-in condition TRUE (because it's connected to the main "power rail"). Associated bit is FALSE, so rung-out condition is set to FALSE
2. OTU is scanned with rung-in condition FALSE. It therefore takes no action with regard to Bit 1, and sets rung-out condition to FALSE
3. OTE is scanned with rung-in condition FALSE. It therefore sets Bit 2 to zero/false, and sets rung-out condition to FALSE
So, at the end of the rung, the only action we have taken is to set Bit_2 to FALSE.
Now lets say that at some point, a HMI sends a signal saying "set bit 1 to TRUE". Let's see how the rung executes this time:
1. XIC is scanned with rung-in condition TRUE (because it's connected to the main "power rail"). Associated bit is TRUE, so rung-out condition is set to TRUE
2. OTU is scanned with rung-in condition TRUE. It therefore sets Bit 1 to FALSE, and sets rung-out condition to TRUE. Remember, the PLC doesn't at this point "look back", and realise that the start of the rung is now false - it just continues on, left to right, as always.
3. OTE is scanned with rung-in condition TRUE. It therefore sets Bit 2 to one/true, and sets rung-out condition to TRUE
So, at the end of the rung, we have taken two actions: we've set Bit_1 to FALSE and Bit_2 to TRUE.
Now, let's execute the rung again:
1. XIC is scanned with rung-in condition TRUE (because it's connected to the main "power rail"). Associated bit is FALSE, so rung-out condition is set to FALSE
2. OTU is scanned with rung-in condition FALSE. It therefore takes no action with regard to Bit 1, and sets rung-out condition to FALSE
3. OTE is scanned with rung-in condition FALSE. It therefore sets Bit 2 to zero/false, and sets rung-out condition to FALSE
So, at the end of the rung, the only action we have taken is to set Bit_2 to FALSE.
At the end of all this, what we've effectively done is created a one-shot: whenever Bit_1 gets set to TRUE, Bit_2 is set to TRUE for a single scan. This technique is very commonly used when programming HMI pushbuttons, but it only makes sense if you remember that a PLC thinks like Edna Mode:
Edna Mode said:
I never look back, darling. It distracts from the now
I hope something in there was useful for you!