EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Re: BitFields

Started by Paul Curtis April 20, 2005
Hi, 

> I am a 8051-core programmer,recently shifted to
MSP430. 
> Kindly tell me how to use bit fields in MSP430... as I have a 
> habit of frequent bit usage.(If possible give some code examples)

No MSP430 compiler that I know of supports the "bit" type as
you'd find
in an 8051 compiler.  (I might be wrong here, I surveyed MSP430
compilers quite a while ago and haven't kept up with this groovy scene.)

However, all compilers support bitfields, for instance:

struct {
  unsigned int flag0 : 1;
  unsigned int flag1 : 1;
  unsigned int flag2 : 1;
  // etc
} my_packed_bits;

As such, you can then change single bits, e.g.

  my_packed_bits.flag0 = 1;
  my_packed_bits.flag1 = 0;
  my_packed_bits.flag0 ^= 1;

--
Paul Curtis, Rowley Associates Ltd  http://www.rowley.co.uk
CrossWorks for MSP430, ARM, AVR and (soon) MAXQ processors

Beginning Microcontrollers with the MSP430

I don't know... I think this struct is hard do use. 
I use this:

#define Bit1  0x01
#define Bit2  0x02
#define Bit3  0x04
#define Bit4  0x08
#define Bit5  0x10
#define Bit6  0x20
#define Bit7  0x40
#define Bit8  0x81

unsigned char BYTE;
if (BYTE & Bit1){
      //it's true
      BYTE |= Bit2;       //Set bit
}
else{
      //it's false
      BYTE &= ~Bit3;  //Clear bit
}


The code of this program is smaller than using structs.

SPIN
On 4/20/05, Paul Curtis <plc@plc@...> wrote:
> 
> Hi,
> 
> > I am a 8051-core programmer,recently shifted to MSP430.
> > Kindly tell me how to use bit fields in MSP430... as I have a
> > habit of frequent bit usage.(If possible give some code examples)
> 
> No MSP430 compiler that I know of supports the "bit" type as
you'd find
> in an 8051 compiler.  (I might be wrong here, I surveyed MSP430
> compilers quite a while ago and haven't kept up with this groovy
scene.)
> 
> However, all compilers support bitfields, for instance:
> 
> struct {
>   unsigned int flag0 : 1;
>   unsigned int flag1 : 1;
>   unsigned int flag2 : 1;
>   // etc
> } my_packed_bits;
> 
> As such, you can then change single bits, e.g.
> 
>   my_packed_bits.flag0 = 1;
>   my_packed_bits.flag1 = 0;
>   my_packed_bits.flag0 ^= 1;
> 
> --
> Paul Curtis, Rowley Associates Ltd  http://www.rowley.co.uk
> CrossWorks for MSP430, ARM, AVR and (soon) MAXQ processors
> 
> .
> 
> 
> Yahoo! Groups Links
> 
> 
> 
> 
>

Hi,

> I don't know... I think this struct is hard
do use. 
> I use this:
> 
> #define Bit1  0x01
> #define Bit2  0x02
> #define Bit3  0x04
> #define Bit4  0x08
> #define Bit5  0x10
> #define Bit6  0x20
> #define Bit7  0x40
> #define Bit8  0x81

Bit8 looks strange and the usual definitions, e. g. in the linux kernel sources,
do start with 1 as BIT0:

/* Standard bits */
#   ifndef BIT0                 /* ifdef to avoid redefinition; existing
definitions should be ok */
#      define BIT0                (0x1) // 2^0, 1
#   endif
#   ifndef BIT1
#      define BIT1                (0x2)
#   endif
#   ifndef BIT2
#      define BIT2                (0x4)
#   endif
#   ifndef BIT3
#      define BIT3                (0x8)
#   endif
#   ifndef BIT4
#      define BIT4                (0x10)        // 16
#   endif
#   ifndef BIT5
#      define BIT5                (0x20)        // 32
#   endif
#   ifndef BIT6
#      define BIT6                (0x40)        // 64
#   endif
#   ifndef BIT7
#      define BIT7                (0x80)        // 128
#   endif
/* ------------ limit for 8 bit and endianess independence ------------ */
#   ifndef BIT8
#      define BIT8                (0x100)       // 256
#   endif
#   ifndef BIT9
#      define BIT9                (0x200)       // 512
#   endif
#   ifndef BITA
#      define BITA                (0x400)       // 1024, 2^(10)
#   endif
#   ifndef BITB
#      define BITB                (0x800)       // 2048
#   endif
#   ifndef BITC
#      define BITC                (0x1000)      // 4096
#   endif
#   ifndef BITD
#      define BITD                (0x2000)      // 8182
#   endif
#   ifndef BITE
#      define BITE                (0x4000)      // 16384
#   endif
#   ifndef BITF
#      define BITF                (0x8000)      // 32768
#   endif
/* ---- limit for 16 bit, the next is bigger than uint16_t ------ */
#   ifndef BIT10
#      define BIT10               (0x10000L)    // 65536, 2^(16)
#   endif
#   ifndef BIT11
#      define BIT11               (0x20000L)    // 131072
#   endif
#   ifndef BIT12
#      define BIT12               (0x40000L)    // 262144
#   endif
#   ifndef BIT13
#      define BIT13               (0x80000L)    // 524288
#   endif
#   ifndef BIT14
#      define BIT14               (0x100000l)   // 1048576, 2^(20)
#   endif
#   ifndef BIT15
#      define BIT15               (0x200000L)   // 2097152
#   endif
#   ifndef BIT16
#      define BIT16               (0x400000L)   // 4914304
#   endif
#   ifndef BIT17
#      define BIT17               (0x800000L)   // 8388608
#   endif
#   ifndef BIT18
#      define BIT18               (0x1000000L)  // 16777216
#   endif
#   ifndef BIT19
#      define BIT19               (0x2000000L)  // 33554432, 2^(25)
#   endif
#   ifndef BIT1A
#      define BIT1A               (0x4000000L)  // 67108864
#   endif
#   ifndef BIT1B
#      define BIT1B               (0x8000000L)  // 134217728
#   endif
#   ifndef BIT1C
#      define BIT1C               (0x10000000L) // 268435456
#   endif
#   ifndef BIT1D
#      define BIT1D               (0x20000000L) // 536870912
#   endif
#   ifndef BIT1E
#      define BIT1E               (0x40000000L) // 1073741824
#   endif
#   ifndef BIT1F
#      define BIT1F               (0x80000000L) // 2147482648, 2^(31)
#   endif
/* ---- limit for 32 bit, the next is bigger than uint32_t ------ */
#   ifndef BIT20
#      define BIT20               (0x100000000LL)       // 4294967296, 2^(32)
#   endif
#   ifndef BIT21
#      define BIT21               (0x200000000LL)       // 2^(33)
#   endif
#   ifndef BIT22
#      define BIT22               (0x400000000LL)       // 2^(34)
#   endif
#   ifndef BIT23
#      define BIT23               (0x800000000LL)       // 2^(35)
#   endif
#   ifndef BIT24
#      define BIT24               (0x1000000000LL)      // 2^(36)
#   endif
#   ifndef BIT25
#      define BIT25               (0x2000000000LL)      // 2^(37)
#   endif
#   ifndef BIT26
#      define BIT26               (0x4000000000LL)      // 2^(38)
#   endif
#   ifndef BIT27
#      define BIT27               (0x8000000000LL)      // 2^(39)
#   endif
#   ifndef BIT28
#      define BIT28               (0x10000000000LL)     // 2^(40)
#   endif
#   ifndef BIT29
#      define BIT29               (0x20000000000LL)     // 2^(41)
#   endif
#   ifndef BIT2A
#      define BIT2A               (0x40000000000LL)     // 2^(42)
#   endif
#   ifndef BIT2B
#      define BIT2B               (0x80000000000LL)     // 2^(43)
#   endif
#   ifndef BIT2C
#      define BIT2C               (0x100000000000LL)    // 2^(44)
#   endif
#   ifndef BIT2D
#      define BIT2D               (0x200000000000LL)    // 2^(45)
#   endif
#   ifndef BIT2E
#      define BIT2E               (0x400000000000LL)    // 2^(46)
#   endif
#   ifndef BIT2F
#      define BIT2F               (0x800000000000LL)    // 2^(47)
#   endif
#   ifndef BIT30
#      define BIT30               (0x1000000000000LL)   // 2^(48)
#   endif
#   ifndef BIT31
#      define BIT31               (0x2000000000000LL)   // 2^(49)
#   endif
#   ifndef BIT32
#      define BIT32               (0x4000000000000LL)   // 2^(50)
#   endif
#   ifndef BIT33
#      define BIT33               (0x8000000000000LL)   // 2^(51)
#   endif
#   ifndef BIT34
#      define BIT34               (0x10000000000000LL)  // 2^(52)
#   endif
#   ifndef BIT35
#      define BIT35               (0x20000000000000LL)  // 2^(53)
#   endif
#   ifndef BIT36
#      define BIT36               (0x40000000000000LL)  // 2^(54)
#   endif
#   ifndef BIT37
#      define BIT37               (0x80000000000000LL)  // 2^(55)
#   endif
#   ifndef BIT38
#      define BIT38               (0x100000000000000LL) // 2^(56)
#   endif
#   ifndef BIT39
#      define BIT39               (0x200000000000000LL) // 2^(57)
#   endif
#   ifndef BIT3A
#      define BIT3A               (0x400000000000000LL) // 2^(58)
#   endif
#   ifndef BIT3B
#      define BIT3B               (0x800000000000000LL) // 2^(59)
#   endif
#   ifndef BIT3C
#      define BIT3C               (0x1000000000000000LL)        // 2^(60)
#   endif
#   ifndef BIT3D
#      define BIT3D               (0x2000000000000000LL)        // 2^(61)
#   endif
#   ifndef BIT3E
#      define BIT3E               (0x4000000000000000LL)        // 2^(62)
#   endif
#   ifndef BIT3F
#      define BIT3F               (0x8000000000000000LL)        // 2^(63),
9223372036854775807
#   endif
/* ---- limit for 64 bit, the next is bigger than uint64_t ------ */
#   ifndef BIT40
#      define BIT40               (0x10000000000000000LL)       // 2^(64),
18446744073709551616
#   endif

Regards,

Rolf





Thanks. 

SPIN


On 4/20/05, rolf.freitag@rolf...
<rolf.freitag@rolf...> wrote:
> 
> Hi,
> 
> > I don't know... I think this struct is hard do use.
> > I use this:
> >
> > #define Bit1  0x01
> > #define Bit2  0x02
> > #define Bit3  0x04
> > #define Bit4  0x08
> > #define Bit5  0x10
> > #define Bit6  0x20
> > #define Bit7  0x40
> > #define Bit8  0x81
> 
> Bit8 looks strange and the usual definitions, e. g. in the linux kernel
sources,
> do start with 1 as BIT0:
> 
> /* Standard bits */
> #   ifndef BIT0                 /* ifdef to avoid redefinition; existing
definitions should be ok */
> #      define BIT0                (0x1) // 2^0, 1
> #   endif
> #   ifndef BIT1
> #      define BIT1                (0x2)
> #   endif
> #   ifndef BIT2
> #      define BIT2                (0x4)
> #   endif
> #   ifndef BIT3
> #      define BIT3                (0x8)
> #   endif
> #   ifndef BIT4
> #      define BIT4                (0x10)        // 16
> #   endif
> #   ifndef BIT5
> #      define BIT5                (0x20)        // 32
> #   endif
> #   ifndef BIT6
> #      define BIT6                (0x40)        // 64
> #   endif
> #   ifndef BIT7
> #      define BIT7                (0x80)        // 128
> #   endif
> /* ------------ limit for 8 bit and endianess independence ------------ */
> #   ifndef BIT8
> #      define BIT8                (0x100)       // 256
> #   endif
> #   ifndef BIT9
> #      define BIT9                (0x200)       // 512
> #   endif
> #   ifndef BITA
> #      define BITA                (0x400)       // 1024, 2^(10)
> #   endif
> #   ifndef BITB
> #      define BITB                (0x800)       // 2048
> #   endif
> #   ifndef BITC
> #      define BITC                (0x1000)      // 4096
> #   endif
> #   ifndef BITD
> #      define BITD                (0x2000)      // 8182
> #   endif
> #   ifndef BITE
> #      define BITE                (0x4000)      // 16384
> #   endif
> #   ifndef BITF
> #      define BITF                (0x8000)      // 32768
> #   endif
> /* ---- limit for 16 bit, the next is bigger than uint16_t ------ */
> #   ifndef BIT10
> #      define BIT10               (0x10000L)    // 65536, 2^(16)
> #   endif
> #   ifndef BIT11
> #      define BIT11               (0x20000L)    // 131072
> #   endif
> #   ifndef BIT12
> #      define BIT12               (0x40000L)    // 262144
> #   endif
> #   ifndef BIT13
> #      define BIT13               (0x80000L)    // 524288
> #   endif
> #   ifndef BIT14
> #      define BIT14               (0x100000l)   // 1048576, 2^(20)
> #   endif
> #   ifndef BIT15
> #      define BIT15               (0x200000L)   // 2097152
> #   endif
> #   ifndef BIT16
> #      define BIT16               (0x400000L)   // 4914304
> #   endif
> #   ifndef BIT17
> #      define BIT17               (0x800000L)   // 8388608
> #   endif
> #   ifndef BIT18
> #      define BIT18               (0x1000000L)  // 16777216
> #   endif
> #   ifndef BIT19
> #      define BIT19               (0x2000000L)  // 33554432, 2^(25)
> #   endif
> #   ifndef BIT1A
> #      define BIT1A               (0x4000000L)  // 67108864
> #   endif
> #   ifndef BIT1B
> #      define BIT1B               (0x8000000L)  // 134217728
> #   endif
> #   ifndef BIT1C
> #      define BIT1C               (0x10000000L) // 268435456
> #   endif
> #   ifndef BIT1D
> #      define BIT1D               (0x20000000L) // 536870912
> #   endif
> #   ifndef BIT1E
> #      define BIT1E               (0x40000000L) // 1073741824
> #   endif
> #   ifndef BIT1F
> #      define BIT1F               (0x80000000L) // 2147482648, 2^(31)
> #   endif
> /* ---- limit for 32 bit, the next is bigger than uint32_t ------ */
> #   ifndef BIT20
> #      define BIT20               (0x100000000LL)       // 4294967296,
2^(32)
> #   endif
> #   ifndef BIT21
> #      define BIT21               (0x200000000LL)       // 2^(33)
> #   endif
> #   ifndef BIT22
> #      define BIT22               (0x400000000LL)       // 2^(34)
> #   endif
> #   ifndef BIT23
> #      define BIT23               (0x800000000LL)       // 2^(35)
> #   endif
> #   ifndef BIT24
> #      define BIT24               (0x1000000000LL)      // 2^(36)
> #   endif
> #   ifndef BIT25
> #      define BIT25               (0x2000000000LL)      // 2^(37)
> #   endif
> #   ifndef BIT26
> #      define BIT26               (0x4000000000LL)      // 2^(38)
> #   endif
> #   ifndef BIT27
> #      define BIT27               (0x8000000000LL)      // 2^(39)
> #   endif
> #   ifndef BIT28
> #      define BIT28               (0x10000000000LL)     // 2^(40)
> #   endif
> #   ifndef BIT29
> #      define BIT29               (0x20000000000LL)     // 2^(41)
> #   endif
> #   ifndef BIT2A
> #      define BIT2A               (0x40000000000LL)     // 2^(42)
> #   endif
> #   ifndef BIT2B
> #      define BIT2B               (0x80000000000LL)     // 2^(43)
> #   endif
> #   ifndef BIT2C
> #      define BIT2C               (0x100000000000LL)    // 2^(44)
> #   endif
> #   ifndef BIT2D
> #      define BIT2D               (0x200000000000LL)    // 2^(45)
> #   endif
> #   ifndef BIT2E
> #      define BIT2E               (0x400000000000LL)    // 2^(46)
> #   endif
> #   ifndef BIT2F
> #      define BIT2F               (0x800000000000LL)    // 2^(47)
> #   endif
> #   ifndef BIT30
> #      define BIT30               (0x1000000000000LL)   // 2^(48)
> #   endif
> #   ifndef BIT31
> #      define BIT31               (0x2000000000000LL)   // 2^(49)
> #   endif
> #   ifndef BIT32
> #      define BIT32               (0x4000000000000LL)   // 2^(50)
> #   endif
> #   ifndef BIT33
> #      define BIT33               (0x8000000000000LL)   // 2^(51)
> #   endif
> #   ifndef BIT34
> #      define BIT34               (0x10000000000000LL)  // 2^(52)
> #   endif
> #   ifndef BIT35
> #      define BIT35               (0x20000000000000LL)  // 2^(53)
> #   endif
> #   ifndef BIT36
> #      define BIT36               (0x40000000000000LL)  // 2^(54)
> #   endif
> #   ifndef BIT37
> #      define BIT37               (0x80000000000000LL)  // 2^(55)
> #   endif
> #   ifndef BIT38
> #      define BIT38               (0x100000000000000LL) // 2^(56)
> #   endif
> #   ifndef BIT39
> #      define BIT39               (0x200000000000000LL) // 2^(57)
> #   endif
> #   ifndef BIT3A
> #      define BIT3A               (0x400000000000000LL) // 2^(58)
> #   endif
> #   ifndef BIT3B
> #      define BIT3B               (0x800000000000000LL) // 2^(59)
> #   endif
> #   ifndef BIT3C
> #      define BIT3C               (0x1000000000000000LL)        // 2^(60)
> #   endif
> #   ifndef BIT3D
> #      define BIT3D               (0x2000000000000000LL)        // 2^(61)
> #   endif
> #   ifndef BIT3E
> #      define BIT3E               (0x4000000000000000LL)        // 2^(62)
> #   endif
> #   ifndef BIT3F
> #      define BIT3F               (0x8000000000000000LL)        // 2^(63),
9223372036854775807
> #   endif
> /* ---- limit for 64 bit, the next is bigger than uint64_t ------ */
> #   ifndef BIT40
> #      define BIT40               (0x10000000000000000LL)       // 2^(64),
18446744073709551616
> #   endif
> 
> Regards,
> 
> Rolf
> 
> 
> .
> 
> Yahoo! Groups Links
> 
> 
> 
> 
>

Hi,

> I don't know... I think this struct is hard
do use. 
> I use this:
> 
> #define Bit1 0x01
> #define Bit2 0x02
> #define Bit3 0x04
> #define Bit4 0x08
> #define Bit5 0x10
> #define Bit6 0x20
> #define Bit7 0x40
> #define Bit8 0x81

Bit8 looks strange and the usual definitions, e. g. in the linux kernel sources,
do start with 1 as BIT0:

/* Standard bits */
# ifndef BIT0 /* ifdef to avoid redefinition; existing definitions should be ok
*/
# define BIT0 (0x1) // 2^0, 1
# endif
# ifndef BIT1
# define BIT1 (0x2)
# endif
# ifndef BIT2
# define BIT2 (0x4)
# endif
# ifndef BIT3
# define BIT3 (0x8)
# endif
# ifndef BIT4
# define BIT4 (0x10) // 16
# endif
# ifndef BIT5
# define BIT5 (0x20) // 32
# endif
# ifndef BIT6
# define BIT6 (0x40) // 64
# endif
# ifndef BIT7
# define BIT7 (0x80) // 128
# endif
/* ------------ limit for 8 bit and endianess independence ------------ */
# ifndef BIT8
# define BIT8 (0x100) // 256
# endif
...
# ifndef BIT3F
# define BIT3F (0x8000000000000000LL) // 2^(63), 9223372036854775807
# endif
...

Regards,

Rolf


I usually use a macro

#define BIT(n)	(1 << (n))

Regards
-Bill Knight
R O SOftWare



Paul...

I think there are three warnings to be remembered here.

1. Each C compiler will have a rule about the field boundary across which 
it will refuse to span a field.

so with

struct{
        field1 : 6 ;
        field2 : 3 
      }

you may find field 1 occupies bits 6 to 8, or bits 8 to 10

2. The struct will normally occupy exactly one native word.

That makes it damned hard to use with a byte-size register. And useless 
for a 32-bit register on a 16-bit architecture machine.
     
3. Most C compilers won't let you assign a value read from a port 
straight to a struct... you will need to use a union

============
what I'd like is a compiler which allows me to say

typedef
struct{
        field1 : 2
        field2 : 6
      } bytesizedstructuretype  ;
bytesizedstructuretype bytesizedstructure @ 0x123245679      

and

typedef 
struct{
        field1 : 2
        field2 : 9
        filed3 : 5
      } wordsizedstructuretype  ;
wordsizedstructuretype wordsizedstructure @ 0x12324567a
      
then do 

bytesizedstructure.field1 = 3 ;

wordsizedstructure.field2 = 0x180 ;

Come back Modula 2, all is forgiven.....

> However, all compilers support bitfields, for
instance:
> 
> struct {
>   unsigned int flag0 : 1;
>   unsigned int flag1 : 1;
>   unsigned int flag2 : 1;
>   // etc
> } my_packed_bits;
> 
> As such, you can then change single bits, e.g.
> 
>   my_packed_bits.flag0 = 1;
>   my_packed_bits.flag1 = 0;
>   my_packed_bits.flag0 ^= 1;
> 


Memfault Beyond the Launch