|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
pin# array |
Posted: Tue Dec 11, 2007 8:48 am |
|
|
I have a rather simple program which refers to a set of 4 pins. I would like to run this program 8 times using 8 different sets of 4 pins. I was wondering if there would be any way to put the pin numbers into an array so that i can use a for loop to run the program through each different iteration. Or if someone has any other ideas for writing the code in a way that would be less messy than to rewrite it 8 times changing the variables and pin#s . Here is my simple code written for 4 pins:
//1drop.c
//control for one needle using one sensor
// pin_A2 motor power
// pin_A3 motor direction
// pin_A4 optoswitch
// pin_A5 light sensor
// all of these prep conditions , were stolen from another program, so its likely
//they are incorrect
#include <18f458.h>
#include <STDLIB>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOMCLR,NOBROWNOUT,NOIESO,NOFCMEN
#use delay(clock=8000000)
const int midpoint = 50; // marks the lowest point that the needle can climb to before redescending
const int height = 100; //declares constant to define the number of rotations descended before remounting
int location = 1; // notes the location of the needle (all the way up equals 1)
char direction; // direction of motor, 0 for up / 1 for down / 2 for unmoving
char optoswitch; // optointerupt switch (1 indicates it is interupted, 0 indicates it is open)
char alpha; //indicates the status of (optoswitch) the last time, if there is a discrepency with its current state, it indicates a rotation (or a part of one)
void main(void){
int direction = 1;
int location = 1;
int optoswitch = 1;
output_low(PIN_A2);
while(1 == 1){
optoswitch = input(PIN_A4);
if (input(PIN_A5) && location <midpoint>= height){
direction = 0;
output_low(pin_A3);
output_high(pin_A2);
}
if (location <= 1){
output_low(pin_A2);
}
if (alpha != optoswitch){
if (direction==1) {
location++;
}
if (direction==0){
location--;
}
alpha=optoswitch;
}
}
}
thank you,
sam |
|
|
Ttelmah Guest
|
|
Posted: Tue Dec 11, 2007 9:30 am |
|
|
It depends on the age of your compiler.
For the latter compilers, the input, and output functions, will accept a variable, and so you can just have an array of pin numbers (remember though that these need to be 16 bit values).
On the older compilers, a search here, will find a generic 'variable to address a pin function'. It is fairly easy to do, all you do is take the pin number/8, as a pointer to the register required, and the bottom three bits of the pin number as the bit to address.
Beware though, that though this makes smaller source code, you may well find the resulting assembler larger, and slower than the single pin addressing!. A simple 'input(PIN_A1)' instruction, translates into a single machine instruction (two if standard_io mode is used), while addressing a pin as a variable, uses quite a few instruction...
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
|