Chapter 10: C Structures
s
rovides a way to have a type that may be of different sizes depending on
the circumstances of its use.
t or an int in the same program
float f;
} u;
structure or union that is defined to be
a flag, or a
u might want to define. These fields can be
fields slow
ficiency
and
ion
ise
ay
In our exam unsigned char when
t could only have two values: TRUE or FALSE. Using bit-fields we can
imilar variables in a single unsigned char (note – not true for
otes them to eight bytes).
W could define:
Union
A union p
We use a union in prgmspacehlp.h to store a floa
memory:
union
{
int i[2]; // uint16_t
Bit-fields......................................................................................................
ANSI C defines bit-fields as a member of a
a cluster of bits. This cluster can be a single bit, as would be used for
4-bit nibble, or any number of bits yo
very useful, but unfortunately, in many microcontrollers, these bit-
things down (the compiler promotes bit to larger data types) so for ef
sake, bits are best dealt with using bit masking, which compiles to faster
smaller assembly code. Bit masking simply uses a constant to define the posit
of a bit in a byte and allows you to read or write only that bit using the bitw
operators. We will look at the C-way, since we are learning C, then the mask-w
since we want to be as efficient as possible.
Bit-Fields the C-way
ples above we have often declared an object as an
that objec
declare eight s
WinAVR, which prom
e
unsigned char calibrate = FALSE;