Loop is behaving weird

petek13

Member
Join Date
May 2006
Location
Kor
Posts
7
I have some problem with loop on Simatic S7-300. It doesn't end at pre-defined number of loops but it is going further and sometimes the loop ends after a while. If the number of loops is set to 2 then it does only 1 instead of 2 and goes forward in program.

Here is the code ( it is in german, but text in Slovenian):


/ L 2
// T DB1.DBW 0
L 1 //stevilo prehodov za posamezni zob
T DB1.DBW 2
//
// L DB1.DBW 0
//
U E 0.2
L C#3
S Z 6
//
L L#1
T MW 184
L DB1.DBW 0
Zank: T MW 18
L MW 22
*D
T MW 22
//
U E 0.3 //izbrano avtomatsko
U E 0.2 //start-stop tipka
FP M 15.0
S A 0.7 //lucka vklop izklop
S M 15.1
ZV Z 2
// L C#30
// S Z 6
//
U M 15.1
UN E 0.4 //pritisk utrjevalca
S A 0.3 //utrjevalec gor
//
U M 15.1
U E 0.4
R A 0.3
////////////
L 2 //vklop-izklop
L Z 2
==R
R M 15.1
R A 0.7
R A 0.3
R M 30.1
//
R Z 3
R Z 2
R Z 6
R M 11.0
U E 0.0
FN M 12.5
R A 0.2
//
// L 1 //stevilo prehodov za posamezni zob
// T DB1.DBW 2
//
U M 15.1
U E 0.3 //avtomatsko
// UN E 0.6 //desno koncno stikalo
U E 0.6
FN M 160.4
ZV Z 3 //stevec prehodov
L Z 3
L DB1.DBW 2
==R
S M 11.2
ZV Z 4
R M 15.1
R A 0.3
S A 0.2 //utrjevalec dol
//
R Z 3
S M 11.0
// SPB Zank
U E 0.4 //dosezen pritisk
U E 0.5 //koncno stikalo L
UN E 0.6 //desno koncno stikalo
UN M 11.0
S A 0.1 //miza levo
//
U E 0.5 //koncno szikalo L
FN M 120.0
R A 0.1
S A 0.0 //miza desno
//
U E 0.6 //koncno stikalo D
FN M 30.0
R A 0.0 //miza desno
S A 0.1
//
// L 2
// T DB1.DBW 0
// T MB 10
// Zank:
R A 0.1
R A 0.0
UN Z 2
R Z 3
R Z 5
R A 0.2
R A 0.3 //pomik utrjevalca gor
R A 0.7
R A 0.1
R M 15.1
R M 30.1
R A 0.6
R M 11.2
R A 0.0
// U E 0.7
// R Z 3
// R Z 2
// R M 11.0
// U E 0.0
// FN M 12.5
// R A 0.2
U A 0.7 //tipka za vklop je aktivna
U E 0.0 //utrjevalec spodaj
FN M 160.3
UN E 0.4 //ni pritiska (utrjevalec ne pritiska na zob)
UN E 0.6 //D koncno stikalo mize
= M 11.4
U M 11.4
U M 11.2 //konec 1-nega prehoda
FP M 11.3
S A 0.6 //Zasuci komad za 1 zob
/////////////////
L 20
T DB1.DBW 4
//
FR Z 5 // stevec impulzov rotgeberja
U A 0.6 //sukanje komada
U E 0.3 //izbrano avtomatsko
// U E 0.4
UN E 0.0 // utrjevalec spodaj
// FN M 160.2
S M 160.7
//
U M 160.7
UN M 2.0
L S5T#100MS
SE T 10
U T 10
= M 2.0
U M 2.0
UN M 2.1
S M 2.2
U M 2.0
U M 2.1
R M 2.2
UN M 2.0
U M 2.2
S M 2.1
ZV Z 5 //stevec impulzov rotgeberja
// FP M 160.1
// = A 0.5 //samo da vidim impulze
UN M 2.0
UN M 2.2
R M 2.1
L Z 5
L DB1.DBW 4 //imp. med zobmi
==R
// ZV Z 6
R A 0.6
R M 160.7
R Z 5
S M 15.1 // uporabljeno za zanko cikla (setira a0.3)
R M 11.0
L MW 18
LOOP Zank
ZV Z 7
R M 15.1
R M 11.0
R A 0.7
R Z 2
// R Z 6
NOP 0




Everything else is working fine (or the way i imagine it to work). Only this loop for repeating the proces doesn't work as it should. Does anyone have any idea to fix this?

Thanx in advance,
Greetings from M.K.
 
Zank: T MW 18

Try to change this to

T MW 18
Zank: xxx

I can not test this now, but maybe this will help. Your code for the loop looks good. An other example for Loop is:

next: xxx

xxx
xxx
L MW18
Loop next
 
If I try it to change into:

T MW 18
Zank:xxx
...
...
L MW18
Loop Zank


I get the error (SF) on siemens cpu 313
Error details:

Event 1 of 55: Event ID 16# 4568
STOP caused by time error (OB not loaded, or not possible, or no FRB)
Caused by OB: 1 Priority class: 1
FC number: 2
Module address: 270
Previous operating mode: RUN
Requested operating mode: STOP (internal)
Internal error, Incoming event

Funny is that even two loops one after another are not the same. I set it for 3 loops and i get 7, then after this i started the same process and get over 10 loops then i canceled. After another try i get 5.
 
I have also not run this code (as it's in German) but cannot see anything wrong (Ivo's suggestion will prevent the loop from terminating as it will not store the updated loop count). MW18 is used as the loop counter and is not modified inside the loop - could this be referenced in an interrupt routine and is being corrupted ? There are also no jumps inside the loop to cause you to exit the loop early.

Note: You are performing double integer arithmetic on MW18 and MW22 and storing the result in MW22. Beware of overflow and negative numbers using this method. MW22 is not used inside the loop so this bit of code is redundant anyway.
 
More questions - why are you repeating the processing inside the loop, how do you know how many times the loop is executing ?
 
Last edited:
did you tried using (WORD!) constant?

L 5
Zank: T MW 18
// ...
// ...
// ...
L MW 18
LOOP Zank

and these are...please... what?
// ...
L MW 22
*D
T MW 22

// ...
L L#1
T MW 184

well... let me gues... aren't you using somewhere.... ?
L L#3
T MW 16

and like SimonGoldsworthy already said...
SimonGoldsworthy said:
(Ivo's suggestion will prevent the loop from terminating as it will not store the updated loop count).

keep on work! :)
 
Zank: T MW 18

Try to change this to

T MW 18
Zank: xxx

This doesn't work, that part is OK as it is.

LOOP decrements what is in the accumulator and loops back to the label providing the accumulator is greater than zero. The code loads MW18 into the accumulator, LOOP decrements it and jumps back to Zank, the first thing at Zank MUST be T MW18, otherwise MW18 will never itself decrement and loop will become never ending.

Must say, the code is awful 'bity'.



L L#1 is a double word function, therefore needs to transferred into a double word otherwise just L 1 would have done.


again MW18 multiplied by MW22 is *D, a double word function, *I would be better or if the calc is expected to be greater than 32767 change MW22 to a double word.

Where you have FP M 15.0 for example, if your using this as a one shot, you need to A the flag as the RLO is not changed with the FP instruction.

i.e

FP M 15.0
A M 15.0
A Q 0.7
S M 15.1

you do the same thing with FN instructions later on, the FN itself is not the one shot, its conditioning the flag as the one shot.

At one point you compare a counter value to the integer 2, but use the instruction ==R, which is REAL (floating point), it should be ==I.
 
Last edited:
Another thing, some outputs are being set by one-shots, therefore as its a loop and input conditions will not change as you go around, just a few flags may change state, the outputs switched on with a one shot will be reset regardless on a second run of the loop.

Don't like the timer in there either, just sits odd.

I'd seriously look at the process that this loop is controlling. Understand it and then re-write the code.

Too many bits in there..o_O


Ah well, off to a party now.... 🍺
 
I'm sorry if the code is 'bity', but this is my first program writen in step7. I'll try to make it better. Thanx to all for your sugestions but the problem is not solved yet. Any other tips?

P.S. The timer is inthere just for simulating some incoming impulses, later on it will be removed.

Have a nice day
M.K.
 
Last edited:
Simon I enter the number of loops at the begining (for now I load a value into db1.dbw0, later on this number will be entered from VT170(esa)).
 
Yes, you may be setting the number of times you want the loop to execute, but how do you know how many times it actually executes ?

In your first post you said you set the number of loops to 2 but it only executed the loop once. How do you know it only executed once ?
 

Similar Topics

Hi, I'm quite new to Rockwell PLC and currently trying to implement the following if Sw1 then for i:=1 to 20 by 1 do xEnable := 1...
Replies
4
Views
128
Hi all, I'm connecting several 4-20mA sensors together in parallel (only one shown below) The enclosure is ABS plastic with metal backplate DAQ...
Replies
5
Views
258
I have always controlled servos in Rockwell motion using position loop. I have an application where one process will push against a servo...
Replies
3
Views
261
Hello All Could we get some expertise on flow control ? -Using a PID loop in Productivity 2000 with an analog output, How can we convert...
Replies
19
Views
1,560
Complete noob here, using a Click C0-02DD1-D to run a test stand. Requirement is to turn on motor run for X seconds, turn off and dwell for X...
Replies
6
Views
1,062
Back
Top Bottom