Difference between sbit and bit?

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
diodo
Posts: 10
Joined: 04 Jan 2010 16:57

Difference between sbit and bit?

#1 Post by diodo » 04 Jan 2010 17:13

Hi.
Can someone explain the difference between sbit and bit?
When sbit is used and when bit?
With some example if possible?
I have not understood well from the manual... :oops:

Thanks

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

#2 Post by tihomir.losic » 05 Jan 2010 17:02

Hello,

The main difference between the bit and sbit is that you can declare sbit a varible in a unit in such way that it points to a specific bit in the SFR register. In the main program you need to specify to which register this sbit points to.

Code: Select all

module MyModule
   
dim Abit as sbit sfr external ' Abit is precisely defined in some external file, for example 

in the main program unit
...
implements
....
end.
The mikroBasic 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, there are no pointers to bit variables, and an array of type bit is not valid.

Code: Select all

dim bf as bit  ' bit variable
sbit is not a new variable and does not take extra memory space, while with a bit different, will the new variable, which further defines and takes additional space in memory.

Best regards,

Losic Tihomir
mikroElektronika [Support team]

igeorge
Posts: 593
Joined: 28 Dec 2005 09:15

Re: Difference between sbit and bit?

#3 Post by igeorge » 31 Dec 2011 13:43

can i declare an sbit as part of an array ?
let's say
dim Heat as sbit at sdata [3] [2]
Sdata is an 5 rows 8 bits array
I would like to declare at start Heat because it is easier to use in the program Heat then sdata [3] [2]
Thank you
Ion
Experience is something you don't get until just after you need it

piort
Posts: 1379
Joined: 28 Dec 2005 16:42
Location: Laval,Québec,Canada,Earth... :-)
Contact:

Re:

#4 Post by piort » 31 Dec 2011 18:49

Hi,
i think you have your answer ...
tihomir.losic wrote: an array of type bit is not valid.
And sbit refer to system register bit like :

Code: Select all

Dim led1 as sbit PortA.2
dim MyTimerEnable as sbit intcon1.GIE
So the first variable refer directly to the Port A , third pin state. Its link to port A register . The second variable link to the general ISR enable in the INTCON1 register. In both case they are system register. System register are register define by microchip inside the datasheet of your Pic. So you can not use sbit to refer to something else.
for what you want to do, just declare a bit as global variable, assign the value of the bit in the array at the beginning of the routine where you will use it...

HTH a bit ;-)

igeorge
Posts: 593
Joined: 28 Dec 2005 09:15

Re: Difference between sbit and bit?

#5 Post by igeorge » 31 Dec 2011 20:13

thank you Piort
Experience is something you don't get until just after you need it

Post Reply

Return to “mikroBasic PRO for PIC General”