
Lesson 7
Shift Registers in Ladder Logic: Tracking Parts on a Conveyor (SHL, BSL, BSR)
A tag read as a row of places, stepping with the belt instead of the clock, shift and rotate, reading the register at a station, and chasing lights.
From Usta's field manual in Meridian Works, a story-driven PLC course: chapter 7, “Shift Registers”. Reading a tag as a row of places, and moving it along with the belt.
Not a quantity, a row of places
The camera is eight stations behind the pusher.
That is the whole problem of this room. When the camera calls a jar cracked, that jar is nowhere near the pusher. It is eight pockets back. By the time it reaches the pusher, the camera has looked at eight more jars.
Wire the verdict straight to the pusher and it will throw out a jar, but the wrong one. The line still looks as if it works. A pusher that moves looks like a pusher that works.
The line has to remember. Not "was there a fault?"; you did that with a seal-in in the Gatehouse. It has to remember which jar. And it has to keep remembering while that jar walks to the end of the belt.
Sixteen bits, two readings
Until now you have read a sixteen-bit tag as a number. You compared it, added to it, turned it into degrees.
Now read the same sixteen bits as a row of places. Each bit is a separate yes-or-no answer. Bit 0 is one station, bit 1 is the next station. On this line each bit answers one question: is the jar at this station a reject?
Nothing changes in the processor. There is no separate shift-register data type. What changes is the question you ask of the tag.
| The tag holds | As a number | As a row of places |
|---|---|---|
0000 0000 0000 0001 |
1 | a reject at station 0 |
0000 0001 0000 0000 |
256 | a reject at station 8 |
0000 0001 0000 0001 |
257 | rejects at 0 and 8 |
0000 0010 0000 0000 |
512 | the same jar, one step later |
Look at the last two rows. As numbers, 512 is twice 256. As a row of places, the same jar has moved one station. Both are true. For this job the second one is the useful one.
That is why arithmetic on a tracking register is nearly always a mistake. Adding 1 to Track does not move the belt. It sets station 0. If station 0 was already set, the carry goes to station 1 and station 0 is cleared. On the machine that means a jar's record jumping one pocket forward.
The type of the tag is a note too. On Siemens, INT says "this is a number" and WORD says "this is a bit pattern". Logix has no WORD type. A tracking register there is usually a DINT or a DINT array. Then write it in the tag's description.
There are three registers in this room. The first carries the camera's verdict to the pusher. The second is not a belt at all; it is the eight lamps over the gatehouse. The third has two registers on one belt, and they have to agree.
A tag does not know whether it is a number. Only you know that.
Tag, byte and bit
Before you move anything, know what you are moving. On this page the two platforms behave differently.
On Siemens a word and its bytes are one memory. A WORD takes two bytes. If Track is at MW10, it covers bytes 10 and 11. The Siemens processor is big-endian: the high half of the word comes first. M10 carries bits 15 down to 8. M11 carries bits 7 down to 0.
So bit 0 of the word is M11.0. Bit 7 is M11.7. Bit 8, the very next station, is M10.0. One pocket forward on the belt is one byte back in the address.
Here is the sentence that matters: MW10 and M11.0 are the same memory. Nothing is copied between them.
- A coil that sets
M11.0changes the value ofMW10. - An SHL on
MW10moves the bit atM11.0toM11.1. You never touchedM11.1, and it changed. - There is no spare bit in the middle of a tracking register. Park another flag at
M10.3because it looked free, and the next step carries it to the pusher as a cracked jar.
Logix has no byte addresses. The bits of a DINT tag are read by name: Track.0, Track.1 … Track.31. Bit 8 is Track.8. The byte-order trap does not exist there. The shift instruction BSL, however, wants a DINT array. Then the bits are named Track[0].0, Track[0].1, and station 32 is Track[1].0.
This simulator makes one simplification here. Here BSL and BSR do not take an array. They take a single integer tag. A BYTE tag gives at most 8 stations, a WORD or INT 16, and a DINT 32. The registers in this room are WORDs on Siemens. The Logix view shows them as INT. Either way there are 16 stations.
On both platforms the rule is the same: reserve the whole tracking register and write it on the drawing. Those bits are not free space to borrow. Each bit means something only because of where the others are.
The work orders in this room give every station they need a name: At_Inspect, At_Divert, Crack_At_Gate. Nobody has to do byte arithmetic with the line stopped. Still, look at the symbol table once. In the S7 view At_Divert is at M10.0. Check that against the drawing above.
One more trap, and it has nothing to do with the processor. A word is written with bit 15 on the left, because that is how we write numbers. A lamp row on a panel is usually drawn with bit 0 on the left, because that is where the belt comes in. The same register looks mirrored on two screens. Before you decide the register runs backwards, check which one you are looking at.
Two names, one memory. Nobody believes it the first time.
The register itself
A shift register is a scale model of the belt, kept in one tag.
Why the line has to remember
The measurement and the action happen in different places. The camera is at one end of the belt and the pusher is at the other. Nothing you add to the pusher's rung can tell it about a jar it cannot see. The verdict has to travel with the jar.
One bit per station. One shift each time the belt moves a station. The bit and the jar travel together and arrive together.
Three jobs
Every shift register is made of these three jobs:
- Shift. On each belt step, move the whole register one place. Every flag moves one station. The flag at the far end falls out, because that jar has left the line.
- Load. Write the new jar's verdict into the entry station.
- Read. Put a contact on the bit of the station where an actuator stands.
The order matters. Shift first, then load. Do it the other way and the verdict you just wrote moves a station forward on the same step. Every flag then runs one pocket ahead of its jar. On the floor this looks like a pusher that always takes the jar behind the cracked one. Everybody blames the camera first.
On Siemens, load with SET
On Siemens the shift (SHL) and the load are two separate rungs. The load rung is true only on the step, for a single scan. Put a plain coil there and on the next scan the rung is false and the coil writes 0. The flag is wiped at once. A SET coil writes the 1 and then leaves that bit alone. Nothing has to clear it later either. On the next step SHL brings a zero in at the bottom anyway.
On Logix, BSL does both
On Logix this job uses the BSL (Bit Shift Left) instruction. BSL has four parameters: a DINT array, a CONTROL tag, a source bit and a length. When the rung goes from false to true, BSL moves every bit one place and puts the source bit into place 0. So shift and load are one instruction. The bit that falls out goes to the .UL bit of the CONTROL tag. Because BSL shifts only when the rung goes true, it needs no separate edge instruction. BSR does the same job in the other direction.
In this simulator BSL and BSR are on the File/Shift tab. Here the Array field is one integer tag, not an array. Length cannot be more than that tag's bit count. The jobs in this room open on the Logix line. Write the shift with BSL. The CONTROL tag is ready in the table: Track_Ctl for Track. If you switch to the Siemens view there is no BSL. There you put SHL behind a P contact and write the verdict with SET.
How long is the register?
As long as the belt is, in stations. That is a measurement, not a decision. Count the pockets from the camera to the pusher. If the answer is eight, the pusher reads station 8. If the real answer is nine, you throw out sound jars all week and there is no bug in the program to find.
On Siemens a WORD gives sixteen stations and a DWORD thirty-two. If the belt is shorter, the flags do not stop where the belt ends. They keep walking through the empty places and fall out a few steps later. That is harmless as long as you have not given those places to anything else. On Logix you set the BSL length to the number of stations on the belt. After the last station the flag falls into .UL.
Clearing the line
When somebody empties the belt by hand, the belt is empty but the register is not. Every flag left in it reaches the pusher in its turn and throws out a sound jar. So the clear button does two jobs: it empties the line and it clears the register. That is why both belts in this room have a CLEAR on the panel.
The register is a promise: what the program knows about the belt is what is standing on the belt.
Step with the belt, not the clock
Nothing on a belt happens per second. It happens per metre.
Everybody's first shift register is stepped by a timer:
T_Step done -> shift Track by 1 (every 500 ms)
On the day it is built the belt runs at a steady speed and it works. Then the belt slows down. When the palletiser fills up, the line drops to half speed for four minutes. The jars slow down and the register keeps marching. A fitter jogs the belt for ten seconds to clear a jam. The jars move a hand's width and the register moves twenty steps. Somebody presses stop and goes for a tea. The belt stands still and the register keeps walking.
All three are the same fault. The register measures time and the belt measures distance. Once they part, every flag belongs to another jar. No lamp comes on. The program does exactly what it was told. That is why the fault takes two days to find.
Shift on the encoder pulse.
Belt_Step gives one pulse each time the belt moves a station. It may be an encoder output or a sensor counting the flights. What matters is that the pulse comes from the belt itself. If the belt stops, no pulse comes. If the belt slows, the pulses come further apart. The register keeps up without knowing the speed.
The rule is bigger than shift registers: anything that tracks material must be driven by the material.
Exactly once per pulse.
A pulse is not instantaneous. It may last sixty milliseconds. With a ten-millisecond scan that is six scans. Put a plain contact on it and you get six shifts for one station. And the error depends on speed. When the line is slow the pulse is longer and the error is bigger.
The cure is edge detection. On Siemens a P contact is used on Belt_Step. On Logix an ONS goes after the contact. BSL already shifts only when its rung goes true. Whichever you use, the result must be the same: one pulse, one shift, one station.
There is another end to this problem. A pulse must last at least one scan. Inputs are read once per scan. A pulse that starts and ends between two reads is never seen by the program. The faster the line, the bigger this risk. On that day you need a high-speed counter input: a card that catches the pulses in hardware. The symptom is a register that falls behind the belt, and further behind the faster the line runs.
If the belt runs backwards.
A belt jogged in reverse carries the jar back. The encoder pulses in both directions. Left alone, the register shifts forward on the reverse pulses too. The jar goes back one station while its flag goes forward one.
There are two right answers. Choose one on purpose.
- Shift the other way. If the belt is going back, use the reverse shift (SHR on Siemens, BSR on Logix). The register follows the belt exactly. This needs a direction signal you can trust.
- Stop shifting, then clear the line. Tie the shift to the forward signal. The register freezes while the belt runs back. The line is cleared before the belt goes forward again. On many plants this is the right choice. The belt is only reversed by a fitter clearing a jam by hand, and he will clear the line anyway.
Ignoring the direction is wrong. That is why Belt_Fwd is on the panel of the first work order.
A stopped belt needs nothing special. No pulse comes and nothing shifts. The pusher keeps reading the same station.
The clock does not move the jars. Let whatever moves the jars drive the register.
Shift and rotate
The only difference between the two instructions is at the end of the word.
SHL and SHR: the end bit falls off.
On Siemens SHL Track, 1 moves every bit one place towards the high end. The bit at 15 is gone and is not kept anywhere. A zero comes in at the bottom to fill the gap. SHR does the same job the other way.
For a belt, losing the end bit is right. A jar past the last station has left the line. A register that refused to forget would need somewhere to keep the whole shift.
Know the edges. Shift a word by 16 places or more and it is empty. Shift it by 0 and nothing happens. A shift moves bits; it does not divide. If you want to divide, use DIV.
ROL and ROR: the end bit comes round.
In a rotate no bit is lost and no new bit comes in. The bit that leaves one end enters at the other. A rotate is a ring. Rotate a word by 16 places and you are back where you started. So rotating by 20 is the same as rotating by 4.
Logix is different. Logix has no SHL, SHR, ROL or ROR instructions. Shifting is done with BSL and BSR, one bit per trigger. There is no separate rotate instruction. The known way is to feed the bit BSL pushes out (.UL) back in as its own source bit. Then the bit that falls off one end comes in at the other.
There is a detail here. In this simulator BSL reads the source bit before the shift. .UL is written at the end of the shift. So the .UL that goes into the source is the bit that fell off on the previous shift. That makes the ring one place longer than Length. With Length 8 the ring has nine places. The ninth place is the .UL bit, and no lamp is wired to it. If you want the ring to be exactly Length, do not feed back .UL. Feed back the bit that is about to fall off. For BSL that is bit Length − 1. For BSR it is bit 0.
In this simulator BSL and BSR are on the File/Shift tab. Array is one integer tag here. The Siemens view has no BSL. There you use SHL, SHR, ROL and ROR.
The width comes from the destination.
The instruction does not know the pattern in your head. It only knows the width of the tag it writes to. A WORD destination is a ring of sixteen places. A DWORD is thirty-two. Rotate an eight-lamp pattern inside a WORD and it travels round sixteen places. For half of every lap it sits in the top byte, where there are no lamps, and the sign is dark for eight beats. The operator will say the program is broken. It is not. The ring is bigger than the sign.
In real TIA Portal, ROL also works on a BYTE and gives a ring of eight places. This simulator shifts in 16 and 32 bits only, so a BYTE destination rotates as a word here. On Logix the length parameter of BSL sets the size of the ring.
The compiler refuses to shift a REAL tag. The bits of a REAL are a sign, an exponent and a mantissa. Shifting them does not halve the number; it produces a meaningless one.
Where it sits in the rung.
On Siemens the shift box is an action box, like MOVE. If the conditions to its left are true, it runs every scan. Power flow passes through it unchanged. So you can put a coil after it on the same rung. The shift box is not a condition and cannot make the rung false.
Which one for which job
| The job | Siemens | Logix | Because |
|---|---|---|---|
| A verdict travelling down a belt | SHL |
BSL |
the jar leaves the line, and so must its flag |
| The same belt in reverse | SHR |
BSR |
follow the movement, do not invent it |
| A light running round a sign | ROL / ROR |
BSL / BSR with the bit about to fall off fed back to the source |
the pattern has to come back |
| A record of the last sixteen batches | SHL |
BSL |
the seventeenth pushes the oldest out |
| Halving a measurement | DIV |
DIV |
a shift is not a division |
Both belts in this room shift. The sign rotates. The wrong choice shows itself within one lap. A shift on the sign spills the pattern off the end and leaves the building dark. A rotate on the belt brings the flag of a jar that left the line back under the camera.
A shift forgets. A rotate forgets nothing. Will the thing you carry ever come back? Choose by that question.
Reading the register
Reading is the easy part. It is where clever people go wrong most.
The pusher reads one bit.
At_Divert ------( Diverter )
That is the whole rung. No compare, no counter, no latch. The pusher stands at one point on the belt and asks the jar in front of it one question: are you a reject? The register already holds the answer.
There are three tempting alternatives, and all three are wrong.
"Compare the register to 256." It works while there is one reject on the belt. When a second reject arrives, the register reads 257 or 384. The compare goes false and both jars go through.
"Latch the pusher when the flag arrives." Now there are two memories for one fact. One day they will disagree. The latch needs a reset, and the reset needs a condition. You have built a small state machine to replace a contact that was already right.
"Count the steps since the camera." Fine for one jar, hopeless for eight. Every bit in the register already works like a separate counter.
The register is a picture of the belt. Looking at it uses nothing up. Any number of actuators can read the same register. Each looks at its own station, and none of them is in the others' way.
Watch it with lamps, not with a number.
Wire the register to a row of lamps. A display reading 256 tells you nothing. You convert it in your head, and at three in the morning you convert it wrong. A lit square tells you where the jar is. The way the square walks tells you the rest:
| What the lamps do | What it means |
|---|---|
| the square jumps two or three places per step | the shift runs on a level, not an edge |
| it walks the wrong way | wrong direction instruction, or the lamp row is drawn with bit 0 on the left |
| it never lights | the load is a plain coil and is wiped on the next scan |
| it walks while the belt is stopped | something is still shifting on a timer |
| it walks correctly but the pusher is one jar late | count the stations again, on the machine |
The last row matters most. The most common shift-register fault is not a software bug. It is a register whose length came off a drawing instead of off the line. Count the pockets yourself. It takes four minutes.
The pusher's response time. On a fast line the pusher takes time to come out: eighty, a hundred milliseconds. A fast jar is gone by then. The cure is not a cleverer register. Read the station the pusher can reach in time. Measure that station on the machine with a stopwatch and write it in the work order.
Watch the lamps, not the number. The lamps show the belt itself.
Patterns and chasing lights
Take the belt away and the same instruction becomes an animation.
The MERIDIAN sign over the gatehouse is eight lamps and one tag. Put a pattern in the tag and rotate it every so often. The pattern walks along the letters. No eight-rung sequence is needed. One box and a beat are enough.
The beat
A rotate that runs every scan takes a hundred steps a second. The sign just looks lit. You need a metronome: a timer that expires, does one thing and resets itself.
You need the timer's done bit in two places. One rotates the pattern, the other resets the timer. On the real platforms that bit is read as a tag: T_Chase.DN on Logix, the Q member of the timer instance on Siemens. In this simulator you copy the timer's output into a named BOOL with a coil (Chase_Tick). That is a good habit on site too. Everybody reads and watches the same named bit.
The speed is data, not a constant.
Chase_Time is a tag in memory. The timer's preset is that tag, not a number typed into the box. On Siemens the tag is of type TIME. On Logix a timer's PRE is a DINT in milliseconds. Either way the result is the same. The operator turns a selector and the speed changes. Nobody comes out with a laptop to download a program.
A preset in memory can be written by a rung, changed from the operator panel, and watched. If the sign runs at the wrong speed, you go and look at that number. The general rule: if somebody may one day want a number different, that number belongs in memory.
The pattern is loaded, not built in.
The pattern that runs is the one the operator picked on the wheel. Move it into the lamps once (MOVE) and rotate it from there.
Everybody gets this wrong once. Load once, then rotate. If the MOVE runs every scan, it puts the pattern back a hundred times a second. The rotate's work is wasted. The timer times, the rotate rotates, and the lamps do not move. Tie the load to an event: the sign being switched on. On Logix you put an ONS after the Sign_On contact for this. On Siemens you use a P contact. After that the tag belongs to the rotate.
If the operator leaves the wheel at zero, the sign is dark. Rotating zero gives zero. Do not write logic to rescue it. The fault is a wheel left at zero.
Width matters again
Eight lamps in a sixteen-place ring means the pattern spends half of every lap off the sign. Two cures are used on site:
- Make the ring look eight wide. Load the same eight-bit pattern into both bytes of the word. Whatever leaves the top is replaced in the same step by its twin from the other half.
- Fold it back by hand. After each rotate, if the bit just above the top lamp is set, set the bottom lamp and clear that bit.
Both are right. Pick one, write it in the rung comment, and never mix the two in one program.
These two cures are for ROL on Siemens. On Logix, BSL makes the ring. Set the BSL length to eight. As the source bit, give it the lamp that is about to fall off, not .UL. Going right, use BSL with Lamp_N; going left, use BSR with Lamp_M. Then the ring is exactly eight places. If you give it .UL, the ring has nine places, and a single lamp disappears for one beat every lap.
Other uses for a rotate
Pump duty: one bit says which of three pumps leads. The bit moves one place every Monday and the wear is shared. Three pumps sit in a sixteen-place ring, so you fold the bit back at place 3. Lamp test: one light walks along the panel and the operator sees which lamp is dead. A waiting indicator: a running light on a machine that is holding shows from far away that it has not frozen.
A chasing light is not decoration. It is the cheapest way to show that something is still alive.
Two registers on one belt
One belt, two questions: is the jar cracked, and is it discoloured? Two registers.
One register per question
Do not mix two questions in one tag, say cracks on the even bits and colour on the odd ones. It compiles. But every step becomes a shift by two and every station becomes a small calculation. The day somebody adds a third kind of fault, you rewrite everything. One register per question costs one tag. A tag is the cheapest thing in the processor.
Each register has its own load, its own gate and its own clear-down. What they share is the belt.
They must shift together
Both registers describe the same belt. So they must shift on the same edge, in the same scan. Two shift boxes on one rung fed by one edge contact is fine. Two rungs fed by one edge bit is fine too. Two separate edge contacts on the same input in two places is wrong. Shifting one register from a flag another rung sets a scan later is wrong too.
One scan of drift is a permanent one-station difference between the two pictures. It does not fix itself. On the floor it looks like a camera out of focus, and somebody spends an afternoon cleaning a lens.
A jar can be in both
A jar that is cracked and discoloured sets a bit in both registers. The jar reaches the nearer gate first and leaves there. That is why the gate for the most serious fault is closest to the entry.
The fault that costs a day
The crack gate takes the jar at its station. The jar is gone. But nobody has told the colour register. The colour flag keeps walking. A few steps later the colour gate opens and throws out whatever jar is in front of it at that moment. A sound jar goes into the seconds crate.
Worse, it does not look like a fault. Both gates work. The counts are slightly off. The operator says the colour camera is "too sensitive". Somebody lowers the threshold. Now jars that really are discoloured go to the customer as well.
When a gate takes a jar, it must clear that jar from every other register. On the step the gate fires, put a reset coil on the same station bit of the other register (RESET on Siemens, OTU on Logix). The rule is bigger than this machine: anything that takes product off the line must delete it from every picture of the line. That includes a reject chute, a jar lifted off by hand and a full box.
Counts that reconcile
The reason to count per class is not the report; it is the arithmetic. Jars in equals sound out plus cracked out plus discoloured out. If that sum does not close, one of the pictures is wrong. The sum tells you before the customer does.
The faults name themselves:
- a jar counted at two gates: a flag not cleared when the first gate took the jar;
- a jar counted at none: a flag cleared too early, or a register cleared with jars still on the belt;
- the total is right but the classes are wrong: the two registers have drifted a station apart.
Clear-down: all of it at once
The clear-down empties every register and every count in the same scan. A half-cleared line is worse than one never cleared. You act on a register you believe is empty and is not.
The whole chapter comes down to one promise:
What the program knows about the belt is what is standing on the belt.
Shift with the movement, and it stays true when the line slows down. Load once, in one place, and it starts true. Read a station, not a number, and two jars are as easy as one. When the belt is cleared, clear every register. When a jar is taken off, delete it from every picture.
In the margin of the last page, in pencil: "The register is not clever. It just never lies about where things are. That is rarer than clever."