Datablock Shift

Daynada

Member
Join Date
Nov 2005
Location
Wisconsin
Posts
28
I have a datablock that has 119 interger words in it.
I would like to shift all of the interger words by one spot.
So I would like DB1.dbw0 to go into .dbw2 to go into .dbw4 and so on until .dbw238 simply drops off the map.
Is this possible?
Thanks in advance.
 
Assuming talking about S7-300/400 here.

Easiest way is to use FC20 (Block move), but it would need 2 moves as it does not allow overlaps, therefore move DB1.DBX0.0 236 bytes long into a temp area, them move the temp area to DB1.DBX2.0 236 bytes long, then zero DB1.DBW0.
 
or here's an example FC that will do the job:

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

VAR_INPUT
  dbDataBlock : BLOCK_DB ; 
END_VAR
VAR_TEMP
  iLoopCount : INT ; 
END_VAR
BEGIN
NETWORK
TITLE =
	  OPN   #dbDataBlock; 
	  L	 DBLG; //db length in bytes
	  SRW   1; // length in ints
	  +	 -1; // do 1 less than length
Loop: T	 #iLoopCount; 
	  +	 -1; 
	  SLD   4; //convert to pointer to int
	  LAR1  ; 
	  L	 DBW [AR1,P#0.0]; //shift int down
	  T	 DBW [AR1,P#2.0]; 
	  L	 #iLoopCount; 
	  LOOP  Loop; 
END_FUNCTION
 
Here is an exaple that does what you want, it's part of a rolling average routine. The code assumes that the first value is DBxxx.DBW0. There are more elegent ways of doing it but this works OK.

#DBlock - Integer DataBlock Number
#iNum_values - Integer number of entries to process
#iNew_Value - New value to add to the top of the list.


<code>
OPN #DBlock
L #iNum_values
SLW 1 // Times 2 for bytes
T #iTemp
ma01: L #iTemp // Ripple down the values
SLD 3 // Losing the oldest one
T #pValueN // Value N
L #iTemp
L -2
+I
SLD 3
T #pValueN1 // Value N-1
L DBW [#pValueN1] // Load N-1
T DBW [#pValueN] // Save as N
L #iTemp
L -2
+I // Decrement counter
T #iTemp
L 0
>I
JC ma01 // Next
// Ripple done
L #iNew_value
T DBW 0 // Put the new value at the top
</code>

Nick

Edit: Simons solution is much more elegant
 
Last edited:

Similar Topics

In a data block, if the tag is ticked in the "Retain" column", is it meant to be retained after cycling the power to the PLC? I'm finding that...
Replies
2
Views
731
I'm having a bad day. Daft question coming up...Anyone know why I can't add anything into this DB? Add button is greyed-out. Thanks
Replies
14
Views
1,589
Hi Looking for some advice. I have a project that was developed for an S7-300. In that project I have a number of datablocks which only contain...
Replies
7
Views
2,609
Hi, Whenever I update a projects CPU with new code and add more variables to datablocks I encounter a problem that I'm sure there is a correct...
Replies
7
Views
3,270
Hi, Can anyone tell me how to copy a input address area with a length of 64 words to a datablock the easiest way? If possibly a code exampel...
Replies
4
Views
1,404
Back
Top Bottom