
??CD74HC4067 作用是選通一路對(duì)十六路模擬信號(hào),就像一個(gè)單刀多擲開(kāi)關(guān),根據(jù)芯片上 S0-S3 四個(gè)不同管腳的組合,讓SIG管腳和C0-C15導(dǎo)通(每次只能連接一個(gè)),它適用于數(shù)字和模擬信號(hào),可以只用5針最多連接16傳感器系統(tǒng),也可以用它來(lái)管理多個(gè)設(shè)備。
//測(cè)量換算每個(gè)Pin的電壓
int s0 = 34;
int s1 = 35;
int s2 = 32;
int s3 = 33;
int SIG_pin = 25;
void setup() {
// put your setup code here, to run once:
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
int v;
for (int i = 0; i < 16; i ++)
{
Serial.print("Value at channel ");
Serial.print(i);
Serial.print(" is : ");
v = readMux(i);
Serial.println(v * 5.0 / 4096);
}
Serial.println(" ");
delay(3000);
}
int readMux(int channel)
{
int controlPin[] = {s0, s1, s2, s3};
int muxChannel[16][4] =
{
{0, 0, 0, 0}, //channel 0
{1, 0, 0, 0}, //channel 1
{0, 1, 0, 0}, //channel 2
{1, 1, 0, 0}, //channel 3
{0, 0, 1, 0}, //channel 4
{1, 0, 1, 0}, //channel 5
{0, 1, 1, 0}, //channel 6
{1, 1, 1, 0}, //channel 7
{0, 0, 0, 1}, //channel 8
{1, 0, 0, 1}, //channel 9
{0, 1, 0, 1}, //channel 10
{1, 1, 0, 1}, //channel 11
{0, 0, 1, 1}, //channel 12
{1, 0, 1, 1}, //channel 13
{0, 1, 1, 1}, //channel 14
{1, 1, 1, 1} //channel 15
};
//loop through the 4 sig
for (int i = 0; i < 4; i ++)
{
digitalWrite(controlPin[i], muxChannel[channel][i]);
}
//read the value at the SIG pin
int val = analogRead(SIG_pin);
//return the value
return val;
}