S7 Indirect Addressing Again

CharlesM

Member
Join Date
Aug 2005
Location
Arkansas
Posts
1,129
Can someone take a look at this and see what I am doing wrong. I am planning on putting this into a loop but I cant get data from one DB to another. Its the same basic code I used before but I was moving from a MW to a DB.

A(
A L 2.0
///// read position put in Point DB
// OPN #Point_DB //open DB w/array
OPN DB 207
L MW 422 // load index
SLW 3 // shift 3 bits
T MD 20 //pointer
L DB202.DBW 8 // read from here
T DBW [MD 20] // write to db
)
 
CharlesM said:
Can someone take a look at this and see what I am doing wrong.

Hi CharlesM
You are not doing anything wrong if MW422 should point to byte address. I assume, it should point to word address -> replace SLW3 by SLW4.

Pete 🍺
 
Thanks Pete, I was able to get this to work with words. Now I want to do it double words. Does the SLW change to SLD?

A(
A L 2.0
///// read position put in Point DB
// OPN #Point_DB //open DB w/array
OPN DB 207
L MW 422 // load index
SLD 3 // shift 3 bits
T MD 20 //pointer
L DB202.DBD 8 // read from here
T DBD [MD 20] // write to db
)
 
CharlesM said:
L DB202.DBW 8 // read from here
T DBW [MD 20] // write to db
)

The way this is coded, the destination DB will be DB202 as the instruction L DB202.DBW8 actually performs an OPN DB202 before executing L DBW8. You need to open the correct destination DB just before the T DBW[MD20] instruction. Is DB202 the destination DB ?
 
I am wanting to load the values out of DB202 into an array DB207
you could open the second DB with the OPN DI instruction.


If the address range is always the same, you could use SFC20 "Block Move"

CALL "BLKMOV"
SRCBLK :=P#DB202.DBX 0.0 BYTE 50
RET_VAL:=#RET_VAL_SFC21 //Temp INT
DSTBLK :=P#DB207.DBX 0.0 BYTE 50

 
I use the following FC to copy data between data blocks. You can either pass the DB as a parameter, or, pass the db number as a parameter. (I use the db number when looping around a number of sequential DB's).

Code:
FUNCTION FC200 : VOID
TITLE =transfer bytes between db's
//Transfer bytes of data between data blocks using SFC20
//
VAR_INPUT
  dbSourceDB : BLOCK_DB ;   //source data block
  iSourceDBNumber : INT ;   //source data block number (set to zero to use above)
  iSourceByteOffset : INT ; //source data byte offset 
  dbDestinationDB : BLOCK_DB ;  //destination data block
  iDestinationDBNumber : INT ;  //destination data block number (set to zero to use above)
  iDestinationByteOffset : INT ;	//destination data byte offset
  iNumberOfBytes : INT ;	//number of bytes to xfer
END_VAR
VAR_OUTPUT
  iMoveResult : INT ;   //result from block xfer SFC20
END_VAR
VAR_TEMP
  pSourceDB : ANY ; 
  pDestDB : ANY ;   
END_VAR
BEGIN
NETWORK
TITLE =any pointer for source
	  LAR1  P##pSourceDB; 
	  L	 W#16#1002; //specify ANY pointer, type 02 = BYTE
	  T	 W [AR1,P#0.0]; 
	  L	 #iNumberOfBytes; //number of bytes to transfer
	  T	 W [AR1,P#2.0]; 
	  L	 0; 
	  L	 #iSourceDBNumber; //if iSourceDBNumber = 0 then 
	  ==I   ; //  opn db and use DBNO to find number
	  JC	opnS; //  else use iSourceDBNumber
	  JU	trnS; 
opnS: OPN   #dbSourceDB; 
	  L	 DBNO; //data block to use
trnS: T	 W [AR1,P#4.0]; 
	  L	 DW#16#84000000; //global data block for area pointer
	  L	 #iSourceByteOffset; 
	  SLD   3; 
	  +D	; 
	  T	 D [AR1,P#6.0]; //start of data block
NETWORK
TITLE =any pointer for destination
	  LAR1  P##pDestDB; 
	  L	 W#16#1002; //specify ANY pointer, type 02 = BYTE
	  T	 W [AR1,P#0.0]; 
	  L	 #iNumberOfBytes; //number of bytes to transfer
	  T	 W [AR1,P#2.0]; 
	  L	 0; 
	  L	 #iDestinationDBNumber; //if iDestinationDBNumber = 0 then 
	  ==I   ; //  opn db and use DBNO to find number
	  JC	opnD; //  else use iDestinationDBNumber
	  JU	trnD; 
opnD: OPN   #dbDestinationDB; 
	  L	 DBNO; //data block to use
trnD: T	 W [AR1,P#4.0]; 
	  L	 DW#16#84000000; //global data block for area pointer
	  L	 #iDestinationByteOffset; 
	  SLD   3; 
	  +D	; 
	  T	 D [AR1,P#6.0]; //start of data block
NETWORK
TITLE =copy data
	  CALL "BLKMOV" (
		   SRCBLK				   := #pSourceDB,
		   RET_VAL				  := #iMoveResult,
		   DSTBLK				   := #pDestDB);
	  NOP   0; 
NETWORK
TITLE =Make sure the ENO shows the BLKMOV result
	  A	 BR; // BR is 1 for no error
	  JC	ok; 
	  JU	nok; 
ok:   SET   ; // Make the ENO high
	  SAVE  ; 
	  JU	end; 
nok:  CLR   ; // Make the ENO low
	  SAVE  ; 
end:  NOP   0; 
END_FUNCTION
 
If the address range is always the same, you could use SFC20 "Block Move""][/

DB202 address will be the same however DB207 will be indexed. I have not added the loop yet. I just wanted to move them one at a time to start.
 
Charles, this will work but a few words of warning. By using SLD will allow you to point farther into a DB. Most people forget the highest number using slw4 is 37648 /16 = 2353. By using SLD4 the pointer (mw422) will point to EVEN number dbw's. That's a good thing. If you point to odd dbw's your values will be all screwed up. If you wanted to put the value into DBW8000 the pointer value would be 4000. You couldn't do that with slw4. You are limited to using a DINT for an indirect pointer, keep the same format. I prefer using a Local Variable, TEMP_DINT and call it Pointer_X16 in a FC in place of MD20 which will save valuble internal memory. Lastly, you are using higher MW's and DB's that will work fine in a S7-400 and in the simulator but when you try to download to a smaller S7-300 you will get errors because the range is too high.

Opn Db 207
L Mw 422
SlD 4
T #Pointer_X16
L Db202.dbw 8
T Dbw [#Pointer_X16]
 
CharlesM said:
DB202 address will be the same however DB207 will be indexed. I have not added the loop yet. I just wanted to move them one at a time to start.

It looks like SFC21 (fill memory area) would be better for your application.
 

Similar Topics

Howdy folks, I am an Allen Bradley guy currently living in an Emerson world. Working with Rx3i on PacSystems Machine Edition v. 9.6? i think...
Replies
3
Views
629
Hello, I'm very new to programming with absolutely zero schooling in this field and pretty hands off training in my new role, it's been fun...
Replies
4
Views
673
Hello Friends, I am trying to index M Bits, however GX Works2 is not allowing it with following message. https://ibb.co/zPcqj6M...
Replies
3
Views
1,391
Hi All, which the best way to do the indirect addressing in an optimize DB? Ccurrently this is my partial code inside an FB...
Replies
7
Views
2,280
Hey everyone, Just used the PLC5/Logix migration utility to convert a program, and while addressing the PCEs, I noticed a lot of errors for "XIC...
Replies
12
Views
2,010
Back
Top Bottom