|
|
View previous topic :: View next topic |
Author |
Message |
oxxyfx
Joined: 24 May 2007 Posts: 97
|
Define PIC pins into an array - is this possible |
Posted: Sat Sep 25, 2010 10:56 am |
|
|
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: 19496
|
|
Posted: Sat Sep 25, 2010 2:24 pm |
|
|
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 |
|
|
|
|
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
|