Step7 - how to initialise an array at run time?

RMA

Member
Join Date
Sep 2004
Location
North of Hamburg, Germany
Posts
2,052
I've got an application where a robot is running 10 objects one after another past a camera so I need to buffer the result of the camera test. This bit is easy enough since I can just AND the falling edge of the camera's data strobe and the result and if the result is OK SET the corresponding bit in the array.

However, in order for this to work I need to initialise the array to 0 at the start of each new run and I can't find any way to this other than to individually reset each element in the array. Apart from the fact that it's too spaghetti-like for my taste even with only ten elements, what are you going to if you've got 100 elements!

Is there really no way reset all the bits in an array at one time?

By the way, the array is a STAT in the IDB and the company I'm doing this for want it to stay there otherwise I'd move it out to Marker memory and define a WORD to access all the bits in one go - can't see any way to do that with a STAT, though.
 
This is easy on a S7

The S7 allows for bit addressing and AR1 and AR2 are bit pointers not byte or word pointers like in other PLCs. I don't have access to my S7 now and LD can probably write this in less than 5 minutes.

Load a pointer to the beginning of the array and then reset each bit in a loop. Increment the pointer by 1 instead of the normal by 8 16 or 32.

A move efficient way required the arrays is based on a 32 bit boundary. As long as the number of bits to be cleared is 32 or more then store a 0 in a dword and increment the pointer by 32 until the number of bits left is less than 32. Then check if the number of bits is 16 or greater and if so then store a 0 at the word at the pointer and increment the pointer by 16. Do the same for bytes and then finally bits.
 
This is easy on a S7

The S7 allows for bit addressing and AR1 and AR2 are bit pointers not byte or word pointers like in other PLCs. I don't have access to my S7 now and LD can probably write this in less than 5 minutes.

Load a pointer to the beginning of the array and then reset each bit in a loop. Increment the pointer by 1 instead of the normal by 8 16 or 32.

A move efficient way required the arrays is based on a 32 bit boundary. As long as the number of bits to be cleared is 32 or more then store a 0 in a dword and increment the pointer by 32 until the number of bits left is less than 32. Then check if the number of bits is 16 or greater and if so then store a 0 at the word at the pointer and increment the pointer by 16. Do the same for bytes and then finally the bits.
 
Have a duplicate Array set to zero in STAT then use SFC20 to copy the duplicate array to the array that needs clearing. As long as you change the size of both arrays, no extra coding will be required to determine the size of the arrays.
 
... This bit is easy enough since I can just AND the falling edge of the camera's data strobe and the result and if the result is OK SET the corresponding bit in the array

Use the assign (=) instead of SET then you will not need to clear the array.
 
Thanks LD, that sounds like a relatively clean and simple solution.

@Peter, thanks for pointing out that source of the problem is the bit-addressing in Step7, I must admit, I hadn't considered that point.

Edit: I can't use the assign because the signals come in one after the other and are not all available at the same time, that's why I need to save them in a buffer to send the result back to the robot after the last test.
 
Last edited:
Use SFC20

Edit: I see that LD got here before me.

The easiest way is to make a duplicate array with blank data in it and use SFC20 to copy the blank data into the working data as required.

Nick

ArrayInit.jpg
 
One could write his own function:

Code:
FUNCTION ResetArray : VOID
TITLE = Resets given array
VERSION : 0.1


VAR_IN_OUT
  DstArray : ANY ;    
END_VAR
VAR_TEMP
  Dwords : INT ;    
  Words : INT ;    
  dbnum : INT ;    
END_VAR
BEGIN
NETWORK
TITLE =

      L     P##DstArray; 
      LAR1  ; 
      L     W [AR1,P#4.0]; 
      T     #dbnum; 
      OPN   DB [#dbnum]; 
      L     W [AR1,P#2.0]; 
      L     4; 
      MOD   ; 
      L     2; 
      /I    ; 
      T     #Words; 
      L     W [AR1,P#2.0]; 
      L     4; 
      /I    ; 
      T     #Dwords; 
      L     D [AR1,P#6.0]; 
      LAR1  ; 
      L     #Dwords; 
      L     0; 
      >I    ; 
      JCN   nodw; 

      TAK   ; 
dw:   T     #Dwords; 

      L     L#0; 
      T     D [AR1,P#0.0]; 
      +AR1  P#4.0; 
      L     #Dwords; 
      LOOP  dw; 

nodw: L     #Words; 
      L     0; 
      >I    ; 
      NOT   ; 
      BEC   ; 

      TAK   ; 
      L     L#0; 
      T     W [AR1,P#0.0]; 

END_FUNCTION
Arrays end at word boundary, so no need to reset "invidual bits&bytes"

ps. plcsim is ***** by behaving like four accu cpu with math instructions.

pss. back to sc2 :D

psssssssssssss. yeah, ld you are right as always :D removed.
 
Last edited:
No need for the loop for words as there is either 0 or 1 words left over after filling the dwords.
 
I use SFC21 "FILL"

Code:
ZERO: L     0
      T     #tZERO

      CALL  "FILL"
       BVAL   :=#tZERO
       RET_VAL:=#tRET
       BLK    :="DB DATA LOG".TRACK_RECORD

TRACK_RECORD ARRAY[1..500] DATA LOG STORAGE AREA
"UDT LOG DATA"
 

Similar Topics

This is the first time I am working with Simatic Manager Step7 as I started my siemens journey with TIA which is pretty easy and do a lot of stuff...
Replies
3
Views
147
When you download a DB, the values get overwritten by what is in the "actual" column in offline DB. Does this happen at the start of the PLC...
Replies
6
Views
143
Hello Inside a FB, I´m trying to transfer a string from a DB to a IN_OUT var that was define as a UDT. The problem is that i can´t determine the...
Replies
4
Views
140
Hi all, I am trying to convert RSLogix 5000 program to Step7. I need to bit shift left my array of double integers for tracking the product on...
Replies
2
Views
527
I have a word in some DB which I want to load to AR1 and use as a pointer. In order to do this I need to write L DBxy.DBW xy SLD 3 LAR1 I...
Replies
3
Views
543
Back
Top Bottom