One overlooked feature of C is bit packing, which is great for embedded work. You can define a struct
to access each bit individually.
typedef struct
{
unsigned char bit0 : 1;
unsigned char bit1 : 1;
unsigned char bit2 : 1;
unsigned char bit3 : 1;
unsigned char bit4 : 1;
unsigned char bit5 : 1;
unsigned char bit6 : 1;
unsigned char bit7 : 1;
} T_BitArray;
The : 1
tells the compiler that you only want each variable to be 1 bit long. And then just access the address that your variable reg
sits on, cast it to your bit array and then access the bits individually.
((T_BitArray *)®)->bit1 = value;
®
is the address of your variable. ((T_BitArray *)®)
is the same address, but now the complier thinks of it as a T_BitArray
address and ((T_BitArray *)®)->bit1
provides access to the second bit. Of course, it's best to use more descriptive names than bit1