Mikroc – ABL electronic PIC Microcontrollers PIC16 User Manual

Page 89

Advertising
background image

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 one byte (8
bits).

As an object, bit fields structure takes one byte. Individual fields are packed with-
in byte from right to left. In

bitfield-declarator-list

, you can omit identi-

fier(s) to create artificial “padding”, thus skipping irrelevant bits.

For example, if we need to manipulate only bits 2–4 of a register as one block, we
could create a structure:

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:

typedef struct

{

prescaler : 2; timeronoff : 1; postscaler : 4;} mybitfield;

which declares structured type

mybitfield

containing three components:

prescaler

(bits 0 and 1),

timeronoff

(bit 2), and

postscaler

(bits 3, 4, 5,

and 6).

Bit Fields Access

Bit fields can be accessed in same way as the structure members. Use direct and
indirect member selector (

.

and

->

). For example, we could work with our

previously declared

mybitfield

like this:

// Declare a bit field TimerControl:

mybitfield TimerControl;

void

main() {

TimerControl.prescaler = 0;

TimerControl.timeronoff = 1;

TimerControl.postscaler = 3;

T2CON = TimerControl;

}

MikroElektronika: Development tools - Books - Compilers

81

page

mikroC - C Compiler for Microchip PIC microcontrollers

mikroC

making it simple...

Advertising
This manual is related to the following products: