CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Question about Union declaration/naming/using

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Sergeant82d



Joined: 01 Nov 2009
Posts: 55
Location: Central Oklahoma

View user's profile Send private message

Question about Union declaration/naming/using
PostPosted: Fri Apr 01, 2011 11:47 am     Reply with quote

On an old board thread (link below), there is a post from RayJones, regarding Unions for the HMC6352 Compass Driver. I have a question about the line which says:
Code:

Val16 heading;

Is this line declaring a copy/iteration of the Val16 union type, named heading? And then the next few lines:
Code:

heading.bytes.msb = i2c_read();
heading.bytes.lsb = i2c_read(0);
return heading.word;

call and store the compass data in the temporary union thus declared? I'm just trying to understand what is going on... I will be using this compass module (from Sparkfun) whenever it arrives next week and wanted to get a handle on the driver.

Thanks,
Brad

http://www.ccsinfo.com/forum/viewtopic.php?t=35913&highlight=hmc6352

Code:

typedef union {
  int16 word;
  struct {
    int8 lsb;
    int8 msb;
  } bytes;
} Val16 ;

int16 example1()
{
  Val16 heading;

  heading.bytes.msb = i2c_read();
  heading.bytes.lsb = i2c_read(0);

  return heading.word;
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 01, 2011 12:21 pm     Reply with quote

Quote:

Is this line declaring a copy/iteration of the Val16 union type, named heading?
Code:
Val16 heading;

Yes. The union is "typedef'ed". Then an instance of it is created in
the line above. The instance is called 'heading'. In other words,
RAM memory is allocated for it, and it exists in the program.

Quote:

And then the next few lines:
Code:
heading.bytes.msb = i2c_read();
heading.bytes.lsb = i2c_read(0);
return heading.word;

call and store the compass data in the temporary union thus declared? I'm just trying to understand what is going on...

Yes, the first two lines read the i2c data and put it into the 'msb' and 'lsb'
bytes of the union. Then, when that's completed, the function returns
the two bytes, but it returns them packed into a 16-bit word.
Sergeant82d



Joined: 01 Nov 2009
Posts: 55
Location: Central Oklahoma

View user's profile Send private message

PostPosted: Fri Apr 01, 2011 7:59 pm     Reply with quote

Thank you
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group