# Can i access single bits?(C)

#### markosloizou

Joined Jan 6, 2012
3
I'm new to microcontrollers and i was wondering if i could access single bits instead of the whole register.Im programming microcontrollers using C. For example to blink a led, i have to set the bit to output and then to high by using something like TRISA = 0; PORTA = 0010;(pic16f84a) to set the pin high. Is there a way to access that bit only without having to write the binary or hex number for the whole register?

#### ErnieM

Joined Apr 24, 2011
8,136
Yes you can. Go look for the pic16f84a.h file (it's in the program files area for the C18 compiler). It gives you definitions for all the symbols for the registers.

The individual bits of PORTA can be accessed like so:

PORTAbits.RA0 = 1;
PORTAbits.RA1 = 1;
...
PORTAbits.RA7 = 1;

The definitions attempt to follow the device names given in the data sheet.

#### DerStrom8

Joined Feb 20, 2011
2,390
Yes you can. Go look for the pic16f84a.h file (it's in the program files area for the C18 compiler). It gives you definitions for all the symbols for the registers.

The individual bits of PORTA can be accessed like so:

PORTAbits.RA0 = 1;
PORTAbits.RA1 = 1;
...
PORTAbits.RA7 = 1;

The definitions attempt to follow the device names given in the data sheet.
I think that's only for C18, Ernie. I think it's different for other compilers.

By the way, the OP didn't mention what compiler he's using. markosloizou, are you using Hi-Tech C or another compiler?

#### AlexR

Joined Jan 16, 2008
732
Many C compilers for micro-controllers have extensions to the C language that support bit operations but as it is not part of the C standard each compiler vendor does it their own way. Check your compiler documentation.

Failing that you can use the following set of macros to perform bit operations. Just add the macros to the start of your source code or to you header file

Rich (BB code):
#define set_bit(ADDRESS,BIT) (ADDRESS |= (1<<BIT))

// The example below demonstrates typical use

if(test_bit(portb, 3)) //if bit 3 of port b is true
{
set_bit(porta, 4); //set bit 4 of port a to 1
}
else
{
clear_bit(porta, 4); //set bit 4 of port a to 0
}

#### markosloizou

Joined Jan 6, 2012
3
I think that's only for C18, Ernie. I think it's different for other compilers.

By the way, the OP didn't mention what compiler he's using. markosloizou, are you using Hi-Tech C or another compiler?
I'm currently using mikroC pro for PIC compiler(demo version)

#### ErnieM

Joined Apr 24, 2011
8,136
OK, Mikro C does things differently. Their C silently included a C file into your project that defines the special needs of a particular PIC. Look thru the Microelectronica area for the defs folder and find your P16F84A.c file. That shows you all the special definitions for bit access and such.

The individual bits of PORTA can be accessed like so:

PORTA.B0 = 1;
PORTA.B6 = 1;
...
PORTA.B7 = 1;

Note B0 to B7 is used for every register unlike the above C18 example.

#### THE_RB

Joined Feb 11, 2008
5,438
MikroC also accepts F instead of B;

PORTA.F0
PORTA.F1 (etc)

I see B used in some of the MikroC earlier examples but F seems to be more popular now.

#### markosloizou

Joined Jan 6, 2012
3
MikroC also accepts F instead of B;

PORTA.F0
PORTA.F1 (etc)

I see B used in some of the MikroC earlier examples but F seems to be more popular now.
I don't really care which is more popular

but since both work i'm happy. Thanks for the reply btw

#### spinnaker

Joined Oct 29, 2009
7,835
MikroC also accepts F instead of B;

PORTA.F0
PORTA.F1 (etc)

I see B used in some of the MikroC earlier examples but F seems to be more popular now.
Why F????

B= Bit I would think. But F?

#### PaulEE

Joined Dec 23, 2011
423
I don't really care which is more popular

but since both work i'm happy. Thanks for the reply btw
Also, I work with MikroC daily. If you have any other questions, do start a thread and PM me about it. MikroC is a great little compiler; very user friendly.

#### DerStrom8

Joined Feb 20, 2011
2,390
Also, I work with MikroC daily. If you have any other questions, do start a thread and PM me about it. MikroC is a great little compiler; very user friendly.
Generally it is preferred that you do not ask technical questions through PMs. A new thread would be best.

#### cebrax

Joined Jan 30, 2010
26
I think he didn't mean that.
He wanted the question starter person to PM him that (s)he started a new question.