0

I am currently writing code for a PIC micro-controller and I would like to structure some of my code using a uint8_t as a "state counter" for a part of my code. This involves a lot of bitwise operations. What I'd like to do is create a struct for this uint8_t in a similar vein to the SFR bits structs that are within the header flies, I have included an example of one from the header file that I'm using below.

The header file allows access to the bits within the SFR using a notation similar to accessing elements within a struct, eg U1STAbits.UTXBF and as a uint16_t this is what I'd like to implement in my code as it will allow me to use a switch statement as the main structure of my code.

#define U1STA U1STA
extern volatile unsigned int  U1STA __attribute__((__sfr__));
__extension__ typedef struct tagU1STABITS {
  union {
    struct {
      unsigned URXDA:1;
      unsigned OERR:1;
      unsigned FERR:1;
      unsigned PERR:1;
      unsigned RIDLE:1;
      unsigned ADDEN:1;
      unsigned URXISEL:2;
      unsigned TRMT:1;
      unsigned UTXBF:1;
      unsigned UTXEN:1;
      unsigned UTXBRK:1;
      unsigned :1;
      unsigned UTXISEL0:1;
      unsigned UTXINV:1;
      unsigned UTXISEL1:1;
    };
    struct {
      unsigned :6;
      unsigned URXISEL0:1;
      unsigned URXISEL1:1;
    };
  };
} U1STABITS;
extern volatile U1STABITS U1STAbits __attribute__((__sfr__));

Update: I have made a potential solution based from this:

typedef struct  {
    union {
        struct{
            unsigned breakDetected  :1;
            unsigned overrunError   :1;
            unsigned framingError   :1;
            unsigned startDetected  :1;
            unsigned dmxMode        :1;
            unsigned rdmMode        :1;
        };
        uint8_t UartFlags;
    };
} UartFlagsBits;

UartFlagsBits uartFlags;
RobbG
  • 371
  • 5
  • 14
  • This is called bit field. And what is your question? – Alex F Jan 27 '15 at 15:35
  • 1
    The smallest addressable unit on just about all modern CPUs is the byte. Bit fields like the one you show is handled by the compiler generating the code needed to get or set the individual bits. – Some programmer dude Jan 27 '15 at 15:37
  • So if I was to declare my bitfield in a similar way would I be able to access it as a uint8_t as well then? – RobbG Jan 27 '15 at 15:40
  • If you use the solution with unions then yes. – Some programmer dude Jan 27 '15 at 15:45
  • Define a union, with uint8_t member and bitfield struct. You can read/write both the whole value through uint8_t and its bits through bitfield structure. Standard way in hardware programming. – Alex F Jan 27 '15 at 15:45
  • 1
    @RobbG At minimum you should at least use same type for bits as a flag variable (`uint8_t` instead of `unsigned`). But even then C standard allows compiler to put padding in bitfields and change type or order. In general bit fields are a mess for what you are trying to do. I would use plain `uint8_t` and use bit mask operators `~&|^` to modify bits to have 100% control. – user694733 Jan 27 '15 at 15:48
  • Relevant: http://stackoverflow.com/a/19376742/694733 – user694733 Jan 27 '15 at 15:52

1 Answers1

0

By defining a union between a bitfield struct and a uint8_t I am able to access the bitfield as a number to perform actions on the whole bitfield as suggested by Alex.

typedef struct  {
    union {
        struct{
            uint8_t breakDetected  :1;
            uint8_t overrunError   :1;
            uint8_t framingError   :1;
            uint8_t startDetected  :1;
            uint8_t dmxMode        :1;
            uint8_t rdmMode        :1;
        };
        uint8_t UartFlags;
    };
} UartFlagsBits;

UartFlagsBits uartFlags;
RobbG
  • 371
  • 5
  • 14