Bit fields, Mikroc – ABL electronic PIC Microcontrollers PIC16 User Manual

Page 88

Advertising
background image

Referring to declarations from the previous example:

mu.d = 4.016;

Lcd_Out_Cp(FloatToStr(mu.d));

// OK: displays mu.d = 4.016

Lcd_Out_Cp(IntToStr(mu.i));

// peculiar result

pm->i = 3;

Lcd_Out_Cp(IntToStr(mu.i));

// OK: displays mu.i = 3

The second

Lcd_Out_Cp

is legal, since

mu.i

is an integral type. However, the bit

pattern in

mu.i

corresponds to parts of the previously assigned

double

. As such,

it probably does not provide a useful integer interpretation.

When properly converted, a pointer to a union points to each of its members, and
vice versa.

Bit Fields

Bit fields are specified numbers of bits that may or may not have an associated
identifier. Bit fields offer a way of subdividing structures into named parts of user-
defined sizes.

mikroC implementation of bit fields requires you to set aside a structure for the
purpose, i.e. you cannot have a structure containing bit fields and other objects.
Bit fields structure can contain up to 8 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 solu-
tion is to use mikroC’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. Declare a structure normally, and
assign individual fields like this (fields need to be unsigned):

struct

tag

{ unsigned

bitfield-declarator-list

; }

mikroC - C Compiler for Microchip PIC microcontrollers

mikroC

making it simple...

80

MikroElektronika: Development tools - Books - Compilers

page

Advertising
This manual is related to the following products: