S7 Pointer help/ Critique

cjd1965

Lifetime Supporting Member
Join Date
Apr 2007
Location
UK
Posts
1,659
Hi
I am looking into code to set a block of 8 booleans within a datablock, depending on the condition of 8 'flags'. The start bit of the 8 bools can vary, and the flags will be determined earlier within this block so will be temp variables.

The DB is basically a long list of bools

So, if flag1 is on, set the first bit of the db which should be the same bit as in iStartbit (example, bit12 of the db)

Does this look suitable? I am unable to test for several weeks

Cheers



Code:
FUNCTION FC 111 : VOID
TITLE =
VERSION : 0.1
VAR_INPUT
  DataBlockA : BLOCK_DB ; 
  iStartBit : INT ; //0=bit0 ,1=bit, 8=bit 8 etc.
END_VAR
VAR_OUTPUT
END_VAR
VAR_TEMP
flag1 :BOOL ; //flag1-8 needed
  iLoopCount : INT ; 
END_VAR
BEGIN
NETWORK
TITLE 
//// decide status of flags 1..8
 
NETWORK
TITLE =Open DB and setup bit 1 position to the pointer  
OPN   #DataBlockA;
L  #iStartBit; 
LAR1  ; 
TITLE =
L #flag1 
= DBX [AR1,P#0.0] 
L #flag2 
= DBX [AR1,P#0.1]
L #flag3 
= DBX [AR1,P#0.2]
L #flag4 
= DBX [AR1,P#0.3]
L #flag5 
= DBX [AR1,P#0.4]
L #flag6 
= DBX [AR1,P#0.5]
L #flag7 
= DBX [AR1,P#0.6]
L #flag8 
= DBX [AR1,P#0.7]
END_FUNCTION
 
OK thanks for that.... I have been doing loads of Mitsubishi lately. Will the pointer side work ok? I assume it will because you didnt say otherwise

Cheers
 
You original description states you wish to set the bits whereas your implementation copies the logic state of the inputs to the DB - can you clarify ?
 
Hi I wish to copy the flag 1-8 status to a 'slot' in the db starting at the bit iStartBit, so if iStartbit was 10 the status of flag 10 would go into bit 10 of the DB etc upto flag 8 into bit 18

hope that makes sense

I may get my hands on the programmer on monday to try it in the simulator

Cheers
 
Code works fine once syntax corrected

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

VAR_INPUT
  DataBlockA : BLOCK_DB ; 
  iStartBit : INT ; //0=bit0 ,1=bit, 8=bit 8 etc.
END_VAR
VAR_TEMP
  flag1 : BOOL ; //flag1-8 needed
  flag2 : BOOL ; //flag1-8 needed
  flag3 : BOOL ; //flag1-8 needed
  flag4 : BOOL ; //flag1-8 needed
  flag5 : BOOL ; //flag1-8 needed
  flag6 : BOOL ; //flag1-8 needed
  flag7 : BOOL ; //flag1-8 needed
  flag8 : BOOL ; //flag1-8 needed
  iLoopCount : INT ; 
END_VAR
BEGIN
NETWORK
TITLE =Open DB and setup bit 1 position to the pointer  
      OPN   #DataBlockA; 
      L     #iStartBit; 
      LAR1  ; 
      A     #flag1; 
      =     DBX [AR1,P#0.0]; 
      A     #flag2; 
      =     DBX [AR1,P#0.1]; 
      A     #flag3; 
      =     DBX [AR1,P#0.2]; 
      A     #flag4; 
      =     DBX [AR1,P#0.3]; 
      A     #flag5; 
      =     DBX [AR1,P#0.4]; 
      A     #flag6; 
      =     DBX [AR1,P#0.5]; 
      A     #flag7; 
      =     DBX [AR1,P#0.6]; 
      A     #flag8; 
      =     DBX [AR1,P#0.7]; 
END_FUNCTION
 
Another variation...
Code:
FUNCTION FC 111 : VOID
TITLE =NB ar2 saved prior to calling an fc from an FB
VERSION : 0.1

VAR_INPUT
  DataBlockA : BLOCK_DB ; 
  iStartBit : INT ; //0=bit0 ,1=bit, 8=bit 8 etc.
END_VAR
VAR_TEMP
  abFlags : ARRAY  [1 .. 8 ] OF BOOL ; 
END_VAR
BEGIN
NETWORK
TITLE =Open DB and setup bit 1 position to the pointer  
      OPN   #DataBlockA; 
      L     #iStartBit; 
      LAR1  ; 
      LAR2  P##abFlags; 
      A      [AR2,P#0.0]; 
      =     DBX [AR1,P#0.0]; 
      A      [AR2,P#0.1]; 
      =     DBX [AR1,P#0.1]; 
      A      [AR2,P#0.2]; 
      =     DBX [AR1,P#0.2]; 
      A      [AR2,P#0.3]; 
      =     DBX [AR1,P#0.3]; 
      A      [AR2,P#0.4]; 
      =     DBX [AR1,P#0.4]; 
      A      [AR2,P#0.5]; 
      =     DBX [AR1,P#0.5]; 
      A      [AR2,P#0.6]; 
      =     DBX [AR1,P#0.6]; 
      A      [AR2,P#0.7]; 
      =     DBX [AR1,P#0.7]; 
END_FUNCTION
 
Interesting.

How would I assign LAR 2? The 'flags' are now in an FB as temps or stats I assume.

My overall goal is to write an analog conditioning block that will provide scaling, broken wire, under range, over range, high, low etc etc alarms

I have a block I wrote in ladder several years ago that I am trying to rewrite as a more easily 'reusable block'. I have FB and DB for multi instance timers so I could use the LAR2 method to read in the status of the timer done bits and to send out the timer run bits.
 
Hi
Still struggling. This is my code to get an input param (int) and turn on the datablock bit that corresponds.



Code:
//// take 1 from the head address  (1 = DBX0.0,2=DBX0.1 ...........128=DBX15.7)
      L     #AlarmHeadAddress           //// int, input par used to offset bit (1 for now)
      L     1
      -I    
      T     #temp1                      //dword
//// add the start address and the headaddress(-1) together to get the integer 1-128
      L     #MR_Start                   /// MR_Start = 0 to start with
      L     #temp1
      +D    
      T     #temp2                      /// dword, target bit 1-128

//// divide by 8 to get the byte
      L     #temp2
      L     8                           // 8 bits per byte
      /D                                // divide to find byte number
      T     #Byte_Number
//// mod by 8 to get the bit
      L     #temp2
      L     8                           // 8 bits per byte
      MOD                               // divide to find byte number
      T     #Bit_Number
//// format the pointer
      L     #Byte_Number                // Shift Right 3 times to get the byte 
      SLD   3
      L     #Bit_Number                 // Add the bit
      +D                                //here you get pointer
      LAR1                              // transfer to AR1 
//// open the DB and set the BIT 
      OPN   #DataBlock
////// condition true
// A xxxx
      =     DBX [AR1,P#0.0]
////// condition false
// AN xxxx
//      R     DBX [AR1,P#0.0]
 
Many thanks for the example code

I got mine working but yours is a lot slicker and more efficient coding.

Q1 - Is FC1 redundant... cant see it used anywhere

Q2 - Also instead of = to set the relevant bit i can use A to read the bit..correct?

Q3 - If I wanted to add an bool bit for status to the INPUT side of FC2 to turn on or rest the bit it would be

A status
= P#..........

(inside FC2)

Cheers
 

Similar Topics

i write code STL and simulate but result Q2.7 not set , i dont know why :scratch: , help me plz thanks LAR1 P#Q2.7 S [AR1,P#0.0] or LAR P#2.7...
Replies
3
Views
1,703
Hi Folks, I'm having problems completing a FB and suspect my pointer programming maybe the issue. The purpose of the FB is to transfer a...
Replies
13
Views
10,616
I just want to set db2.dbx0.0 when q100.0 is on, just trying to learn pointer. Please help what i m doing wrong. thank for help..... L 2...
Replies
8
Views
1,650
Hi all, I have the following FC: FC100 INPUT TRACK_NR := dint FUNCTIE_NR := dint BITJE := bool INPUT/OUTPUT FUNCTIE_WOORDEN :=...
Replies
5
Views
2,055
Hello I am a rather newly hatched PLC programmer who just ran into a problem. Hopefully someone out there can give me a hint on how to move on...
Replies
5
Views
6,847
Back
Top Bottom