This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

long bit fields

I am using the SPI to access a couple of 20 & 24 bit A/D converters which have some digital status bits preceding the reading. I already have some C code written (for another CPU) that defines the incoming data structure as a unsigned long. The Keil compiler, however, says I must use char or integer size bit fields. Is there an easy way around this without having to hand code the byte manipulation code manually?

Parents
  • I'm not sure I properly understand this question. If I do, a union in which one member is an unsigned long, and the other is a struct containing unsigned chars and bit fields may solve your problem.

    I never use bit fields for this sort of thing, because the way bit fields map onto bytes is implementation-dependent, and may even change from one version of a compiler to the next, or with different compiler option settings.

    -- Gary Culp

Reply
  • I'm not sure I properly understand this question. If I do, a union in which one member is an unsigned long, and the other is a struct containing unsigned chars and bit fields may solve your problem.

    I never use bit fields for this sort of thing, because the way bit fields map onto bytes is implementation-dependent, and may even change from one version of a compiler to the next, or with different compiler option settings.

    -- Gary Culp

Children
  • Here is a simplified example.

    typedef struct {
     unsigned long det_in     :4,   /*compliance & short detection bits */
                   Brd_rev    :4,   /* board / pal revision  */
                   nu        :4,    /* not used as of 8/2000 */
                   AD_data    :20;  /* 0=-max, 0x80000=0v, fffff=+max */
                  } Bridge_brd_Rx_Type;  /* for each bridge card */
    
    I do use this variable type in a union of however many longs it takes.My end goal is to point a PEC register at this structure, let the SPI run, and then access the different bit fields as
     { long reading; char revision;
    reading = Brdg1.AD_data;
    revision = Brdg1.Brd_rev;
    ...
     }