avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

AW: [avr-gcc-list] Bit Variables


From: Stumpf Michael
Subject: AW: [avr-gcc-list] Bit Variables
Date: Wed, 12 Jun 2002 10:25:15 +0200


But be aware that using bitfields will yield much larger code for single variable access.
Have a look at http://www.avrfreaks.net/phorum/read.php?f=2&i=677&t=676#677

If you need some bit variables and have some spare i/o registers you don't use you
may abuse them as yours with very efficient access.
Which registers may be used depends on your application, others arent't usable at all.

regards
Michael



-----Urspr√ľngliche Nachricht-----
Von: Heinrich Vermeulen [mailto:address@hidden]
Gesendet: Mittwoch, 12. Juni 2002 09:06
An: avr-gcc-list
Betreff: RE: [avr-gcc-list] Bit Variables


You can use bitfields in a structure.  This will also keep all your bits
together, especially when using it as global vars.
E.g.
typedef struct {
 unsigned bit_x : 1;  //bit is called bit_x reserving 1 bit location
 unsigned bit_y : 1;  //bit is called bit_x reserving 1 bit location 
 unsigned bit_z : 1;  //bit is called bit_x reserving 1 bit location
 unsigned bit_a : 1;  //bit is called bit_x reserving 1 bit location
 unsigned bit_b : 2;  //bit is called bit_x reserving 2 bit locations
 unsigned bit_c : 2;  //bit is called bit_x reserving 2 bit locations
 unsigned bit_d : 4;  //bit is called bit_x reserving 4 bit locations
 unsigned bit_e : 4;  //bit is called bit_x reserving 4 bit locations
} BitField;

/*
2 bytes will be reserved for a var of this type (count the bits above)
The number of bits reserved will be:
bytes reserved = nbits/8 + (nbits%8 > 0)
IOW 1 byte for every 8 bits or part thereof
*/

main()
{
  BitField myBitField;

  myBitField.bit_x = 1; //can be assigned a value of 0 to 1 
  myBitField.bit_b = 3; //can be assigned a value of 0 to 3
  myBitField.bit_e = 12; //can be assigned a value of 0 to 15
}

Heinrich Vermeulen


> -----Original Message-----
> From: Michael Bellefeuille [mailto:address@hidden]
> Sent: 11 June 2002 07:42
> To: avr-gcc-list
> Subject: [avr-gcc-list] Bit Variables
>
>
> Hi,
>     What is the best way of handing bit variables?  I found the AVR
> Freaks design note #008, but it is about handling i/o with bitfields.
>
> Michael
>
>
> avr-gcc-list at http://avr1.org
>
avr-gcc-list at http://avr1.org


reply via email to

[Prev in Thread] Current Thread [Next in Thread]