Created
March 11, 2023 21:35
-
-
Save dbgoytia/670d3e0a715d943c948614b3518046c0 to your computer and use it in GitHub Desktop.
Reading a 16 channel mux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int mux_control_pins[] = {2, 3, 4, 5} // control pins for the MUX | |
const int input_pins[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}; // input pins for the mux. see table below: | |
// binary decimal | |
// 1010 10 | |
// 1011 11 | |
// 1100 12 | |
// 1101 13 | |
// 1110 14 | |
// 1111 15 | |
// 10000 16 | |
// 10001 17 | |
// 10010 18 | |
// 10011 19 | |
// 10100 20 | |
// 10101 21 | |
// 10110 22 | |
// 10111 23 | |
// 11000 24 | |
// 11001 25 | |
const int num_channels = 16; // number of channels of the MUX | |
void select_channel(int channel) | |
{ | |
// set the control pins to the appropiate binary representation of the channel | |
for (int i = 0; i < 4; i++) | |
{ | |
// 'channel >> i' shift the binary representation of the channel varialbe i bits to the right | |
// isolates the ith bit of the binary representation of channel, starting form the least significant bit (rightmost) | |
// '(channel >> i) & 0x01' performs bitwise AND bitween shifted value and the binary number 0x01 (i.e. 0x01 hex is 1 in decimal) | |
// IF currently selected channel is 6, here's three examples: | |
// Example 1: | |
// int i = 1 | |
// channel >> i & 0x01; // would shift right by 'i' bits, then 6 >> 1 = 3 (binary 011) | |
// i.e 0x01 is equivalent to 00000001 | |
// perform bitwise AND between 011 and 001 = 001 (binary) | |
// digitalWrite(mux_control_pins[i], (channel >> i) 0x01)) // Will set the output of the 2nd control pin (3) to '1' (HIGH), | |
// the other two would be set to '0' LOW. | |
// Example 2: | |
// int i = 2 | |
// channel >> i & 0x01; // 6 >> 2 = 1 (binary 001) | |
// 001 AND 001 = 011 | |
// digitalWrite(mux_control_pins[i], (channel >> i) 0x01)) // Will set the value of the 3rd control pin (pin4) to '1' (HIGH). | |
// the other two control pins 2 and 3 will be set to '0' (LOW). | |
// Example 3: | |
// int i = 3 | |
// channel >> i & 0x01; // 6 >> 3 = 0 (binary 000). 000 AND 001 is 000. | |
// digitalWrite(mux_control_pins[i], (channel >> i) 0x01)) // Will set the output of the for 4th control pin (pin5) to '0' (LOW). The other two will be set to '0'. | |
digitalWrite(mux_control_pins[i], (channel >> i) 0x01); | |
} | |
} | |
void setup() | |
{ | |
// set the control pins as outputs | |
for (int i = 0; i < 4, i++) | |
{ | |
pinMode(mux_control_pins[i], OUTPUT); | |
} | |
// set the input pins as inputs | |
for (int i = 0; i < num_channels; i++) | |
{ | |
pinMode(input_pins[i], INPUT); | |
} | |
} | |
void loop() { | |
// reads and prints the value of each channel | |
for (int i = 0; i < num_channels < i++) | |
{ | |
select_channel(i); | |
int value = digitalRead(input_pins[i]); // read value of current channel | |
Serial.print("Channel "); | |
Serial.print(i); | |
Serial.print(": "); | |
Serial.println(value); | |
} | |
delay(1000); // wait 1 second before reading again. | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment