Siemens: copy specific word address in DB

Holmux

Lifetime Supporting Member
Join Date
Oct 2013
Location
Aalborg
Posts
279
Good evening

Hope I can get a little help with this one :)

When I scroll true my DB and find a certain value, I would like to copy the actual address in the DB to a tag, is there a function I can use or is more simple then that ?
 
Sorry LD for the late reply

I was trying to spool down true a DB to find a specific value and then transfer the DB address of this value to a tag.

Well I will probably have to step-up and use indexed addressing anyway to do the Search, so when I get that right :) I will have the pointer as my value.

First I will have to fill the DB,
so I am working on a routine that will insert data into the next available address in the DB until its full.
This must have been done a million times before, Do You know of any sample project that I can have a look at ?

Thanks
/Holmux
 
Hi LD

I am feeding the DB with different values, I have a DB with DINT where a fix't part is filled with different values.

Out of these values I need a Minimum, Maximum and Average,
I have found a way to sort the values so I have minimum and the first address and Maximum at the last address, (my first day with STL, so I have to cheat where ever i can :) )

And i found a code for generating an Average of the data area, so in theory a am able to put my project together, the last part I am working on is combining the two codes to safe a loop, maybe I am going to deep in my surge for scan time :unsure:

Here are the codes I am trying to combine:

//Average
L P##values_to_average
LAR1
L W [AR1,P#2.0]
T #elem_count
L W [AR1,P#4.0]
T #db_num
OPN DB [#db_num]
L D [AR1,P#6.0]
LAR1

L 0
T #sum
L #elem_count
_loo: T #loop_temp

L W [AR1,P#0.0]
L #sum
+I
T #sum

L 16
+AR1

L #loop_temp
LOOP _loo

L #sum
L #elem_count
/I
T #RET_VAL

//Sorting
OPN #Data_DB //Open Data Block
M002: LAR1 P#DBX 0.0
SET //LET #Sort_done = TRUE
S #Sort_done
L #DB_Length
L 1
-I
Loop: T #Count //FOR INDEX = Count TO DB_length
L W [AR1,P#0.0]
L #Average // load sum of all previous elements
+D // add these two together !!!
T #Average // store it in the sum
L W [AR1,P#0.0] //IF M(INDEX) > M(INDEX+1) THEN
L W [AR1,P#2.0]
<=I
JC M003
// change M(INDEX+1) for M(INDEX)
L W [AR1,P#0.0] //LET TEMP = M(INDEX)
T #Temp
L W [AR1,P#2.0] //LET M(INDEX) = M(INDEX+1)
T W [AR1,P#0.0]
L #Temp //LET M(INDEX+1) = TEMP
T W [AR1,P#2.0]
SET
R #Sort_done //LET #Sort_done = FALSE
M003: TAR1
L 16 //NEXT INDEX
+D
LAR1
L #Count
LOOP Loop //End of sorting pass
AN #Sort_done //If sorting is not done
JC M002 //Jump Conditionally to M002
L #Average
L #DB_Length
/D
T #Average
BE
 
Your code is summating swapped data so the average will be incorrect. No need to sort the data - set up a min/max and compare as you summate.Your code is also operating on integers, not DINTs as your post specifies.
Hint - prefix your variable names with their data type to minimise coding errors.

Here's my implementation. I've calculated the average in a separate network to make it easier to monitor.

I've posted source code that can be cut/pasted into a STL source code container and compiled to generate the block container.

Code:
FUNCTION FC 667 : VOID
TITLE =
VERSION : 0.0


VAR_TEMP
  bSort_Done : BOOL ;    
  diAverage : DINT ;    
  iTemp : INT ;    
  iDB_length : INT ;    
  iCount : INT ;    //Sorting
  iMax : INT ;    
  iMin : INT ;    
END_VAR
BEGIN
NETWORK
TITLE =

      OPN   DB   666; //Open Data Block
      L     10; 
      T     #iDB_length; 
      L     DBW    0; 
      T     #iMax; //init max and min
      T     #iMin; 
      L     0; 
      T     #diAverage; 
      LAR1  P#DBX 0.0; 
      L     #iDB_length; 
Loop: T     #iCount; //FOR INDEX = Count TO DB_length
      L     W [AR1,P#0.0]; 
      ITD   ; 
      L     #diAverage; // load sum of all previous elements
      +D    ; // add these two together !!!
      T     #diAverage; // store it in the sum

      L     #iMax; 
      L     W [AR1,P#0.0]; 
      <I    ; 
      JCN   nmax; 
      T     #iMax; 

nmax: L     #iMin; 
      TAK   ; 
      >I    ; 
      JCN   nmin; 
      T     #iMin; 
nmin: +AR1  P#2.0; 
      L     #iCount; 
      LOOP  Loop; //End of sorting pass




NETWORK
TITLE =

      L     #diAverage; 
      L     #iDB_length; 
      /D    ; 
      T     #diAverage; 
      L     #iMax; 
      L     #iMin; 

END_FUNCTION
 
Last edited:

Similar Topics

This feels like a simple, elementary question, but I am a Siemens novice and know there are experts here. My S7-1500 project (TIA Portal v17) has...
Replies
10
Views
2,711
Gents, I have a DB, that is an array of a UDT. This UDT has got a combination of INT's and REAL's. When an action is completed, I want to move...
Replies
11
Views
4,237
How do I move one byte of data in an array to a UDT that is one byte in length. AB would be a slam dunk on this with a simple Copy instruction...
Replies
22
Views
5,399
I am tying to find a way to copy 10 words from my input area into a structure I have mapped out for my device. What is the best/simplest way to...
Replies
5
Views
2,156
Hello! Is it possible to copy pointer's pointing address to any variable at Siemens step7? I mean I have pointer address to db block with...
Replies
14
Views
15,541
Back
Top Bottom