Siemens compare using AR1 and AR2

JOLTRON

Lifetime Supporting Member
Join Date
Aug 2006
Location
MI
Posts
692
I am looking for some advice on this FC I just wrote. I have trace numbers that are constantly moved and want to verify that they match. They are always 10 bytes long stored as CHAR. This is my first attempt at using AR1 and AR2. Is this the most efficient way to do this? Also do I need to save AR1 and AR2 and the restore them at the end of the FC? I remeber hearing this but can't remeber how or why.

Here it is, what do you think?
Code:
FUNCTION "CompareTraceNum" : BOOL
TITLE =Copy block with a block move
{ S7_language := '7(1) Deutsch (Deutschland)  28.05.2007  14:31:12' }
NAME : CopyByte
VERSION : 0.1
 
VAR_INPUT
  DB_Src_1 : INT ; 
  ByteSrc_1 : INT ; 
  DB_Src_2 : INT ; 
  ByteSrc_2 : INT ; 
  Bytes : INT ; 
END_VAR
VAR_TEMP
  tempDB_Src_1 : INT ; 
  tempDB_Src_2 : INT ; 
  tempSrc_1_pntr : DWORD ; 
  tempSrc_2_pntr : DWORD ; 
END_VAR
BEGIN
NETWORK
TITLE =Copy data
//Create Temp DB numbers so they can be used with the OPN command 
      L     #DB_Src_1; 
      T     #tempDB_Src_1; 
      L     #DB_Src_2; 
      T     #tempDB_Src_2; 
 
//Create pointers for base of compare
      L     P#DBX 0.0; //Create pointer for source 1 byte
      L     #ByteSrc_1; 
      SLD   3; 
      +D    ; 
      LAR1  ; 
      L     P#DBX 0.0; //Create pointer for source 2 byte
      L     #ByteSrc_2; 
      SLD   3; 
      +D    ; 
      LAR2  ; 
//compare 1st DWord of Trace
      OPN   DB [#tempDB_Src_1]; 
      L     D [AR1,P#0.0]; 
      OPN   DB [#tempDB_Src_2]; 
      L     D [AR2,P#0.0]; 
      ==D   ; 
      JCN   NOK; 
//compare 2nd DWord of Trace
      OPN   DB [#tempDB_Src_1]; 
      L     D [AR1,P#4.0]; 
      OPN   DB [#tempDB_Src_2]; 
      L     D [AR2,P#4.0]; 
      ==D   ; 
      JCN   NOK; 
//compare last Word of Trace
      OPN   DB [#tempDB_Src_1]; 
      L     W [AR1,P#8.0]; 
      OPN   DB [#tempDB_Src_2]; 
      L     W [AR2,P#8.0]; 
      ==D   ; 
      JCN   NOK; 
//If trace matches set the return value TRUE
      SET   ; 
      =     #RET_VAL; 
      BEU   ; 
NOK:  CLR   ; 
      =     #RET_VAL; 
//SET and SAVE so ENO is always enabled
      SET   ; 
      SAVE  ; 
 
 
 
END_FUNCTION
 
I can save a few instructions, but that is being picky.

If you call this block from an FC/FB and you are using AR1 in a loop in the calling FC/FB, then your FC will have to save and restore AR1

If you call this block from an FB, the editor will automatically save AR2 for you before calling the block and restore it after returning from the block.

Code:
FUNCTION FC 1 : BOOL
TITLE =Copy block with a block move
{ S7_language := '7(1) Deutsch (Deutschland)  28.05.2007  14:31:12' }
NAME : CopyByte
VERSION : 0.1


VAR_INPUT
  DB_Src_1 : INT ;    
  ByteSrc_1 : INT ;    
  DB_Src_2 : INT ;    
  ByteSrc_2 : INT ;    
  Bytes : INT ;    
END_VAR
VAR_TEMP
  tempDB_Src_1 : INT ;    
  tempDB_Src_2 : INT ;    
  tempSrc_1_pntr : DWORD ;    
  tempSrc_2_pntr : DWORD ;    
END_VAR
BEGIN
NETWORK
TITLE =Copy data
//Create Temp DB numbers so they can be used with the OPN command 
      L     #DB_Src_1; 
      T     #tempDB_Src_1; 
      L     #DB_Src_2; 
      T     #tempDB_Src_2; 

//Create pointers for base of compare
      L     #ByteSrc_1; 
      SLD   3; 
      LAR1  ; 
      L     #ByteSrc_2; 
      SLD   3; 
      LAR2  ; 
//compare 1st DWord of Trace
      OPN   DB [#tempDB_Src_1]; 
      L     DBD [AR1,P#0.0]; 
      OPN   DB [#tempDB_Src_2]; 
      L     DBD [AR2,P#0.0]; 
      ==D   ; 
      JCN   NOK; 
//compare 2nd DWord of Trace
      L     DBD [AR2,P#4.0]; 
      OPN   DB [#tempDB_Src_1]; 
      L     DBD [AR1,P#4.0]; 
      ==D   ; 
      JCN   NOK; 
//compare last Word of Trace
      L     DBW [AR1,P#8.0]; 
      OPN   DB [#tempDB_Src_2]; 
      L     DBW [AR2,P#8.0]; 
      ==D   ; 
      JCN   NOK; 
//If trace matches set the return value TRUE
      SET   ; 
      =     #RET_VAL; 
      BEU   ; 
NOK:  CLR   ; 
      =     #RET_VAL; 
//SET and SAVE so ENO is always enabled
      SET   ; 
      SAVE  ; 



END_FUNCTION
 
Very clever! Thanks for looking it over:geek:

So when you use the Address Register you don't need to build the pointer prior to it. I guess that is taken care of here L DBD [AR2,P#0.0];


I guess I don't fully understand this:
If you call this block from an FC/FB and you are using AR1 in a loop in the calling FC/FB, then your FC will have to save and restore AR1
Do you mean using AR1 to indirectly call FC's and FB's?

Is it considred good practice to save and restore the the AR's whenever they are used?


Is it as simple as:
LAR1
T TempDW
[do work in FC]
L TempDW
T AR1
 
Yes, thats the code that the editor inserts prior to calling an FC from an FB but it saves AR2
 
Last edited:
Good spot Kalle, I can save a couple of lines :)
Code:
      TAR1  #dwTemp
      [do work in FC]
      LAR1  #dwTemp
 

Similar Topics

In TIA Portal, I know it is possible to compare what the CPU has running and a blank project. I know it's possible to copy and paste each...
Replies
9
Views
3,063
Hi. I have 2 files where I would like to see the ladder logic differences (similar to the Rockwell compare function). I am using TISoft Siemens...
Replies
1
Views
1,264
Hey i have a array [1..10] that holds an other array with [1..10] real I wonder if there is an easy way to compare those 100 values? I want to...
Replies
3
Views
6,468
Hey, i wonder if there is any way to compare a entirely project also the OS? right now i cant go online when i open a chart, like i do in a...
Replies
0
Views
1,878
Hello I was wondering if anyone knows if it possible to compare a protool project with what is loaded into the HMI (MP270B), I am not sure if I...
Replies
3
Views
3,550
Back
Top Bottom