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

'union' question + CCS Compiler

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



Joined: 24 Aug 2007
Posts: 4

View user's profile Send private message

'union' question + CCS Compiler
PostPosted: Fri Mar 13, 2009 11:15 am     Reply with quote

Hi,

Not sure if this is a general c-syntax/functionality or if its a compiler specific question...

In embedded devices where memory is scarce, I'd like to define a structure that can hold an 8 bit value that will be used to drive an output port. At the same time however, in the code, I would like to access individual bits without using the 8 bit value/mask.

Something like:
Code:
typedef struct
{
    union
    {
        uint8    OutputPortA;
        int1      led0;
        int1      led1;
        int1      led2;
        int1      led3;
        int1      led4;
        int1      led5;
        int1      led6;
        int1      led7;
    } uSelection;

} PORT_A_OUTPUT;

Will the above reserve a single 8 bit chunk, such that setting/clearing any of the individual led bits will automatically update OutputPortA? Similarly, modifying OutputPortA will affect the individual single bit values?

I know most compilers don't operate on single bits so perhaps this is not possible?
Heath



Joined: 21 Dec 2007
Posts: 41

View user's profile Send private message

PostPosted: Fri Mar 13, 2009 12:09 pm     Reply with quote

As a quick answer, look up the #bit directive. Maybe that something like what you're looking for.

I think what you suggest is possible but your code doesn't look right to me. I haven't tested it though.
Ttelmah
Guest







PostPosted: Fri Mar 13, 2009 3:51 pm     Reply with quote

The problem is that the union as shown, will allocate an eight byte chunk as 'OutputPortA', but then 8 single bits _each addressing the same bit_.
In a union, each entity, talks to the same memory. You are showing nine separate entities.
The union syntax, would be:
Code:

    union {
        uint8    WholePort;
        int1      LED[8];
    } PORT_A_OUTPUT;

(You don't need to fiddle around with a structure definition as well - just use the union, and typedef it if needed - though see below for why a structure definition might be used).

The you can access PORT_A_OUTPUT.WholePort to write all eight bits, and PORTA_OUTPUT.LED[0] to LED[7] to access the individual bits.

Even better, you can map this directly to the port register if required. Having defined the variable, then use:
Code:


#byte PORT_A_OUTPUT=0xnnn


Where 'nnn' is the memory address of the port (depends on whether you are using a PIC16, or PIC18).

Now, using an array of bits, requires a V4 compiler. The alternative, is to declare an 8bit structure, and then declare a union of this to the unsigned int8.

Best Wishes
snoop911



Joined: 24 Aug 2007
Posts: 4

View user's profile Send private message

PostPosted: Fri Mar 13, 2009 4:04 pm     Reply with quote

Thanks a bunch!

Not many compilers work with 'bit' types, so adding 'union' into the mix, was my concern. I'll give a try and post how it goes.

BTW, how would the mapping go from the bit to the byte? For example, would LED[7] correspond to the most-significant-bit of the WholePort or would the compiler map LED[0] to that? I'm guessing the same mapping convention would apply if an LED struct it used in place of the array.

Cheers
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