# How to change a Single bit in MikroC

Status
Not open for further replies.

##### New Member
Hey,

Just wondering how you change a single bit in MikroC...

that is, what is the equivalent of this assembly:

BSF PORTA,1

in MikroC?

Seems simple enough but I haven't been able to find the syntax...

#### Pommie

##### Well-Known Member
I don't use microC but in other Cs it's done in various ways.

PORTA.1=1;
PORTA|=2;
RA1=1;
PORTAbits.RA1=1;

Most of the compilers I've seen will turn any of the above into a single bsf instruction.

Edit, forgot I have the manual here. It's PORTA.F1=1;
Why F1 I have no idea.

Mike.

Last edited:

#### gaspode42

##### Member

Taken from the Mikroe documentation

sbit type

The mikroC PRO for PIC compiler has sbit data type which provides access to bit-addressable SFRs. You can access them in the following manner:

sbit LEDA at PORTA.B0;
sbit bit_name at sfr-name.B<bit-position>;

sbit LEDB at PORTB.F0;
sbit bit_name at sfr-name.F<bit-position>;

// If you are familiar with a particular MCU and its ports and direction registers (TRIS), you can access bits by their names:
sbit LEDC at RC0_bit;
sbit bit_name at R<port-letter><bit-position>_bit;

sbit TRISC0 at TRISC0_bit;
sbit bit_name at TRIS<port-letter><bit-position>_bit;

Also have a look at this thread - I know it's on MikroB but most of their compliers are the same.

Hope this helps

Last edited:

#### be80be

##### Well-Known Member
You use the PORTA.f0
Edit, forgot I have the manual here. It's PORTA.F1=1;
Why F1 I have no idea.
It's kind of crazy They use the PORT.F1 the f matches the numbers on there
experimenter boards that way if you wanted to use pin f1 you use porta.f1.
I like using it for small chips
You can all so hit a bit like this
Code:
         PORTB |=  (1<<5);  // Sets bit five of portb

#### 3v0

##### Coop Build Coordinator
Forum Supporter
If you are going to use different compilers I suggest you set bits using compiler independent code.

Masking as described by Daniel Johnson in JPUG#1 will work.

3v0

#### Mr RB

##### Well-Known Member
...
That is, what is the equivalent of this assembly:
Bsf porta,1
...

porta.f1 = 1;

#### be80be

##### Well-Known Member
The good thing about this is it works in any C boostC HI-tech C18 microC you name it
Code:
PORTB |=  (1<<0);  // Sets bit O of portb
PORTB |=  (1<<1);  // Sets bit one of portb
PORTB |=  (1<<2);  // Sets bit two of portb
PORTB |=  (1<<3);  // Sets bit threeof portb
PORTB |=  (1<<4);  // Sets bit four of portb
PORTB |=  (1<<5);  // Sets bit five of portb

Status
Not open for further replies.

Replies
6
Views
11K
Replies
3
Views
953
Replies
5
Views
1K
Replies
3
Views
790
Replies
3
Views
2K