User manual

Table Of Contents
138
mikoC PRO for PIC32
MikroElektronika
Accessing Individual Bits
The mikroC PRO for PIC32 allows you to access individual bits of 32-bit variables. It also supports sbit and bit data
types.
Lets use the Zero bit as an example. This bit is dened in the denition le of the particular MCU as:
const register unsigned short int Z = 1;
sbit Z_bit at SR.B1;
To access this bit in your code by its name, you can write something like this:
// Clear Zero bit
SR.Z = 0;
In this way, if Zero bit changes its position in the register, you are sure that the appropriate bit will be affected.
But, if Zero bit is not located in the designated register, you may get errors.
Another way of accesing bits is by using the direct member selector (.) with a variable, followed by one of identiers
B0, B1, … , B31, or F0, F1, … F32, with F32 being the most signicant bit, to access the desired bit:
// predened globals as bit designators
// Clear Zero bit
SR.B1 = 0;
// Set Zero bit
SR.F1 = 1;
In this way, if the target bit changes its position in the register, you cannot be sure that you are invoking the appropriate
bit.
This kind of selective access is an intrinsic feature of mikroC PRO for PIC32 and can be used anywhere in the code.
Identiers B0–B31 are not case sensitive and have a specic namespace.
You may override them with your own members B0–B31 within any given structure.
When using literal constants as bit designators instead of predened ones, make sure not to exceed the appropriate
type size.
Also, you can access the desired bit by using its alias name, in this case Z_bit:
// Set Zero Bit
C_bit = 1;
In this way, if the Zero bit changes its register or position in the register, you are sure that the appropriate bit will be
affected.
For backward compatibility, you can access bits in this way also:
// Clear TRISB3
TRISBbits.TRISB3 = 0;