Accessing Individual Bits

The mikroPascal PRO for PIC allows you to access individual bits of 8-bit variables. It also supports sbit and bit data types.

Lets use the Global Interrupt Bit (GIE) as an example. This bit is defined in the definition file of the particular MCU as :

const GIE = 7; register;
var   GIE_bit : sbit at INTCON.B7;

To access this bit in your code by its name, you can write something like this:

// Clear Global Interrupt Bit (GIE) 
INTCON.GIE := 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 GIE 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, … , B7, or 0, 1, … 7, with 7 being the most significant bit :

// predefined globals as bit designators
// Clear bit 0 in INTCON register 
INTCON.B0 := 0;

// literal constant as bit designator
// Set bit 5 in ADCON0 register 
ADCON0.5 := 1;

// expression as bit designator
// Set bit 6 in STATUS register 
i := 5;
STATUS.(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.

When using literal constants as bit designators instead of predefined ones, make sure not to exceed the appropriate type size.

There is no need of any special declarations. This kind of selective access is an intrinsic feature of mikroPascal PRO for PIC and can be used anywhere in the code. Identifiers B0B7 are not case sensitive and have a specific namespace. You may override them with your own members B0B7 within any given structure.

Also, you can access the desired bit by using its alias name, in this case GIE_bit :

// Set Global Interrupt Bit (GIE) 
GIE_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 mikroPascal PRO for PIC compiler has sbit data type which provides access to bit-addressable SFRs.
You can declare a sbit varible in a unit in such way that it points to a specific bit in SFR register:

unit MyUnit;
   
var Abit: sbit; sfr; external; // Abit is precisely defined in some external file, for example in the main program unit
...
implementation
....
end.

In the main program you have to specify to which register this sbit points to, for example:

program MyProgram;
...
var Abit: sbit at PORTB.0; // this is where Abit is fully defined
...
begin
...
end.

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.
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:

unit MyUnit;
   
var AnotherBit: sbit; external; // Abit is precisely defined in some external file, for example in the main program unit
...
implementation
...
end.
program MyProgram;
...
var MyVar: byte;
var Abit: sbit at MyVar.0; // this is where Abit is fully defined
...
begin
...
end.

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:

unit MyUnit;
   
var PORTAlias: byte; external; // here in the library we can use its symbolic name
...
implementation
...
end.
program MyProgram;
...
var PORTAlias: byte at PORTB; // this is where PORTAlias is fully defined
...
begin
...
end.
  Note : Bear in mind that when using at operator in your code over a variable defined through a external modifier, appropriate memory specifer must be appended also.

bit type

The mikroPascal PRO for PIC 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.

var bf : bit;    // bit variable

There are no pointers to bit variables:

var ptr : ^bit;    // invalid

An array of type bit is not valid:

var arr[5] : bit;     // invalid 
  Note :
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code