Sei sulla pagina 1di 3

7/23/13

Accessing Individual Bits

Accessing Individual Bits


mikroC PRO for PIC Specifics >

Accessing Individual Bits


The mikroC PRO for PIC allows you to access individual bits of 8-bit variables. It also supports s b i tand b i tdata types. Lets use the Global Interrupt Bit (GIE) as an example. This bit is defined in the definition file of the particular MCU as :
c o n s tr e g i s t e ru n s i g n e ds h o r ti n tG I E=7 ; s b i t G I E _ b i ta tI N T C O N . B 7 ;

To access this bit in your code by its name, you can write something like this:
/ /C l e a rG l o b a lI n t e r r u p tB i t( G I E ) I N T C O N . G I E=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 one of identifiers B 0 , B 1 , , B 7 , or F 0 , F 1 , F 7 , with F 7being the most significant bit, to access the desired bit :
/ /p r e d e f i n e dg l o b a l sa sb i td e s i g n a t o r s / /C l e a rb i t0i nI N T C O Nr e g i s t e r I N T C O N . B 0=0 ; / /S e tb i t0i nI N T C O Nr e g i s t e r I N T C O N . F 0=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 PIC and can be used anywhere in the code. Identifiers B 0 B 7are not case sensitive and have a specific namespace. You may override them with your own members B 0 B 7within 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 G I E _ b i t:
/ /S e tG l o b a lI n t e r r u p tB i t( G I E ) G I E _ b i t=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. Note : If aiming at portability, avoid this style of accessing individual bits, use the bit fields instead. See Predefined Globals and Constants for more information on register/bit names.

sbit type
The mikroC PRO for PIC compiler has s b i tdata type which provides access to registers, SFRs, variables, etc.
www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/accessing_individual_bits.htm 1/3

7/23/13

Accessing Individual Bits

You can declare a s b i tvariable in a unit in such way that it points to a specific bit in SFR register:
e x t e r ns f rs b i tA b i t ;/ /A b i ti sp r e c i s e l yd e f i n e di ns o m ee x t e r n a lf i l e ,f o re x a m p l ei nt h em a i np r o g r a mu n i t

In the main program you have to specify to which register this sbit points to, for example:
s b i tA b i ta tP O R T B . B 0 ;/ /t h i si sw h e r eA b i ti sf u l l yd e f i n e d . . . v o i dm a i n ( ){ . . . }

In this way the variable A b i twill actually point to PORTB.0. Please note that we used the keyword s f rfor declaration of A b i t , because we are pointing it to PORTB which is defined as a s f rvariable. Note : Declaring a s b i tvariable is not possible via F 0 , F 1 , F 1 5identifiers. In case we want to declare a bit over a variable which is not defined as s f r , then the keyword s f ris not necessary, for example:
e x t e r ns b i tA n o t h e r B i t ;/ /A n o t h e r B i ti sp r e c i s e l yd e f i n e di ns o m ee x t e r n a lf i l e ,f o re x a m p l ei nt h em a i np r o g r a mu n i t

c h a rM y V a r ; s b i tA n o t h e r B i ta tM y V a r . B 0 ;/ /t h i si sw h e r eA n o t h e r B i ti sf u l l yd e f i n e d . . . v o i dm a i n ( ){ . . . }

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 :
e x t e r nc h a rP O R T A l i a s ;/ /h e r ei nt h el i b r a r yw ec a nu s ei t ss y m b o l i cn a m e

c h a rP O R T A l i a sa tP O R T B ;/ /t h i si sw h e r eP O R T A l i a si sf u l l yd e f i n e d . . . v o i dm a i n ( ){ . . . }

Note : Bear in mind that when using a toperator in your code over a variable defined through a e x t e r nmodifier, appropriate memory specifer must be appended also.

bit type
The mikroC PRO for PIC compiler provides a b i tdata type that may be used for variable declarations. It can not be used for argument lists, and function-return values.
b i tb f ; / /b i tv a r i a b l e

There are no pointers to bit variables:


www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/accessing_individual_bits.htm 2/3

7/23/13
b i t* p t r ; / /i n v a l i d

Accessing Individual Bits

An array of type bit is not valid:


b i ta r r[ 5 ] ; / /i n v a l i d

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

Related topics: Bit fields, Predefined globals and constants, Extern modifier

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

www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/accessing_individual_bits.htm

3/3

Potrebbero piacerti anche