User manual
142
mikoC PRO for dsPIC
MikroElektronika
Accessing Individual Bits
The mikroC PRO for dsPIC30/33 and PIC24 allows you to access individual bits of 16-bit variables. It also supports
sbit and bit data types.
Lets use the Zero bit as an example. This bit is dened in the denition 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 identiers B0,
B1, … , B15, or F0, F1, … F15, with F15 being the most signicant bit, to access the desired bit :
// predened 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 dsPIC30/33 and PIC24 and can be used anywhere
in the code. Identiers B0–B15 are not case sensitive and have a specic namespace.
You may override them with your own members B0–B15 within any given structure.
When using literal constants as bit designators instead of predened 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;