On 5/31/06, Jesse Lackey wrote: > Hi - unfortunately I don't think this is possible in CCS, and probably > not in other PIC C compilers, but that's just a guess. > > What you're trying to do is something like this: > > typedef union { > byte Byte; > struct { > word SCME :1; /* Self-clock mode enable */ > word PCE :1; /* COP Enable during Pseudo Stop Bit */ > word PRE :1; /* RTI Enable during Pseudo Stop Bit */ > word :1; > word ACQ :1; /* Acquisition */ > word AUTO :1; /* Automatic Bandwidth Control */ > word PLLON :1; /* Phase Lck Loop On */ > word CME :1; /* Crystal Monitor Enable */ > } Bits; > } PLLCTLSTR; > > (this is from a header file for the freescale MC9S12NE64) > > so that in code you can say: > PLLCTLSTR foobar; (with some compiler-dependent pragma to set foobar to > the correct special chip register) > > then: > foobar= 0xFF; > or > foobar.PRE= 1; > etc. > > but for splitting across two 8-bit registers, the union{} would have to > cover both, and any assignment directly (like foobar= 0x0123) would set > all the pins on both ports. > > Even if this is possible in other compilers, they are going to generate > a number of masks and shifts and so forth, so it won't be smaller/faster > than how you'd do it "by hand". I do not know about CCS but it is quite common to use Union for these kind of things with Hi-Tech PICC and MPLAB C18. For example, the following is from Hi-Tech FAQ. http://www.htsoft.com/support/faqs.php ********************************************** How to map bits onto a RAM variable? Q: I want to be able to access single bits in a byte, but if I try to define a bit variable using the absolute variable construct, e.g. static bit bitvar @ ((unsigned)&bytevar)*8+0; I get a compiler error. How can I do this? A: The short answer is "you can't do this". The absolute variable construct using @ requires an address known at compile time. The long (and more useful) answer will depend on what you're actually trying to do. You may find all you need is some simple macros like: #define testbit(var, bit) ((var) & (1 <<(bit))) #define setbit(var, bit) ((var) |= (1 << (bit))) #define clrbit(var, bit) ((var) &= ~(1 << (bit))) or you may like to define a union, e.g. union both { unsigned char byte; struct { unsigned bit0:1; // etc. } bits; } var; Now you can refer to var.byte or var.bits.bit0 for example. Taking this a bit further, you can use unions to map bit "variables" onto an existing byte, which is getting close to your original question. First up, define a structure type with bits in it: typedef struct { unsigned b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1; } bitv; Now given a char variable, e.g. char myvar; define a "variable" to be a single bit in that char like this: #define mybit (((bitv *)&myvar)->b0) Now you can refer to mybit and it will access bit 0 of myvar. You can use this just like you would use a bit variable defined any other way. The code generated will be just as good as any other way. Example of use: if(mybit) something(); mybit = 1; To streamline the process a little, you can define a helper macro like this: #define _paste(a,b) a##b #define bitof(var,num) (((bitv *)&(var))->_paste(b,num)) Now defining a bit "variable" is done like this: #define x4 bitof(myvar, 4) So now x4 represents bit 4 of myvar. ********************************************** -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist