User manual

Table Of Contents
mikroC PRO for PIC32
MikroElektronika
219
Bit Fields Access
Bit elds can be accessed in the same way as the structure members. Use direct and indirect member selector (. and
->). For example, we could work with our previously declared myunsigned like this:
// Declare a bit eld Value_For_PortB:
myunsigned Value_For_PortB;
// Declare a pointer to mybiteld type:
mybiteld *TimerControl;
void main() {
TimerControl = (mybiteld *) (void *) &T2CON ; // explicit casting of pointer to
T2CON, so it can be assigned
...
Value_For_PortB.lo_nibble = 7;
Value_For_PortB.hi_nibble = 0x0C;
Value_For_PortB.high_byte = 0xAA;
PORTB = *(unsigned *) (void *)&Value_For_PortB;
// typecasting :
// 1. address of structure to pointer to void
// 2. pointer to void to pointer to unsigned
// 3. dereferencing to obtain the value
}
Types Conversions
The mikroC PRO for PIC32 is a strictly typed language, with each operator, statement and function demanding
appropriately typed operands/arguments. However, we often have to use objects of “mismatching” types in expressions.
In that case, type conversion is needed.
Conversion of object of one type means that object’s type is changed into another type. The mikroC PRO for PIC32
denes a set of standard conversions for built-in types, provided by compiler when necessary. For more information,
refer to the Standard Conversions.
Conversion is required in the following situations:
- if a statement requires an expression of particular type (according to language denition), and we use an
expression of different type,
- if an operator requires an operand of particular type, and we use an operand of different type,
- if a function requires a formal parameter of particular type, and we pass it an object of different type,
- if an expression following the keyword return does not match the declared function return type,
- if intializing an object (in declaration) with an object of different type.
In these situations, compiler will provide an automatic implicit conversion of types, without any programmer’s interference.
Also, the programmer can demand conversion explicitly by means of the typecast operator. For more information, refer
to the Explicit Typecasting.