codevision union question

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi freaks,

Can anyone please explain to me why it isn't possible to do the following in codevision:

union {
   struct {
      uint8_t b0:1;  
      uint8_t b1:1; 
      uint8_t b2:1; 
      uint8_t b3:1; 
      uint8_t b4:1; 
      uint8_t b5:1; 
      uint8_t b6:1; 
      uint8_t b7:1; 
   };
   uint8_t val;
}x;
 

I know it is possible to do it when the struct is declared outside the union. But then I have to do:

x.someName.b0 or x->someName.b0

but what I want is:
x.b0 or x->b0

Thanks in advance

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am not a CV user, but this question triggered me and should be of general interest. I suspect that you can not have anonymous structs in unions "C" (it is not allowed in C++). One reason for this would be that if they where allowed they could only be so in the special case where no two anonymous structs in the same union would have conflicting names.

Eg. generally allowing anonymous structs would create problems like

union {
  struct{
    int a;
  }
  struct {
    float a;
  }
}

How would you discern between a and a?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'll need to leave the topic of anonymous structures/unions to the purists [in CV and elsewhere--I just don't find myself using complex data structures in my normal (Mega8-Mega32-class) AVR apps nearly as much as I did as when PC/DOS programming in past lives].

But I've use the following construct to advantage in several apps using CV "bit" variables, and re-mapping banks of 8 for byte- and word-level access. Full disclosure: You need to know (and verify in the .MAP file) that R2 is really your first bank, etc.

/*
 * 	Bit Banks--byte-wide bit manipulation
 *	=====================================
 *
 *	Can be used for banks of bits such as shift registers,
 *	keyswitch inputs, etc.
 *
 *	Ensure that these banks are on known byte boundaries if it
 *	is desired to use the byte-wide access declared below them.
 *
 */

// Bank 0
bit	bank0_0;		// R2 Bit 0 (least significant)
bit	bank0_1;
bit	bank0_2;
bit	bank0_3;
bit	bank0_4;
bit	bank0_5;
bit	bank0_6;
bit	bank0_7;		// R2 Bit 7 (most significant)

volatile unsigned char bank0 @0x02;		// force to R2.  Note that CodeVision
                                        // generates LDS/STS to this.

// Bank 1
bit	bank1_0;		// R3 Bit 0 (least significant)
bit	bank1_1;
bit	bank1_2;
bit	bank1_3;
bit	bank1_4;
bit	bank1_5;
bit	bank1_6;
bit	bank1_7;		// R3 Bit 7 (most significant)

volatile unsigned char bank1 @0x03;		// force to R3.  Note that CodeVision
										// generates LDS/STS to this.
volatile unsigned int word0 @0x02;		// force to R2.
										// Remap of bank1(high):bank0(low).

Recent versions can force register operations instead of the LDS/STS:

register unsigned char bank0 @0x02;		// force to R2.  
register unsigned char bank1 @0x03;		// force to R3.
register unsigned int word0 @0x02;		// force to R2.
	// Remap of bank1(high):bank0(low).

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Mon. Sep 10, 2007 - 09:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Johan is correct. It's not allowed in C99, and not in CodeVision.

The best place for CodeVison specific questions would be the CodeVison AVR Yahoo group; http://tech.groups.yahoo.com/group/codevisionavr/.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
register unsigned char bank0 @0x02;      // force to R2. 
register unsigned char bank1 @0x03;      // force to R3.
register unsigned int word0 @0x02;      // force to R2.
   // Remap of bank1(high):bank0(low).

I'm glad to see that I'm not the only one doing this. It is very handy for interrupts where you want to snatch a 16 bit timer capture value one byte at a time.

official AVR Consultant
www.veruslogic.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I'm glad to see that I'm not the only one doing this. It is very handy for interrupts where you want to snatch a 16 bit timer capture value one byte at a time.

I don't use it for that; only with bit/byte bank manipulation. I'll use "vanilla" register variables for other work so I don't need to worry about the address space overlap and the resultant warning.

Unless you dedicate these registers to only ISR and don't need to save them first, then there isn't much advantage vs. letting the compiler PUSH the working registers is there? [Unless the particular ISR(s) are the critical path of the program and one can "afford" to dedicate global register variables--a scarce and valued commodity--for just a reference or two.]

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.