Chapter 10: C Structures
;
unsigned int this : 1;
unsigned int that : 1;
unsigned int the : 1;
unsigned int other : 1;
unsigned int tobe: 1;
unsigned int or!tobe : 1;
unsigned int hello : 1;
} flags;
Setting aside 8 flags in the space formerly used by calibrate. Now we control the
loop:
while(!flags.calibrate)
That is, we could have done this in an ideal world. In the real world, our compiler
would allow us to use the above syntax, but would assign 8 bytes to do the job.
K&R notes: “Almost everything about fields is implementation dependent.”
Bit-fields the masking-way
This is mostly a review of stuff presented with the bitwise operator section, but
reviews are good. Let’s look at a bit-masking example from OSCCAL.C:
We define an alias name for the Timer/Counter 2 Interrupt Flag Register:
#define TIFR2 _SFR_IO8(0x17)
Noting that the _SFR_IO8(0x17) is itself an alias defined elsewhere, but
eventually is aliased to a specific register address on our ATMEGA169.
We next define two ‘bit-fields’ in the TIFR2 register;
to control a loop:
while(!calibrate) { // do something while calibrate == FALSE};
which runs as long as calibrate equals FALSE. We could have used:
struct {
unsigned int calibrate : 1