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;