Go Down

Topic: multiplexing question (Read 601 times) previous topic - next topic

scary11

Hello all, I'm completly new at this. I finally decided to get help from the experts after wasting four hours on a simple problem.

I'm using the Arduino Duemilanove with the Mux Shield, and attempting to address a single output. If there is a good resource/thread for this please direct me. I did check before I posted, but wasn't able to find anything that answered my questions.

Here is my code, barely modified from the digital output example for this shield.

Code: [Select]
//Give convenient names to the control pins
#define CONTROL0 5    //MUX control pin 0 (S3 is connected to Arduino pin 2)
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2

void setup()
{
  //Set MUX control pins to output
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);


void loop()
{
   
  //Turn on output to digital pin 14 (MUX 0) and turn off the other 2 multiplexer data pins
  pinMode(14, OUTPUT);
  pinMode(15, INPUT);
  pinMode(16, INPUT);
   
  //This for loop is used to scroll through the FIRST multiplexer
  for (int i=2; i<3; i++) //as you can see, I altered the for loop so it only applies to one pin.
  {   
    //The following 4 commands set the correct logic for the control pins to select the desired input

//this section eludes my understanding. I tried altering it so the value returned would match the values for
// a specific pin as defined in the logic table. I was unsuccessful.
    digitalWrite(CONTROL0, (i&15)>>3); //S3
    digitalWrite(CONTROL1, (i&7)>>2);  //S2
    digitalWrite(CONTROL2, (i&3)>>1);  //S1
    digitalWrite(CONTROL3, (i&1));     //S0
   
    digitalWrite(14, HIGH);
    delay(200);
    digitalWrite(14, LOW);
    delay(200);
  }
 
//Turn on output to digital pin 15 (MUX 1) and turn off the other 2 multiplexer data pins
  pinMode(14, INPUT);
  pinMode(15, OUTPUT);
  pinMode(16, INPUT);
   
  //This for loop is used to scroll through the SECCOND multiplexer
  for (int i=12; i<13; i++) //specifies only pin 12. I need a more effecient way to do this.
  {   
    //The following 4 commands set the correct logic for the control pins to select the desired input

    digitalWrite(CONTROL0, (i&15)>>3); //S3
    digitalWrite(CONTROL1, (i&7)>>2);  //S2
    digitalWrite(CONTROL2, (i&3)>>1);  //S1
    digitalWrite(CONTROL3, (i&1));     //S0
   
    digitalWrite(15, HIGH); //gives power to the entire multiplexer
    delay(200);
    digitalWrite(15, LOW);  //turns off power to the entire multiplexer
    delay(200);
  }
}
 


Now, I understand that this example uses bit shift to scroll through the outputs. I modified the original for statement so I could specify the pin I wanted. I know there has to be a better way to do this. I have looked at the logic table in the spec sheet for the chip used in the mux shield... but I am having difficulty understanding how to incorporate that into my code. Most of my problem stems from my fuzzy understanding of bit shift. I've read the tutorial on this site but it just doesn't quite click. Any help or links to further resources would be greatly appreciated.

Thanks!

Go Up