I have found that some programmers don't understand the essence of SET and RESET. I believe this is because they don't understand the true nature of computer memory.
When you use SET and RESET it is EXACTLY like using the light switch in your kitchen.
If your wife tells you to turn on the kitchen light... of course, you jump up off your arse and do so! Then you go back to drinkin' your beer. As you sit there, tilting your head back shooting another big swallow, it suddenly occurs to you... you notice that you don't have to stay at the switch keeping the light on... it just stays on... and you keep drinkin' your beer.
Then, mid-gulp, you get one of those MGD-Moments... Gee, What if...
You run out to the garage and turn off the breaker to the kitchen lighting circuit. Immediately, of course, you hear your wife yell out, HEY!
You turn the breaker back on and listen for a moment... hmmm... no more yellin'.
So, you go back into the kitchen and see that the light is on. You ask your wife, What happened? She says, "The light! It went off and came back on... all by itself!"
Hmm..., you say, Imagine that! Oh... are you OK?
(That, "... are you OK?" is a standard, self-preservation measure used by many of the males in the species.)
Back to Planet-PLC...
A Memory-Bit in a PLC is like a wall switch. A Memory-Word is like a bank of wall switches.
There is a slight difference between PLCs and the wall switch example... wait for it.
A wall switch will respond to, and maintain, the last action applied. If the switch is off when the circuit breaker is opened then the switch will still be off when power is restored (assuming no one is messing with the switches). The same is true if the switch was off when power was killed.
Except for a slight difference in the analogy, memory bits act the same way.
A bit in PLC Memory will go ON if it is told to go ON. It will then stay ON until it is explicitly told to go OFF. This is just like a wall switch. Flip it on and it stays on. It will stay on until you flip it off. These are explicit actions... turn it on, or turn it off.
Now comes the slight difference in the example...
In the wall switch example, the power was killed to the wall switch (wall switch = memory-bit). The power being killed to the wall switch is synonymous to turning off the PLC.
Now... pay attention here... slight difference continued...
Turning off the PLC does NOT turn off the power to the memory bits (possible exceptions to be described). There is a battery in the PLC whose sole purpose is to keep those bits energized when the PLC is off. This is why the program is still there when you turn the PLC back on! Turning the PLC off does NOT make the PLC stupid... that only makes it inoperative.
Many PLCs have what are called Retentive Bits and Non-Retentive Bits.
In the case of the Retentive Bits, when the PLC is off, the battery power is always available (until such time that it craps out) to maintain the states of the bits.
In the case of the Non-Retentive Bits... this one I wonder about.
Let me describe something else first.
In a normal PLC rung, a certain combination of conditions is necessary to turn on the particular output.
Now... here is a subtle, but hugely important difference in the way that PC's and PLC's manage memory.
In a typical PC program, if a bunch of code causes a bit to be turned on, then the bit will go on and stay on until some other code turns it off.
In a typical PLC program, the output is handled a little bit differently.
If the bunch of code causes the bit to turn on, then fine... the bit turns on. The next time the particular bunch of code is examined, if the necessary combination does NOT exist, then the bit is told to turn off!
Can you get the drift of this?
In PLCs, for any given output bit, if there is NOT cause to turn on and keep on the bit, then the bit is turned off!
In the wall switch example, this would mean that the switch is replaced by a momentary pushbutton and that you had to continuously hold that pushbutton, so as to provide "cause", to provide light so that your wife could eat her dinner. (Hopefully, she will have enough patience to do the same for you. You, are only looking forward to Hamburger Helper with a beer while she, of course, has a seven-course what-cha-ma-call-it, with a full bottle of wine.)
Now, with respect to the Non-Retentive Memory Bits...
In some cases, those particular bits are simply NOT wired to the battery back-up. In other cases, where Retentive and Non-Retentive can be declared, those are obviously connected to the battery. In that case, it is then a "house-keeping" issue on the part of the processor.
That is, in the first scan, after power-on, those bits that are declared as Non-Retentive are turned off before the processor begins to run code.
So, I'm having one of those wonderful MGD-Moments now... I wonder if I left any threads hanging loose.
Come back and hit me if I did. I'll try to square it up.