CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Define PIC pins into an array - is this possible

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

Define PIC pins into an array - is this possible
PostPosted: Sat Sep 25, 2010 10:56 am     Reply with quote

Hello,

Since I am not really a C programmer, I just write PIC codes in my free time, I am perhaps missing a lot of basics.

I would like to define 12 pins on a PIC processor in an array so later in the code I can refer to each by their respective order in the array.

Normally I would do a:
Code:

#define Ch1 PIN_B1
#define Ch2 PIN_A2
#define Ch3 PIN_B3
#define Ch4 PIN_C4

etc. The above is just an example, the Chx would be a mixed set of pins.

Now I would like to refer to these later in the code through a single cycle of FOR or WHILE and for that I would need something like this:
Code:

For ....{

      Output_high(Ch[i]);
      delay_ms(10);
      Output_low(Ch[i]);
}

where i goes from 1 to 12.

Is there a way to achieve this?

Thanks in advance.
Ttelmah



Joined: 11 Mar 2010
Posts: 19511

View user's profile Send private message

PostPosted: Sat Sep 25, 2010 2:24 pm     Reply with quote

Yes.
The later compiler (the last couple of years now), support the use of a variable for pins. There is though a big 'caveat'. Though in coding terms, 'pretty', this makes the I/O functions larger, and slower. You will almost certainly find that a 'generic' version, using an array for pin names, will be something like perhaps 6 times the size of a single 'fixed pin' version. You have both the extra functions to allow each pin operation to use a variable, and the array accesses themselves. You 'pays your money' on this.
As to 'how', declare the array to hold int16 values (pins are coded like this), so:
Code:

int16 pins[] = {PIN_B1,PIN_A2,PIN_B3,PIN_C4};

output_high(pins[n]);
//etc...


If you want the fast version, then used fixed pin names, with a switch statement instead.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group