Sign in to follow this  
Followers 0
HelGaa

Union

5 posts in this topic

Hello!

I am trying to get hold of the bits in a word, i have read that union is the way to go.

My problem is that i cant figure out how union works. I have made a union between a word (16bit) and an array(0...15). The way I understood this was that the word now automaticly updates the array so that when the least significant bit in the word should update array[00] and so on.

This is not happening. When I monitor the variables I can see the word change, but nothing happens with the array.

Have I misunderstood the consept and what should I do to get hold of the bits?

Share this post


Link to post
Share on other sites

Did you do everything with respect to Unions as I suggested in this post:

Here is my union definition:

Bz4mK8kDuq+PAAAAAElFTkSuQmCC

Here is my variable definition:

1W5ZRiGYRim67DGzDAMwzBBBAtmhmEYhgkiWDAzD

Here is my simulated program (all that I did in simulation was to set on Bit00 of the array):

wM19YpNMzNtZAAAAABJRU5ErkJggg==

Note that the value of UnionTesting.WordVal = 0001 Hex.  As expected, the least significant bit turned on when I set bit 0 on.

 

Share this post


Link to post
Share on other sites

Thank you for your help!

I had the global variables all wrong!

Share this post


Link to post
Share on other sites

Hi,

It doesn't seem to be possible to use the Union inside a Function Block. Am I right?

Share this post


Link to post
Share on other sites

You can use a union as an internal variable, but not as an input or output.  So, here is what you do:

Let's say this is your union:

5f772d4ec30ca_UnionDef.jpg.89ea1446cda0f

You could then create an internal variable in the FB that is of the union type like this:

UnionInternal.jpg.27902eb0ee83b20c4baa38

Then you can pass a portion of the union into the FB as an input (Word Input in this case) and map it to the internal union variable, like this:

UnionProg.jpg.f0a8675a34aef7e33b9c53a05e

Then when you use the FB in your code, you just choose the WordLevel portion of the union that you want to input into the FB, like this:

UnionFBUse.jpg.70425d0ded98137042e4c6fb6

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0