S7 pointers - help for beginner

italo2008

Member
Join Date
Feb 2009
Location
Padova
Posts
95
Hi

I started programming in step7 but I'm not very skilled in it yet... I think that the question at my answer will be very simple for many of you..

I have to set a number of BITS (for example: the first 20 bits of a data block) in a data block using a pointer.


In all the examples that I found on the web, pointers refers to BYTES or WORD, so I don't know how I can increment the pointer. This is what I did:

Code:
L P#0.0
T #Generic_Pointer
AUF DB9
 
 
SDBB:  S DBX [#Generic_Pointer]
       L P#0.1
       L #Generic_Pointer
       +D
       T #Generic_Pointer
       L #number_of_bits_to_be_set
       LOOP SDBB

Is it correct?


thank you
 
The code is very close to work, except for the fact that the command "S" takes the value on the RLO and sends it to the address you specify (check Step7 help). In this case, you have

"S DBX [#Generic_Pointer]"

That instruction by itself won't work, unless you set the RLO to "1" on the previous instruction.

Also, the code will go into an infinite loop. Why? After you Load the number of bits, and then the instruction LOOP decrements, you don't update the value of the number_of_bits. If you slowly see it, that tag will always be 20.

Therefore (for 20 bits):

L P#0.0
T #Generic_Pointer
AUF DB9

L 20
SDBB: T #number_of_bits_to_be_set //Update value
SET //Set RLO to 1
S DBX [#Generic_Pointer]
L P#0.1
L #Generic_Pointer
+D
T #Generic_Pointer
L #number_of_bits_to_be_set
LOOP SDBB

The tag "Generic_Pointer" should be DWORD and the Number_of_bits should be INT in this case.
 
Last edited:
Your errors have now been pointed out. My own preference is to use address register 1 (AR1) for this type of functionality.

arg.JPG
 
Your errors have now been pointed out. My own preference is to use address register 1 (AR1) for this type of functionality.

I agree, but you have to be carefull if you're also using INOUT parameters and complex data types as Input parameters.
 
I agree, but you have to be carefull if you're also using INOUT parameters and complex data types as Input parameters.

Can you post a specific example highlighting why you have to be careful when using AR1 in conjunction with INOUT parameters ?
 
That's it.

Using AR1:
- On FCs, the PLC uses DB and AR1 registers to address the complex data types as parameters (DATE_AND_TIME, ARRAY, STRUCT).
- On FBs, the PLC uses DI and AR1 registers to address any complex data types as INOUT parameters.

Therefore, if you change AR1 for indirect addressing, you won't be able to access these complex parameters anymore.

Using AR2:
- On FCs, no restrictions.
- On FBs, the PLC uses DI and AR2 registers to address every parameter for the FB, including STAT variables. Thus, if you change AR2 register within an FB, that FB will be kind of useless if you are using parameters.

Solution: for both cases, you must SAVE the AR1 or AR2 registers, and DB or DI registers (depending on the case) to a tag, BEFORE any indirect addressing.
After the indirect addressing routines, you must RESTORE the saved values to the according registers. Only then, you will be able to access the corresponding parameters.

Example for FB and AR2:

TAR2 #AR2_REG // Save AR2 to a tag
L DINO // Load DI to ACCU1
T #DI_REG // Save DI to a tag

*Indirect addressing instructions here. Can't access any parameters in here.*

LAR2 #AR2_REG // Restore AR2
OPN DI[#DI_REG] // Restore DI

** Can access parameters again from here**

Greetings.
 
And in case of FC's save AR1 before calling it, and restore it after calling it.
In FB save AR2 inside it !!!

Best Save AR1 and AR2 before calling FC, FB then do so again inside it and then restore both internally and externally.
:D :D
 
An ANYpointer sample

Hi

I started programming in step7 but I'm not very skilled in it yet... I think that the question at my answer will be very simple for many of you..

I have to set a number of BITS (for example: the first 20 bits of a data block) in a data block using a pointer.


In all the examples that I found on the web, pointers refers to BYTES or WORD, so I don't know how I can increment the pointer.

I struggled for a while and have documented my efforts in the accompanying pdf.

It is as straightforward as possible and shows how to construct a pointer to write structures to a DB. By exchanging the source and target pointers it will read from the DB into the structure.

For sake of simplicity it writes sequentially in a circular manner. But that may easily adapted to suit your needs one you get the idea.

The 'whole' project is also available but it's size is around 680kB which is just too large to upload here.

Best regards, from the Netherlands....
 
*Indirect addressing instructions here. Can't access any parameters in here.*

Ok now cannot use the code

Code:
 LAR1  P#DBX 0.0
      LAR2  P#DBX 10.0


      [COLOR=Red]A     #match2[/COLOR]

      JCN   end1
      L     10
C1:   T     #count

      L     B [AR1,P#0.0]
      L     B [AR2,P#0.0]
      ==I   
      JC    j3
      A     #trigger
      S     #TEMP14
      JU    end1

j3:   +AR1  P#1.0
      +AR2  P#1.0

      L     #count
      LOOP  C1

      SET   
      S     #match_success
      CLR   

end1: NOP   0


here #match2 should not be accessible, right?
Or we can say may or may not be...
In this case how to write the above logic??
 
Do the LAR1 and LAR2 after the JCN.

Save AR2 in a temp DWORD before the LAR2 by using the TAR2 instruction.

After the LOOP restore LAR2 (before the SET instruction and if the code continues).

I presume count is a TEMP but TEMP14 can only be a TEMP or 'M' flag, and trigger has to be a 'M' flag, neither can be an IN, OUT, INOUT or STAT.

If the block ends after the NOP then TEMP14 would have to be an 'M' flag, it can only ne a TEMP if the code continues and it is used before the end of the block.


Just noticed the JU END1, if you do this then AR2 will need to be restored prior to the JU instruction (if the code continues).
 
Last edited:
Ok PeterW
Got your points.

Edited code look like this:
Code:
  A     #match2
      JCN   end1

     [COLOR=Red] TAR2  #store_1[/COLOR]
      LAR1  P#DBX 0.0
      LAR2  P#DBX 10.0


      L     10
C1:   T     #count

      L     B [AR1,P#0.0]
      L     B [AR2,P#0.0]
      ==I   
      JC    j3

      [COLOR=Red]LAR2  #store_1[/COLOR]

      A     #trigger
      S     #TEMP14
      JU    end1

j3:   +AR1  P#1.0
      +AR2  P#1.0

      L     #count
      LOOP  C1

     [COLOR=Red] LAR2  #store_1[/COLOR]

      SET   
      S     #match_success
      CLR   

end1: NOP   0

code continues ..........

With #trigger and #TEMP14 being part of STAT in FB while
#count being part of TEMP in FB.

Will this be a correct method?
 
hey, you beat me this time..... :)

Code:
      A     #match2
      JCN   end1
      LAR1  P#DBX 0.0
      TAR2  #dwAR2Store                 //must be temp variable
      LAR2  P#DBX 10.0
      L     10
C1:   T     #count                      //must be temp variable
      L     B [AR1,P#0.0]
      L     B [AR2,P#0.0]
      ==I   
      JC    j3
      LAR2  #dwAR2Store
      A     #trigger
      S     #TEMP14
      JU    end1
j3:   +AR1  P#1.0
      +AR2  P#1.0
      L     #count
      LOOP  C1
      LAR2  #dwAR2Store
      SET   
      S     #match_success
      CLR   
end1: NOP   0
 
.. in your example you do not need to use AR2, see below:

Code:
      LAR1  P#DBX 0.0
      A     #match2
      JCN   end1
      L     10
C1:   T     #count
      L     B [AR1,P#0.0]
      L     B [AR1,P#10.0]
      ==I   
      JC    j3
      A     #trigger
      S     #TEMP14
      JU    end1
j3:   +AR1  P#1.0
      L     #count
      LOOP  C1
      SET   
      S     #match_success
      CLR   
end1: NOP   0
 

Similar Topics

Hello Everyone, I am making an FB that is going to retrieve information from a Pilz Multi. The information given from the Pilz Multi is made up...
Replies
6
Views
1,429
Hello, I've just came out of a call where a program that was modified two weeks ago threw the processor into fault. The program has been done...
Replies
9
Views
3,470
I'm trying to get a 5069-L306er to talk to a Automation Direct ProSence Controller by using RA's AOI for modbus client but its stuck not getting a...
Replies
9
Views
1,881
The last time I did anything with Wonderware was just after the dot com bust. Boy, I feel old. Anyway, it looks like I will get the chance to...
Replies
3
Views
1,516
Hi guys, Some pointers needed: Having PC with WIN7 pro X64 and Amsamotion clone of 6ES7 972-0CB20-0XA0 cable and need to backup VIPA cpu...
Replies
14
Views
3,120
Back
Top Bottom