S7 Pointers/ For Next loops

cjd1965

Lifetime Supporting Member
Join Date
Apr 2007
Location
UK
Posts
1,659
Hi
I am starting to look into S7 pointers

I have 3 data blocks in which i wish to compare bits.

Below (ladder logic made to source file) is a basic list of a single alarm construct that i wish to
whizz through 128 times, incrementing the datablocks bit address each time. I havent dont it below but i intend to use an input variable for the length (128)

Essentially, How do i index the datablock bit and is a for next loop style construct ok like this

Next:
{my code}
Loop


FUNCTION FC 123 : VOID
TITLE =
VERSION : 0.1
VAR_OUTPUT
Warning : BOOL ;
Critical : BOOL ;
Buzzer : BOOL ;
END_VAR
VAR_TEMP
On : BOOL ;
END_VAR
BEGIN
NETWORK
TITLE =Clear the blocks internal bits ready for processing
R #Critical;
R #Warning;
R #Buzzer;
NETWORK
TITLE =Alarm001 is Active
A DB10.DBX0.0; //alarm bit is active
= L 1.0;
A L 1.0;
A DB14.DBX0.0; //critical alarm bit
S #Critical;
A L 1.0;
AN DB14.DBX0.0; //not critical so warning only
S #Warning;
A L 1.0;
AN DB12.DBX0.0; //alarm acked
S #Buzzer;
/// Alarm001 has cleared, is acknowledged, clear acknowledge
AN DB10.DBX0.0;
A DB12.DBX0.0;
R DB12.DBX0.0;
END_FUNCTION
 
Here's one implementation. This block is passed the data blocks involved and the bit offset (starting from DBX0.0) and uses address register 1 (AR1) to perform the indirect addressing. Note that your reset instructions need to ensure the RLO is true, hence I've added the set. I've also replaced L1.0 with a named temp variable called Active. The key thing to remember when accessing different data blocks is that you need to open the correct data block each time.


Code:
FUNCTION FC 123 : VOID
TITLE =
VERSION : 0.1
 
VAR_INPUT
DataBlockA : BLOCK_DB ; 
DataBlockB : BLOCK_DB ; 
DataBlockC : BLOCK_DB ; 
iBitOffset : INT ; 
END_VAR
VAR_OUTPUT
Warning : BOOL ; 
Critical : BOOL ; 
Buzzer : BOOL ; 
END_VAR
VAR_TEMP
Active : BOOL ; 
END_VAR
BEGIN
NETWORK
TITLE =Clear the blocks internal bits ready for processing
	 SET ; 
	 R	 #Critical; 
	 R	 #Warning; 
	 R	 #Buzzer; 
NETWORK
TITLE =bit offset into addresss register
	 L	 #iBitOffset; 
	 LAR1 ; 
NETWORK
TITLE =Alarm is Active
	 OPN #DataBlockA; 
	 A	 DBX [AR1,P#0.0]; //alarm bit is active 
	 =	 #Active; 
	 A	 #Active; 
	 OPN #DataBlockC; 
	 A	 DBX [AR1,P#0.0]; //critical alarm bit 
	 S	 #Critical; 
	 A	 #Active; 
	 OPN #DataBlockC; 
	 AN	DBX [AR1,P#0.0]; //not critical so warning only
	 S	 #Warning; 
	 A	 #Active; 
	 OPN #DataBlockB; 
	 AN	DBX [AR1,P#0.0]; //alarm acked
	 S	 #Buzzer; 
	 OPN #DataBlockA; 
	 AN	DBX [AR1,P#0.0]; 
	 OPN #DataBlockB; 
	 A	 DBX [AR1,P#0.0]; 
	 OPN #DataBlockB; 
	 R	 DBX [AR1,P#0.0]; 
END_FUNCTION
 
Thanks for your reply.
When I get time at the weekend I will try and decipher it

Looking at it quickly I still need to add a loop to load the iOffset variable from 1 to 128... is this correct, I havent got my laptop here to try in a simulator.

I am very grateful for your personal assistance over the last few months.

Cheers
 
Last edited:
If all the bits are the same I would do it in word or double word format to speed it up.

For example 1 double word = 32 bits

// First AND the alarms and the Critical bits, if result
// not equal to zero then its critical
L DB10.DBD0
L DB14.DBD0
AD
L 0
<>D
S #Critical

// Then AND the alarms withe the inverse of the Critical
// bits, if not equal to zero then its a warning.
L DB10.DBD0
L DB14.DBD0
INVD
AD
L 0
<>D
= #Warning

// Third AND the alarms and the inverted ack bits,
// if result not equal to zero then sound buzzer
L DB10.DBD0
L DB12.DBD0
INVD
AD
L 0
<>D
S #Buzzer

// Maintain Ack whilst the alarm still exists
L DB10.DBD0
L DB12.DBD0
AD
T DB12.DBD0
 
Hi Peter

Thanks for the tip, my original STL code (not shown here) was DW as you describe. Thanks anyway, all advice gratefully received.

I am attempting to make a standard alarm checking block that can handle an infinite number of boolean alarms (variable input of function) so i have split it into basics to try and learn more STL and about pointers at the same time. I appreciate the scan time issues so i think my task maybe to convert the boolen model to DW as an exercise once i getthe boolean one working.

Cheers
 
The way I've shown was something similar to code I'd come across in S5 some years ago.

The DW or DD can be accessed via pointers as well, just would require less loops.
 
From your first post I assumed you would be doing the looping in the block that was calling FC123. Anyway, here's the revised block with the looping being done inside FC123.

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

VAR_INPUT
  DataBlockA : BLOCK_DB ; 
  DataBlockB : BLOCK_DB ; 
  DataBlockC : BLOCK_DB ; 
  iStartBit : INT ; //0=bit0 ,1=bit, 8=bit 8 etc.
  iNumberofBits : INT ; 
END_VAR
VAR_OUTPUT
  Warning : BOOL ; 
  Critical : BOOL ; 
  Buzzer : BOOL ; 
END_VAR
VAR_TEMP
  Active : BOOL ; 
  iLoopCount : INT ; 
END_VAR
BEGIN
NETWORK
TITLE =Clear the blocks internal bits ready for processing
	  SET   ; 
	  R	 #Critical; 
	  R	 #Warning; 
	  R	 #Buzzer; 
NETWORK
TITLE =bit offset into addresss register and loop point
	  L	 #iStartBit; 
	  LAR1  ; 
	  L	 #iNumberofBits; 
A:	T	 #iLoopCount; 
NETWORK
TITLE =Alarm is Active
	  OPN   #DataBlockA; 
	  A	 DBX [AR1,P#0.0]; //alarm bit is active 
	  =	 #Active; 
	  A	 #Active; 
	  OPN   #DataBlockC; 
	  A	 DBX [AR1,P#0.0]; //critical alarm bit 
	  S	 #Critical; 
	  A	 #Active; 
	  OPN   #DataBlockC; 
	  AN	DBX [AR1,P#0.0]; //not critical so warning only
	  S	 #Warning; 
	  A	 #Active; 
	  OPN   #DataBlockB; 
	  AN	DBX [AR1,P#0.0]; //alarm acked
	  S	 #Buzzer; 
	  OPN   #DataBlockA; 
	  AN	DBX [AR1,P#0.0]; 
	  OPN   #DataBlockB; 
	  A	 DBX [AR1,P#0.0]; 
	  OPN   #DataBlockB; 
	  R	 DBX [AR1,P#0.0]; 
NETWORK
TITLE =loop test
	  +AR1  P#0.1; //increment to next bit
	  L	 #iLoopCount; 
	  LOOP  A; 
END_FUNCTION
 
Thanks again
I intend to analyse this at the weekend so I understand it

Cheers
 

Similar Topics

From memory in C I would have done something like this: char * a = (char*) malloc(1024 * sizeof(char)); char * b = (char*) malloc(1024 *...
Replies
4
Views
4,904
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,465
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,878
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,513
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,112
Back
Top Bottom