title: Tips on redefining a register bitfield in C
body: <p>I am struggling trying to come up with a clean way to redefine some register bitfields to be usable on a chip I am working with.</p>
  
  <p>For example, this is what one of the CAN configuration registers is defined as:</p>
  
  <pre><code>extern volatile near unsigned char       BRGCON1;
  extern volatile near struct {
    unsigned BRP0:1;
    unsigned BRP1:1;
    unsigned BRP2:1;
    unsigned BRP3:1;
    unsigned BRP4:1;
    unsigned BRP5:1;
    unsigned SJW0:1;
    unsigned SJW1:1;
  } BRGCON1bits;
  </code></pre>
  
  <p>Neither of these definitions is all that helpful, as I need to assign the BRP and SJW like the following:</p>
  
  <pre><code>struct
  {
      unsigned BRP:6;
      unsigned SJW:2;
  } GoodBRGbits;
  </code></pre>
  
  <p>Here are two attempts that I have made:</p>
  
  <p>Attempt #1:</p>
  
  <pre><code>union
  {
      byte Value;
      struct
      {
          unsigned Prescaler:6;
          unsigned SynchronizedJumpWidth:2;
      };    
  } BaudRateConfig1 = {NULL};
  BaudRateConfig1.Prescaler = 5;
  BRGCON1 = BaudRateConfig1.Value;
  </code></pre>
  
  <p>Attempt #2:</p>
  
  <pre><code>static volatile near struct
  {
      unsigned Prescaler:6;
      unsigned SynchronizedJumpWidth:2;
  } *BaudRateConfig1 = (volatile near void*)&amp;BRGCON1;
  BaudRateConfig1-&gt;Prescaler = 5;
  </code></pre>
  
  <p>Are there any "cleaner" ways to accomplish what I am trying to do?  Also I am slightly annoyed about the volatile near casting in Attempt #2.  Is it necessary to specify a variable is near?</p>