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 CCS Technical Support

Using a RAM structure as a byte array

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



Joined: 07 Mar 2006
Posts: 8
Location: Ashby, MA

View user's profile Send private message Send e-mail Visit poster's website

Using a RAM structure as a byte array
PostPosted: Tue Mar 07, 2006 7:02 pm     Reply with quote

Hi All,

This is probably old hat to some of you, and while I've done it 1,000x in Assembler, I'm unsure of how to go about it in PICC ... Confused

I have a project where I occasionally need to send system status out a serial interface. The data in the status is NOT all the same size, so an array doesn't eliminate my problems:

int16 variable1;
int8 variable2;
int8 variable3;
int8 variable4;
int32 variable5;


I want to be able to access them as individual variables of varying sizes, AND I want to be able to use a pointer to variable1 to send the whole mess out a serial port, in one gulp, high-endian, without having to worry about the byte sizes. This is not a bulk backup to storage; byte order matters at the other end.

The question that I have is, since PICC handles all of the RAM management behind the scenes, how can I guarantee that these five variables will occupy 9 continguous locations in the compiled program? Will a structure alone guarantee this? (The manual and help file aren't much ... er, help on this matter.)

And when I go to set a pointer to the collection, can I just typecast a byte pointer to variable1, and they'll all come out in order? Or am I asking for byte-swapping, invalid data, etc.? The only 100% safe way that I can think of is to define a structure, then create a union with an array of 9 bytes. That seems ludicrous, given that the result is in the form of a.b->c -- C is supposed to be EASIER than Assembler, not harder! Any thoughts?

TIA,

-BW
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 07, 2006 8:50 pm     Reply with quote

Quote:

I want to be able to access them as individual variables of varying sizes,
AND I want to be able to use a pointer to variable1 to send the whole
mess out a serial port, in one gulp, high-endian, without having to worry
about the byte sizes. This is not a bulk backup to storage; byte order
matters at the other end.

16 and 32 bit variables are stored in Lo-Hi format. So if you want
to send them out MSB first, you can't do it by creating a pointer to
the first byte.


Quote:

how can I guarantee that these five variables will occupy 9 continguous
locations in the compiled program? Will a structure alone guarantee this?

In CCS, it will. The default packing alignment is on byte boundaries.
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Wed Mar 08, 2006 8:44 am     Reply with quote

PCM is right. For me I take care of this at PC side
((since it has the horsepower))

To simply send them out in consecutive bytes, you can use a array,,... a struct in your case since they change in size.

Then you can have a ponter to the struct and get all the fun . -> stuff
and a pointer to the buffer you want to send.
take a look at modbus packt struct modbus
http://www.ccsinfo.com/forum/viewtopic.php?t=19306&highlight=modbus
drawbars



Joined: 07 Mar 2006
Posts: 8
Location: Ashby, MA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Mar 10, 2006 8:20 am     Reply with quote

Hi PCM,

Thanks. It looks like a struct is all I need to keep the bytes in line.

And while I said "high-endian," I meant low-endian (the other high-endian! Wink), so that should work out OK, too.


Regards,

-BW
Bruce Wahler
Design Consultant
Ashby Solutions™
http://consult.ashbysolutions.com
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