Accessing Individual Bits
The mikroC PRO for AVR allows you to access individual bits of 8-bit variables. It also supports sbit
and bit
data types.
Lets use the Global Interrupt Bit as an example. This bit is defined in the definition file of the particular MCU as :
const register unsigned short int SREG_I = 7; sbit SREG_I_bit at SREG.B7;
To access this bit in your code by its name, you can write something like this:
// Clear Global Interrupt Bit SREG.SREG_I = 0;
In this way, if GIE bit changes its position in the register, you are sure that the appropriate bit will be affected.
But, if SREG_I 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 identifiers B0
, B1
, … , B7
, or F0
,
F1
, … F7
, with F7
being the most significant bit, to access the desired bit :
// predefined globals as bit designators // Clear bit 6 in ADCSRA register ADCSRA.B6 = 0; // Set bit 5 in SPCR register SPCR.F5 = 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 AVR and can be used anywhere in the code. Identifiers B0
–B7
are not case sensitive and have a specific namespace.
You may override them with your own members B0
–B7
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 SREG_I_bit
:
// Set Global Interrupt Bit SREG_I_bit = 1;
In this way, if the GIE 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.
sbit type
The mikroC PRO for AVR compiler has sbit
data type which provides access to registers, SFRs, variables, etc.
You can declare a sbit
variable in a unit in such way that it points to a specific bit in SFR register:
extern sfr sbit Abit; // Abit is precisely defined in some external file, for example in the main program unit
In the main program you have to specify to which register this sbit points to, for example:
sbit Abit at PORTB.F0; // this is where Abit is fully defined ... void main() { ... }
In this way the variable Abit
will actually point to PORTB.0. Please note that we used the keyword sfr
for declaration of Abit
, because we are pointing it to PORTB which is defined as a sfr
variable.
sbit
variable is not possible via F0
, F1
, … F15
identifiers.
In case we want to declare a bit over a variable which is not defined as
sfr
, then the keyword sfr
is not necessary, for example:
extern sbit AnotherBit; // AnotherBit is precisely defined in some external file, for example in the main program unit
char MyVar; sbit AnotherBit at MyVar.B0; // this is where AnotherBit is fully defined ... void main() { ... }
at keyword
You can use the keyword "at" to make an alias to a variable, for example, you can write a library without using register names, and later in the main program to define those registers, for example:
extern char PORTAlias; // here in the library we can use its symbolic name
char PORTAlias at PORTB; // this is where PORTAlias is fully defined ... void main() { ... }
at
operator in your code over a variable defined through a extern
modifier, appropriate memory specifer must be appended also.
bit type
The mikroC PRO for AVR compiler provides a bit
data type that may be used for variable declarations. It can not be used for argument lists, and function-return values.
bit bf; // bit variable
There are no pointers to bit variables:
bit *ptr; // invalid
An array of type bit is not valid:
bit arr [5]; // invalid
- Bit variables can not be initialized.
- Bit variables can not be members of structures and unions.
- Bit variables do not have addresses, therefore unary operator
&
(address of) is not applicable to these variables.
What do you think about this topic ? Send us feedback!