Structure bit field

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Structure bit field

464 Views
Aswin_5232
Contributor II
Hi, I am using CodeWarrior 5.2 and the controller is MC9S12DG256B.
When I am assigning each structure element with values and finally reading the overall value using union it is giving different values. how to solve this issue. I think there is difference in byte ordering.
typedef union
{
  unsigned long  w;
struct  {
    unsigned long disp_comp_cal : 2;//0
    unsigned long disp_home     : 2;//2
    unsigned long disp_trips    : 2;//4
    unsigned long disp_fuel     : 2;//6
    unsigned long disp_hours    : 2;//8
    unsigned long disp_service  : 2;//10
    unsigned long disp_diags    : 2;//12
    unsigned long disp_slftst   : 2;//14   
    unsigned long disp_cust_set : 2;//16
   
  }b;
}attrib2;
0 Kudos
Reply
3 Replies

285 Views
Aswin_5232
Contributor II

Hi Lama, when I am changing the project settings this issue get resolved but values read from different PORTS are also getting reversed. Any other solution for this. 

0 Kudos
Reply

417 Views
Aswin_5232
Contributor II

Hi Lama,

Thanks it is working now.

0 Kudos
Reply

426 Views
lama
NXP TechSupport
NXP TechSupport
 
 
 

Hi,

look into attached file to see solution.

You should think about compiler setup .... look into the help or manuals ... keyword=bitfield.

Your issue is order of bits in the bitfield.

Best regards,

Ladislav

 

My test code:


#include "derivative.h"      /* derivative-specific definitions */
 
//******************************************************************************
typedef union
{
  unsigned long  w;
struct  
  {
    unsigned char disp_comp_cal : 2;//0
    unsigned char disp_home     : 2;//2
    unsigned char disp_trips    : 2;//4
    unsigned char disp_fuel     : 2;//6
    unsigned char disp_hours    : 2;//8
    unsigned char disp_service  : 2;//10
    unsigned char disp_diags    : 2;//12
    unsigned char disp_slftst   : 2;//14   
    unsigned char disp_cust_set : 2;//16
    unsigned int  dummy         : 14;
   
  }b;
}attrib2;
//******************************************************************************
 
void main(void) 
{
 
  attrib2 attrib22;
 
  attrib22.w = 0x00UL;
 
  attrib22.b.disp_comp_cal =2;
  attrib22.b.disp_home     =1;
  attrib22.b.disp_trips    =3;
  attrib22.b.disp_fuel     =1;
  attrib22.b.disp_hours    =2;
  attrib22.b.disp_service  =1;
  attrib22.b.disp_diags    =3;
  attrib22.b.disp_slftst   =1;
  attrib22.b.disp_cust_set =1;  
  attrib22.b.dummy         =0x3FFFF;  
 
  // expected 0B1001 1101 1001 1101 0111 1111 1111 1111
  //          0x9D9D 7FFF 
 
  DDRAB = 0xFFFF;
 
  //------------------------
  for(;;)
   {
    attrib22.b.disp_comp_cal +=1;
    attrib22.b.disp_home     +=1;
    attrib22.b.disp_trips    +=1;
    attrib22.b.disp_fuel     +=1;
    attrib22.b.disp_hours    +=1;
    attrib22.b.disp_service  +=1;
    attrib22.b.disp_diags    +=1;
    attrib22.b.disp_slftst   +=1;
    attrib22.b.disp_cust_set +=1;  
      
    PORTAB = (unsigned int) (attrib22.w & 0xFFFFUL);
    PORTAB = (unsigned int) ((attrib22.w >> 16)  & 0xFFFFUL);
   }
 
 
  //------------------------
EnableInterrupts;
  //------------------------
  for(;;) 
   {
     _FEED_COP(); /* feeds the dog */
   }
  //------------------------
}
//******************************************************************************