Simatic S7-STL Bubblesort funciton code

miami_vice

Member
Join Date
Aug 2010
Location
Aruba
Posts
9
Hi to everybody,

I'm new in the Simatic S7 world......
I've searched in this forum for some post about how realize in STL code a bubblesort function, but sorry I don't have success for realize the routine.

So I need to realize the follow code in FC routine.
In the DB10 are written 100 word in INT format, on an event eg.m100.0 the data in the DB10 must be sorted in DB11 having same size and data format.....

Someone can supply the file project or code to make the FC routine??

Thanks Thanks a lot
 
Last edited:
Scl

I can give you my SCL Bubble Sort for S7, does this help ?
Regards,
Gerry

Code:
FUNCTION FC123: VOID
// BUBBLESORT IN ARRAY
VAR_INPUT
TRIG: BOOL;
Data_In: ARRAY[1..10] OF INT;   
END_VAR    
VAR_OUTPUT
Data_Out: ARRAY[1..10] OF INT;
END_VAR    
VAR_IN_OUT
Flank_Mem: BOOL;    
END_VAR    
VAR_TEMP
i: INT;
v: BOOL;
j: INT;
Temp: INT;
Data: ARRAY[1..10] OF INT; 
Trig_Flank: BOOL;
END_VAR

BEGIN
Trig_Flank:= TRIG AND (NOT Flank_Mem); // Positieve flank op TRIG bit
IF Trig_Flank THEN
  FOR i := 1 TO 10 DO
    Data [i]:= Data_In[i] ;            // Data_In naar tijdelijke Data verzenden voor verwerking
  END_FOR;     
    i := 1;
    v := True;        
    WHILE i < 10 AND v = True DO
        v := False;
        j := 1;
        WHILE j <= 10 - i DO
            IF Data[j] > Data[j + 1] THEN
                Temp := Data[j];
                Data[j] := Data[j + 1];
                Data[j + 1] := Temp;
                v := True;
            END_IF;
            j := j + 1;
        END_WHILE;
        i := i + 1;
    END_WHILE;
   
    FOR i := 1 TO 10 DO                // Gesorteerde tijdelijke Data verzenden naar Data_Out
    Data_Out [i]:= Data[i] ;
    END_FOR;
END_IF; 
Flank_Mem:= TRIG;                      // Flank wegschrijven
END_FUNCTION




Hi to everybody,

I'm new in the Simatic S7 world......
I've searched in this forum for some post about how realize in STL code a bubblesort function, but sorry I don't have success for realize the routine.

So I need to realize the follow code in FC routine.
In the DB10 are written 100 word in INT format, on an event eg.m100.0 the data in the DB10 must be sorted in DB11 having same size and data format.....

Someone can supply the file project or code to make the FC routine??

Thanks Thanks a lot
 
Thanks a lot Gerry.

I have not the SCL tool programming installed in my S7.
So when I open the routine function FC123, suppose that the original source code is compiled automatically in STL, but really the code is very big respect the SCL.

As I have told in my previous message I'm new in S7 world, but I think that during the compile operation for convert the code from SCL to STL, S7 use many instruction and the program is not very efficient about the memory used and is not very clear to understand (like spaghetti plate) especially for me don't have a lot esperience in S7.

Maybe is possible rewrite better the code in STL respect the code generated from SCL to STL, but maybe this is only my personal opinion.

Anyway your cooperation is very appreciated.
 
:)

I'm no SCL expert :). I think you need to write the For Next with a loop instruction in STL. But, I never wrote databasecode in STL.

Regards,
Gerry

Thanks a lot Gerry.

I have not the SCL tool programming installed in my S7.
So when I open the routine function FC123, suppose that the original source code is compiled automatically in STL, but really the code is very big respect the SCL.

As I have told in my previous message I'm new in S7 world, but I think that during the compile operation for convert the code from SCL to STL, S7 use many instruction and the program is not very efficient about the memory used and is not very clear to understand (like spaghetti plate) especially for me don't have a lot esperience in S7.

Maybe is possible rewrite better the code in STL respect the code generated from SCL to STL, but maybe this is only my personal opinion.

Anyway your cooperation is very appreciated.
 
Here is Combo's SCL code re-written in STL using DB10&11 for the data using the evolutionary biodegradable compiler :)

Code:
FUNCTION FC 124 : VOID
TITLE =
VERSION : 0.1


VAR_INPUT
  TRIG : BOOL ;    
END_VAR
VAR_IN_OUT
  Flank_Mem : BOOL ;    
END_VAR
VAR_TEMP
  ii : INT ;    
  v : BOOL ;    
  j : INT ;    
  Temp : INT ;    
  Trig_Flank : BOOL ;    
  iSFC20Return : INT ;    
END_VAR
BEGIN
NETWORK
TITLE =
//
//
      A     #TRIG; 
      FP    #Flank_Mem; 
      =     #Trig_Flank; 
      A     #Trig_Flank; 
      JCN   exit; 
      CALL SFC   20 (
           SRCBLK                   := P#DB10.DBX 0.0 INT 100,
           RET_VAL                  := #iSFC20Return,
           DSTBLK                   := P#DB11.DBX 0.0 INT 100);
      L     1; 
      T     #ii; // ii=1
      SET   ; 
      S     #v; // v = True
whl1: NOP   0; 
      A(    ; // while ii < 100 and v = true
      L     #ii; 
      L     100; 
      <I    ; 
      )     ; 
      A     #v; 
      JCN   enw1; // do
      R     #v; // v = false
      L     1; // j = 1
      T     #j; 
whl2: NOP   0; //While j <= 100 - ii
      L     100; 
      L     #ii; 
      -I    ; 
      L     #j; 
      TAK   ; 
      <=I   ; 
      JCN   enw2; 
      OPN   DB    11; // do
      L     #j; 
      +     -1; 
      SLD   4; 
      LAR1  ; 
      L     DBW [AR1,P#0.0]; // if Data[j] > Data[j+1]
      L     DBW [AR1,P#2.0]; 
      >I    ; 
      JCN   eif1; 
      TAK   ; // then Begin 
      T     #Temp; //       Temp = Data[j]
      L     DBW [AR1,P#2.0]; 
      T     DBW [AR1,P#0.0]; //       Data[j+1] = Data[j]
      L     #Temp; 
      T     DBW [AR1,P#2.0]; //       Data[j] = Temp
      SET   ; 
      S     #v; //       v = true
eif1: NOP   0; //      end
      L     #j; // j = j + 1
      +     1; 
      T     #j; 
      JU    whl2; 
enw2: NOP   0; // endwhile

      L     #ii; // ii=ii+1
      +     1; 
      T     #ii; 
      JU    whl1; 
enw1: NOP   0; //endwhile

exit: NOP   0; 

END_FUNCTION
 
STL expert

Voila, thanks to an STL expert you have the code you needed :).


Here is Combo's SCL code re-written in STL using DB10&11 for the data using the evolutionary biodegradable compiler :)

Code:
FUNCTION FC 124 : VOID
TITLE =
VERSION : 0.1
 
 
VAR_INPUT
  TRIG : BOOL ;    
END_VAR
VAR_IN_OUT
  Flank_Mem : BOOL ;    
END_VAR
VAR_TEMP
  ii : INT ;    
  v : BOOL ;    
  j : INT ;    
  Temp : INT ;    
  Trig_Flank : BOOL ;    
  iSFC20Return : INT ;    
END_VAR
BEGIN
NETWORK
TITLE =
//
//
      A     #TRIG; 
      FP    #Flank_Mem; 
      =     #Trig_Flank; 
      A     #Trig_Flank; 
      JCN   exit; 
      CALL SFC   20 (
           SRCBLK                   := P#DB10.DBX 0.0 INT 100,
           RET_VAL                  := #iSFC20Return,
           DSTBLK                   := P#DB11.DBX 0.0 INT 100);
      L     1; 
      T     #ii; // ii=1
      SET   ; 
      S     #v; // v = True
whl1: NOP   0; 
      A(    ; // while ii < 100 and v = true
      L     #ii; 
      L     100; 
      <I    ; 
      )     ; 
      A     #v; 
      JCN   enw1; // do
      R     #v; // v = false
      L     1; // j = 1
      T     #j; 
whl2: NOP   0; //While j <= 100 - ii
      L     100; 
      L     #ii; 
      -I    ; 
      L     #j; 
      TAK   ; 
      <=I   ; 
      JCN   enw2; 
      OPN   DB    11; // do
      L     #j; 
      +     -1; 
      SLD   4; 
      LAR1  ; 
      L     DBW [AR1,P#0.0]; // if Data[j] > Data[j+1]
      L     DBW [AR1,P#2.0]; 
      >I    ; 
      JCN   eif1; 
      TAK   ; // then Begin 
      T     #Temp; //       Temp = Data[j]
      L     DBW [AR1,P#2.0]; 
      T     DBW [AR1,P#0.0]; //       Data[j+1] = Data[j]
      L     #Temp; 
      T     DBW [AR1,P#2.0]; //       Data[j] = Temp
      SET   ; 
      S     #v; //       v = true
eif1: NOP   0; //      end
      L     #j; // j = j + 1
      +     1; 
      T     #j; 
      JU    whl2; 
enw2: NOP   0; // endwhile
 
      L     #ii; // ii=ii+1
      +     1; 
      T     #ii; 
      JU    whl1; 
enw1: NOP   0; //endwhile
 
exit: NOP   0; 
 
END_FUNCTION
 
Thanks still everyone for your help....

I'm testing the new code rewrite from LD, and the memory occupation of the routine rewrite in STL is 324 bytes now respect the 1456 bytes of STL code produced from the SCL conversion to STL.

Really I don't understand why compiling the conversion from SCL to STL use a lot resources of memory and a lot of instructions respect the code produced from LD.

Anyway I've the code of the two versions and......

Thanks again Gerry and LD.
 
I'm tested but in the new STL routine there is a bug I think...

The routine working well only if the data unsorted write in the DB10 are all negative in case of positive number the data are not write in the DB11.

If i change the code from
Code:
L     DBW [AR1,P#0.0]; // if Data[j] > Data[j+1]
      L     DBW [AR1,P#2.0]; 
      >I    ;

to

Code:
L     DBW [AR1,P#0.0]; // if Data[j] > Data[j+1]
      L     DBW [AR1,P#2.0]; 
      <I    ;

I get the opposite situation...only positive number are write in DB11.
in my case I've positive and negative numbers...maybe I must insert a double control for minus and great??
 
My apologize LD...

I tested inserted only 6 number unsorted, I think find them sorted in this mode:

Minus number in DB11.DBW0
Great number in DB11.DBW5

and not the great number in the bottom of the DB.

Sorry again
 
ylade if you would read whole thread as it is not long, you would see that it was LD who rewrote scl implementation in stl.

Here is Combo's SCL code re-written in STL using DB10&11 for the data using the evolutionary biodegradable compiler :)
 
I was specifically asking Miami how he converted his origional SCL code to STL.

From post 8!

"...... now respect the 1456 bytes of STL code produced from the SCL conversion to STL.

Really I don't understand why compiling the conversion from SCL to STL use a lot resources of memory and a lot of instructions respect the code produced from LD...."
 

Similar Topics

Hi, I recently had an incident with a program that was written in STL. It went like this: A M0.0 = Q42.0 A( AN M6.1 AN M6.2 O M7.0 ) = Q42.1...
Replies
20
Views
6,051
Hello,maybe somebody can give me advice how to edit Value T81 in my attachment.I’m just beginner so maybe can explain to me step by step.thank you
Replies
0
Views
1,547
Hello everyone! I've just stared with programming on STL and I have problem which I can't understand. I'm writing programs on Step 7 and I'm...
Replies
2
Views
2,202
Hello everyone! I've just stared with programming on STL and I have problem which I can't understand. I have this piece of code:
Replies
1
Views
1,960
Hello, I am new to PLC programming. I have a very basic doubt about STL programming. As per Siemens manual 'Statement List (STL) for S7-300 and...
Replies
1
Views
2,322
Back
Top Bottom