Programming error

...

What should I change in my code then ?
I don't understand what the problem is now...

L D[AR2 said:
Not quite - Your function block will not work correctly if you use it as a multiple instance FB as you are not adding AR2 (the base address of the instance data) to AR1
 
You could try to call your FB twice times from another FB as a multiple instance call and see what happens.

The key point is this instruction:

Code:
LAR1  P##bit_0; // Save state of the 16 bits

In your particular function block, as bit_0 is the first bit of input parameters, this instruction will result in the value dw#16#8500_0000 (i.e. P#DIX0.0) being loaded into AR1.

Now if you have called the FB with a unique instance DB, the data in the instance DB will start at DBX0.0 and when the FB is called, AR2 will be set to P#0.0, so, in this case
L DIW[AR1,P#0.0] is actually accessing DIW0 - this is the correct location.

If a multiple instance FB is used, things change. Lets assume that you add the FB to the static area of the calling FB and the data area for your FB starts at DIX2.0 - this means that the absolute address of bit_0 is DIX2.0

As already stated, your code will execute
L DIW[AR1,P#0.0] resulting in the access to DIW0 - clearly the wrong address in this new example. When the FB is called, the editor "knows" that the instance data starts at DIX2.0 so, before the call, AR2 is set to +P#2.0

The trick is to load ar1 with ar2 and then add the pointer to the static variable on to that, e.g.

Code:
LAR1 AR2   //AR2 --> AR1
L P#bit_0  //pointer to stat variable
+AR1	   //add to base address of instance
L DIW[AR1,P#0.0]

This needs to be done for each indirect access to an interface variable (i.e. not a temp) in an FB.
 
hey

I think I understand what you mean now...

hv.JPG


In this case it wouldn't work... right ?

But, I never do such things. If I use the FB twice in a program in different FC's, with different DB instances, that wouldn't be a problem I guess ?
But to make it work like on the snapshot I should write your code:

Code:
LAR1 AR2 //AR2 --> AR1
L P#bit_0 //pointer to stat variable
+AR1	 //add to base address of instance
L DIW[AR1,P#0.0]

?

L D[AR2 said:
You could try to call your FB twice times from another FB as a multiple instance call and see what happens.

The key point is this instruction:

Code:
LAR1 P##bit_0; // Save state of the 16 bits

In your particular function block, as bit_0 is the first bit of input parameters, this instruction will result in the value dw#16#8500_0000 (i.e. P#DIX0.0) being loaded into AR1.

Now if you have called the FB with a unique instance DB, the data in the instance DB will start at DBX0.0 and when the FB is called, AR2 will be set to P#0.0, so, in this case
L DIW[AR1,P#0.0] is actually accessing DIW0 - this is the correct location.

If a multiple instance FB is used, things change. Lets assume that you add the FB to the static area of the calling FB and the data area for your FB starts at DIX2.0 - this means that the absolute address of bit_0 is DIX2.0

As already stated, your code will execute
L DIW[AR1,P#0.0] resulting in the access to DIW0 - clearly the wrong address in this new example. When the FB is called, the editor "knows" that the instance data starts at DIX2.0 so, before the call, AR2 is set to +P#2.0

The trick is to load ar1 with ar2 and then add the pointer to the static variable on to that, e.g.

Code:
LAR1 AR2 //AR2 --> AR1
L P#bit_0 //pointer to stat variable
+AR1	 //add to base address of instance
L DIW[AR1,P#0.0]

This needs to be done for each indirect access to an interface variable (i.e. not a temp) in an FB.
 
Yes you've got it. If you create a unique instance DB for each FB call then AR2 will always be zero at the FB call - if you use multiple instances it will not be and you do have to add AR2
 

Similar Topics

I have a customer who had a faulty Siemens OP7. They sourced a replacement. They have the original program. I downloaded the program to the...
10
Replies
4,067
Views
Hello. I first off I wanna say that I am completely new to PLCs and am trying to help fix my Uncles hydraulic press machine that uses a Mitsubishi...
2
Replies
4,612
Views
Hello all I have an internal error on S7-416 PLC. You can see the diagnostic in the attached file. Can someone explain me the module number in...
5
Replies
2,390
Views
This problem is a little easy for this board but it is driving me mad so i can use the help. I'm using a PID FB from the TwinCAT PLC Library...
2
Replies
2,793
Views
I want to use register 8107 of a Sy/Max 400 to point to an error register stack but not sure how to program the rung to reserve this 16 register...
0
Replies
2,066
Views
Top