sbit data type in mikro C pro

Thread Starter

khatus

Joined Jul 2, 2018
95


hello guys i have alraedy read that sbit data type provides access to bitaddressable
SFRs



But i want to know how it works in code.What is the meaning of the line
sbit LEDA at PORTA.B0;
or
sbit LCD_RS at RB4_bit;
 

JohnInTX

Joined Jun 26, 2012
4,787
It's just MicroC's different ways of allowing you to name a bit in an SFR register (like PORTA,0). They compile to the same code.

C:
sbit LEDA at PORTA.B0;   // PORTA.B0 is equivalent to RA0.
//PORTA is treated as a structure of bit variables and is a little more general i.e. bit 0 of that register.

sbit LCD_RS at RB4_bit; // RB4 is equivalent to PORTB.B4 .
//RB4 is predefined by MikroC (from Microchip) as PORTB.B4 . 
//Similar but you have to know that Microchip calls that pin RB4.

LEDA = 1;  // generates bsf PORTA,o in assembler
LCD_RS = 0; // generates bcf PORTB,4
You can see this for yourself by clicking the assembler or listing output on the toolbar after compiling.
 

JohnInTX

Joined Jun 26, 2012
4,787
So it assign a name LEDA to the bit0 of sfr register PORTA
Yes. It's so your code
1) reads better - you know what setting the port pin does and
2) is easier to modify if the LED moves to another pin. You should name the LED without reference to the pin name. For example, if LEDA indicates that you have detected something - a limit switch maybe - name the LED something like:

sbit LIMIT_SWITCH_DETECTED_LED at PORTA.B0;

Also, consider defining what turns the LED on and off. If a 1 turns the LED on, define
#define LED_ON 1
#define LED_OFF 0

Then write:
LIMIT_SWITCH_DETECTED_LED = ON; // much easier to understand and maintain.
 
Top