Mikroelektronika MIKROE-442 Datenbogen
140
mikoBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
Accessing Individual Bits
The mikroBasic 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 defined in the definition file of the particular MCU as:
const Z = 1
dim Z_bit as sbit 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 a primary expression.
Primary expression can be variable, constant, function call or an expression enclosed by parentheses. For individual bit
access there are predefined global constants
B0, B1, … , B15
, or
0, 1, … 15
, with
15
being the most significant bit:
‘ predefined globals as bit designators
‘ Clear bit 0 in STATUS register
SR.B0 = 0
‘ literal constant as bit designator
‘ Set bit 5 in STATUS register
SR.F5 = 1
‘ expression as bit designator
‘ Set bit 6 in STATUS register
i = 5
SR.(i+1) = 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 mikroBasic PRO for dsPIC30/33 and PIC24 and can be used
anywhere in the code. Identifiers
B0–B15
are not case sensitive and have a specific namespace.
You may override them with your own members
B0–B15
within any given structure.
When using literal constants as bit designators instead of predefined 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
Z_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.
See Predefined Globals and Constants for more information on register/bit names.