# Scan Sequence



## Navyguy (Mar 15, 2010)

I just want to confirm the Scan Sequence in the picture below...

I believe it is (rung 0000) AUTO, HAND, PB1, PL1, PB2, but have been told it is AUTO, PB1, PL1, HAND, PB2.

Sequence is obviously left to right, but if I recall the scan sequence scans the branch outside the first instruction before the branch(es) on the second instruction. So if there were multiple instructions beside (Rung 0000) HAND, they would be scanned before PB1 and PL1 on the same rung; is that correct?

Cheers
John


----------



## dronai (Apr 11, 2011)

Looks like Hand is the manual on, and bypass Auto. Left to right, top to bottom on the scan

Yes on your question 
I just read that Allen Bradley is done this way, but Modcon PLC's use a vertical by column method, so it depends on what you're are using.


----------



## gpop (May 14, 2018)

The instruction is basically broken down into a C command so AUTO, PB1, PL1, HAND, PB2 would be the way it was scanned. 

Its not being scanned one bit at a time so it really doesn't matter 

IF ((Auto and (PB1 or PL1)) or Hand) and PB2 then PL1; end_if;

I guess that would be the scan order (i haven't written code for a long while so its probably totally wrong) but as its all processed as one instruction (e.g you can not change anything until its finished the instruction) i can not see why the order makes much difference.

Forgot to add that once compiled the order may change as it may look for the fastest exit point first. checking to see if PB2 was false first would give it a chance to exit before having to work on the rest of the line making it micro seconds faster so there's no telling in what order it really happened


----------



## Navyguy (Mar 15, 2010)

I believe it is being scanned one bit at a time, just several hundred times per second...

In a simple ladder like this, you are right it really does not matter I suspect, but in a more complicated ladder, scan order is extremely important. If you are able to stop time, you can see there are moments when some instructions will be TRUE and some will be False based on the same OTE...

As an example in the picture above, when the PLC scans PL1 the very first time it will be FALSE because of the scan order... the OTE Green Pilotlight does not become TRUE until the PLC scans the Red Pushbutton. It is not until the next scan does the XIC Green Pilotlight (Sealing In Rung) become TRUE. This happens so fast is looks instantaneous, but it is not, it actually takes two scan cycles.

So my question is when is the HAND XIC scanned compared to the Start PB and the sealing in Pilotlight XICs...

Cheers
John


----------



## splatz (May 23, 2015)

Navyguy said:


> I believe it is being scanned one bit at a time, just several hundred times per second...
> 
> In a simple ladder like this, you are right it really does not matter I suspect, but in a more complicated ladder, scan order is extremely important. If you are able to stop time, you can see there are moments when some instructions will be TRUE and some will be False based on the same OTE...
> 
> ...




I think when the PBL (pushbutton light) is scanned on the first scan on startup, it's value is whatever the physical switch position indicates. 



Inputs are scanned first, each iteration of the scan. They are not updated until later. 



Each iteration of the scan: read inputs, then evaluate / execute logic, then communications, then finally update outputs. Rinse, lather, repeat.


----------



## Navyguy (Mar 15, 2010)

splatz said:


> I think when the PBL (pushbutton light) is scanned on the first scan on startup, it's value is whatever the physical switch position indicates.
> 
> Inputs are scanned first, each iteration of the scan. They are not updated until later.
> 
> Each iteration of the scan: read inputs, then evaluate / execute logic, then communications, then finally update outputs. Rinse, lather, repeat.


I agree with this, but it is not what I am explaining / asking; apparently not very clear either!

I know how the scan works in general and is as follows:

Step 1 - Scan real-word inputs to check state;
Step 2 - Scan logic against real-word input and determine if OTE is true or false and change output bit to true or false; and
Step 3 - Scan OTE output bit to change real-world output to true or false.

Generally the scan is horizontal, but my question is when does the "step 2 scan" scan the branches in this scenario? Or in other words, what is the order of the instructions when rungs are involved before telling the OTE it is true or false?

I believe it is in the order in the picture below with the Red Numbers indicated the scan order.

Cheers
John


----------



## splatz (May 23, 2015)

I think there is no "order of operations" with respect to reading the inuputs. You're just initialzing the values for the logic that follows. 



Within the rung, really the branching is directly related to grouping symbols in arithmetic or algebra. You evaluate the expressions in the grouping symbols first, from the inside out. Look at the way @*gpop* wrote it out, 





gpop said:


> ((Auto and (PB1 or PL1)) or Hand) and PB2





the parentheses are directly equivalent to the branch points. 

It's kind of elegant how it works ...


----------



## gpop (May 14, 2018)

This might be to far down the rabbit hole.

there are 2 types of memory 

assigned which is a position in memory that hold info that is non destructive (this is where the plc stores the data in your tag which is why you can choose the size of the storage area. In your case its all bools)

stack memory is a temp memory slot assigned by the plc and is destructive meaning it can be deleted by writing over it. 

PL1 =((Auto and (PB1 or PL1)) or Hand) and PB2

notice that this statement only changes the state of PL1 if true its a 1 if false its a 0

go to memory PB1 get whats in there......its a 1
go to memory PL1 get whats in there......its a 0 (interesting memory as its also the same space used for the output assignment)

PB1 or PL1 = 1 write to stack x (x is a storage place in plc memory)

go to memory AUTO get whats there.......its a 1

x or auto = 1 write to stack y (delete x as we are done with it)

go to memory Hand get whats in there .... its a 0

y or hand = 1 write to stack z (delete y)

go to memory PB2 get whats in there .... its a 1

z and PB2 = 1 write to assigned memory address PL1 (delete z)

Now heres the twist PL1 memory position is used in 2 places one time it was read the next time it was written to. It was read before it written to. The plc does not go back to check if the rung changed it simply moved a 1 into a memory position the executed the next rung


So back to the beginning it doesn't matter what order a rung is executed especially in plc5 as you can not stack multiple inputs and outputs on the same rung (input - output - input - output)


----------



## sparkiez (Aug 1, 2015)

It is being scanned in the following order:


AUTO, HAND
PB1,PL1
PB2


The scan cycle goes from left to right, top to bottom. Each time it his a "connection point", it looks for a vertical branch, evaluates that branch to the common connection point, then does the same until it reaches no connection points in that vertical branch. It then moves forward.



The scan sequence for logix5k processors is an asynchronous cycle, and you don't see to much about it. Basically, physical outputs are turned on as soon as the rung is evaluated. Physical IO is handled somewhat differently than internal memory. For this reason, on these controllers, I prefer to map all of the physical IO to memory. In that way, I can have a synchronous scan cycle, which I'm used to working with, and is easier for me to visualize.


----------



## gpop (May 14, 2018)

basic programming

Do not use a OTE on more than one rung 
A rung that is false is the same as saying turn OTE off it doesn't ignore the rung

On plc5 which updates at the end of the scan (5000 updates I/O all the time even while scanning the rung) If you turn it on in rung 3 and turn it off in rung 50 the last rung executed wins (in this case it will never come on but ladder may show it on in rung 3)

Always be careful moving rungs of data up or down to look nice or to make it easier to trouble shoot as changing the order can cause lots of problems

A simple learning exercise to understand the way code is scanned is to play with a one_shot and a counter (even writing a one_shot is ladder rather than using the instruction). If you get it correct the counter will only count by one when you toggle a bit to on then stay there until toggle off and back on.


----------



## Rora (Jan 31, 2017)

Just want to clarify one thing that will make this a lot simpler--when they say top to bottom, they mean rung by rung. Any given rung will be evaluated left to right, including 'or-d' instructions, because it is necessary to evaluate an 'or' before you have an input for the next instruction (to the right) in the rung.

The general PLC scan sequence is as follows:

1) Read inputs (hardware) to the I/O buffer (software)
2) Evaluate logic (software) with the fresh hardware input values from step 1, this is done according to method described in the paragraph above. Outputs are updated in the I/O buffer as the program executes.
3) Use the I/O buffer (software, updated by the logic in step 2, to write to the outputs (hardware)

There are exceptions, some PLCs update I/O within some semblance of real time instead of once per program scan, but for the most part this should be enough to predict exactly how the code will execute.

For instance, a rung may write a value of 0.5 or 50% to an analog output in one rung. If a later rung also writes to the same analog output, the final state of the I/O buffer is the outcome of the last rung which wrote to it, so this would overwrite the 50% generated by the first rung. The value generated by the latter wrung will be written to the hardware outputs.


----------



## paulengr (Oct 8, 2017)

That’s an AB PLC. The ladder logic is actually text. Double click on the rung number and it brings up a text box for you to edit. That is actually how the PLC views it. Once you know the 3 letter mnemonics for everything you can type code faster than doing the graphics and mouse thing. Those of us that were doing this back in the 80s and 90s still do it that way.

The comment about C is incorrect. Micrologix processors, really any processor using Logic 5 or 50” is interpreted, not compiled, the 5 used custom ASICs. The 500 series was originally a Motorola 68000 but I’m sure now they use something else. Only Logic 5000 is compiled. The 800 series is actually a licensed version of another system, Codesys, not “AB native”. Not sure if it’s compiled or interpreted.

Not just Modicon but Square D and Koyo PLCs also have a column based approach at least in the editor. Not sure how it runs internally.


----------



## splatz (May 23, 2015)

gpop said:


> This might be to far down the rabbit hole.


Programs that evaluate arithmetic expressions use the stack data structure to handle order of operations and grouping symbols. This is why 

3 + 3 * 3 = 3 + (3 * 3) = 12 

not 

3 + 3 * 3 = 18 


This is exactly the same, the stack is used so that branches and subbranches are evaluated from the inside out.


----------



## gpop (May 14, 2018)

splatz said:


> Programs that evaluate arithmetic expressions use the stack data structure to handle order of operations and grouping symbols. This is why
> 
> 3 + 3 * 3 = 3 + (3 * 3) = 12
> 
> ...


Some people understand plc maths. Me i just add lots of ((( ))) and force it to do it my way....lol


----------



## bill39 (Sep 4, 2009)

Post this on PLCTALK.net
It seems to be the best PLC forum.


----------



## sparkiez (Aug 1, 2015)

I add parenthesis so that I don't have to focus on order of operations. Besides, the OOO can get a bit screwy when doing it from a computational perspective.


----------

