Electronics > Beginners
How sbit works in mikroc
(1/1)
khatus:


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;
KL27x:

--- Quote ---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;
--- End quote ---
God knows why I am doing, because (disclaimer!) I am guessing. I am not proficient at coding in C and my only experience with mikroC was to run the other way.

This sbit business appears to be how you define/name bits. So how does that work in code? It doesn't. You would not write "sbit LEDA at PORTA.B0" in your code. It doesn't actually do anything as far as code compilation/generation is concerned. It would be in your header files.

So after this is added to your header file, if you wanted to read or write (set or clear) to this bit, you now have two ways to do this. For example, say you wanted to clear this bit. You could write (in your code):

1)
LEDA = 0

2)
PORTA.B0 =0             

I said there are two ways. But in reality, after you have defined this bit in your header file, you would want to avoid using the second method in your code. In addition to having a name that is handy and easy to remember, there is another major reason for naming/defining it. In the future, you might want to move LEDA to another pin. Because you have named/defined it, you can save a lot of work by changing one line in your header.

LEDA, let's say, is theoretically an output pin connected to an LED. But imagine you decide you want to move it to another pin, which happens to be PORTB.B5. So you go to your header file and you change it to:

sbit LEDA at PORTB.B5

So now you move your LED to that pin. And you're done (kinda... you also have to consider any byte operations you might have done to PORTA and PORTB, now, as well on some devices you can indirectly address some registers through a pointer, although usually not ports). So all good... except if you have any read/write instructions to "PORTA.B0" in your code, you have to find it and change it in each instance you have done that.


There is a numerical address of this port and bit which is the absolute address. And the device file that mikroC provides to you for the device, which you will (probably) include in your header files defines "PORTA.B0" as this set of numbers when it compiles. So technically, you could muck around in this device file and swap pins around, too. But that would get messy, fast, and you'd have to keep track of all your changes to know what you are doing. (Also probably would be a great idea to copy and make a new device file if you are going to muck around with it on a project; basically, don't do this).


Navigation
Message Index
There was an error while thanking
Thanking...

Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod