I am not sure if this is the write section as my question is more related to programming.
So, currently I am trying to control two multiplexer with an Arduino mega. I am using port manipulation to write the address to my two multiplexers to receive data from the selected pin.(I attached a picture of the relevant part of the data sheet). The current code I am using makes a seperate function for each address I write to the multiplexers. This is not efficient for what I am doing as I am taking 9 inputs into the multiplexers and eventually want to expand to 16 inputs. To do this would require a large number of functions. Here is the code I am currently using for just 3 inputs. I have attached the relevant piece of code below. I am using pins 22-30 on the Arduino mega which corresponds to port A when using port manipulation.
void setup() {
DDRA = DDRA | B11111111; //22-30 as output
analogRead(54);
}
void loop() {
//read1and2();
//read2and3();
read1and3();
}
void read1and2() {
PORTA = B00010000; // all low select pin 1 on first mux and 2 on second mux
t1 = micros();
data1 = analogRead(54);
}
void read2and3() {
PORTA = B00100001; //write pin 22 and 27 select pins 2 and 3 on muxes
t2 = micros();
data2 = analogRead(54);
}
void read1and3() {
PORTA = B00100000; //write pin 27 select pins 1 and 3 on muxes
t3 = micros();
data3 = analogRead(54);
}
What I was thinking of doing was creating a single function that took two inputs corresponding to the two inputs that I want to choose. I would then have an array for each pin that has the address it needs to write. The use the PORT to select using the array. However, I am not sure of how to actually implement it. I put some code that hopefully shows the idea of what I am trying to do. Obviously this code works but is suppose to give an idea of what I am trying to accomplish.
void setup() {
// put your setup code here, to run once:
array[] = {0000,0001,0010,0011,0100,0101,0110,0111,1000};
}
void loop() {
// put your main code here, to run repeatedly:
chooseinput(1,2);
}
void chooseInput(int mux1, int mux2){
PORTA = Barray[mux2]array[mux1]
//readdata
}
If has an idea on the implementation or has another completely different idea to reduce all of these calls to a single function that just needs the two input pins you want I would appreciate any idea.