Datasheet
BIT FIELDS
Bit fields are specified numbers of bits that may or may not have an associated iden-
tifier. Bit fields offer a way of subdividing structures into named parts of user-defined
sizes.
Structures and unions can contain bit fields that can be up to 16 bits.
You cannot take the address of a bit field.
Note: If you need to handle specific bits of 8-bit variables (
char and unsigned
short
) or registers, you don’t need to declare bit fields. Much more elegant solution
is to use the mikroC PRO for AVR’s intrinsic ability for individual bit access — see
Accessing Individual Bits for more information.
Bit Fields Declaration
Bit fields can be declared only in structures and unions. Declare a structure normal-
ly and assign individual fields like this (fields need to be unsigned):
struct tag {
unsigned bitfield-declarator-list;
}
Here, tag is an optional name of the structure; bitfield-declarator-list is a list
of bit fields. Each component identifer requires a colon and its width in bits to be
explicitly specified. Total width of all components cannot exceed two bytes (16 bits).
As an object, bit fields structure takes two bytes. Individual fields are packed within
two bytes from right to left. In
bitfield-declarator-list, you can omit
identifier(s) to create an artificial “padding”, thus skipping irrelevant bits.
For example, if there is a need to manipulate only bits 2–4 of a register as one block,
create a structure like this:
struct {
unsigned : 2, // Skip bits 0 and 1, no identifier here
mybits : 3; // Relevant bits 2, 3 and 4
// Bits 5, 6 and 7 are implicitly left out
} myreg;
Here is an example:
172
MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Language Reference
mikroC PRO for AVR
CHAPTER 5