is there a PIN Array by default?

General discussion on mikroC.
Post Reply
Author
Message
Nameless
Posts: 7
Joined: 19 Jul 2010 21:35

is there a PIN Array by default?

#1 Post by Nameless » 21 Jul 2010 16:29

is there any quick way to access PINs except doing each on its owen?
like
for (i = 0; i < 6; i++)
RB_bit = 0xFF; ?
insted of
RB[1]_Bit = 0xFF;
RB[2]_Bit = 0xFF;
RB[3]_Bit = 0xFF;
RB[4]_Bit = 0xFF;
RB[5]_Bit = 0xFF;
........

i also tried to make a function to do that for me
but i couldnt decalre a bit array
bit arry[7];
compiler said "array of bit type objects is not allowed" :(
cheers;

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

Re: is there a PIN Array by default?

#2 Post by Sobrietytest » 22 Jul 2010 00:42

I can understand the logic behind your question but the compiler answered it very well! Bits can only be either 1 or 0, ports can only be In or Out, therefore you use these commands:

TRISB = (0b11110000) //Set Port B pin directions (4 input, 4 output)
//LATB = (0b11110000) //Set Port B pin directions (4 input, 4 output) //PIC18 series+
PORTB = (0b00001111) //Set Port B output values

Each binary bit corresponds to a Port pin.

These instances can be declared in hex, decimal or binary. Generally you use these commands at the beginning of your program but you can still change them in-program, not advisable though.

Nameless
Posts: 7
Joined: 19 Jul 2010 21:35

Re: is there a PIN Array by default?

#3 Post by Nameless » 22 Jul 2010 03:03

thank you for ur reply
but i meant writing to pins, like light a LED on a pin or turn it off

User avatar
ranko.rankovic
Posts: 433
Joined: 11 Jun 2010 09:22

Re: is there a PIN Array by default?

#4 Post by ranko.rankovic » 22 Jul 2010 15:30

Hello Nameless,

You should consider our new compiler, mikroC PRO, and find explanation for sbit's in Help, those are meant exactly for your needs.

The mikroC PRO for PIC compiler has sbit data type which provides access to registers, SFRs, variables, etc.
You can declare a sbit varible in a unit in such way that it points to a specific bit in SFR register:

Code: Select all

extern sfr sbit Abit; // Abit is precisely defined in some external 
                      // file, for example in the main program unit
In the main program you have to specify to which register this sbit points to, for example:

Code: Select all

sbit Abit at PORTB.F0; // this is where Abit is fully defined
...
void main() {  
...
}
In this way the variable Abit will actually point to PORTB.0. Please note that we used the keyword sfr for declaration of Abit, because we are pointing it to PORTB which is defined as a sfr variable.
In case we want to declare a bit over a variable which is not defined as sfr, then the keyword sfr is not necessary, for example:

Code: Select all

extern sbit AnotherBit; // AnotherBit is precisely defined in some external file, 
                        //for example in the main program unit
char MyVar;
sbit AnotherBit at MyVar.F0; // this is where AnotherBit is fully defined
...
void main() {  
...
}
Also you could consider Migration Documents for easier adaption to our new compiler and its IDE. You will see how it's easy to use now compared to old one!

Hope this helped.

Best regards
Ranko Rankovic
mikroElektronika [Support Department]

Nameless
Posts: 7
Joined: 19 Jul 2010 21:35

Re: is there a PIN Array by default?

#5 Post by Nameless » 22 Jul 2010 16:36

im not sure if ur answer is even close to my question :!:

i need a loop to go through all pins, and set the leds connected to them either to on or off

User avatar
ranko.rankovic
Posts: 433
Joined: 11 Jun 2010 09:22

Re: is there a PIN Array by default?

#6 Post by ranko.rankovic » 26 Jul 2010 15:13

Hello Nameless,

All I wanted with my previous post is to tell you how easier and more elegant is now with usage of sbits.
i.e.
RB1_bit instead of PORTB.F1

You can access individual bits like this:

Code: Select all

int i;
void main(){
     TRISB = 0;
     PORTB = 0;
     for (i = 0; i < 8; i++){
          PORTB |= 0x01 << i;
     }
}
Best regards
Ranko Rankovic
mikroElektronika [Support Department]

Post Reply

Return to “mikroC General”